Thursday, June 13, 2013

Chapter 8: CSS Padding



Chapter 8: CSS Padding

Inherited: No
Padding is the distance between the border of an (X)HTML element and the content within it.
Most of the rules for margins also apply to padding, except there is no "auto" value, and negative values cannot be declared for padding.

padding-top: length percentage;
padding-left: length percentage;
padding-right: length percentage;
padding-bottom: length percentage;

As you can also see in the above example you have 2 choices of values for the padding property
·         length
·         percentage
You can also declare all the padding of an element in a single property as follows:

padding: 10px 10px 10px 10px;
If you declare all 4 values as I have above, the order is as follows:
    1.      top
    2.      right
    3.      bottom
    4.      left
If only one value is declared, it sets the padding on all sides. (see below)
padding: 10px;
If you only declare two or three values, the undeclared values are taken from the opposing side. (see below)
padding: 10px 10px; /* 2 values */
padding: 10px 10px 10px; /* 3 values */
If you do not declare the padding value of an element, the padding is 0 (zero).

Note: You do not have to add px (pixels) or whatever units you use, if the value is 0 (zero).

You can see in the example below, the main container for this site has 30px (pixels) of padding between the border and the text.

#container
{
width: 70%;
margin: auto;
padding: 30px;
border: 1px solid #666;
background: #ffffff;
}

Chapter 7: CSS Margins



Chapter 7: CSS Margins

Inherited: No
As you may have guessed, the margin property declares the margin between an (X)HTML element and the elements around it. The margin property can be set for the top, left, right and bottom of an element. (see example below)

margin-top: length percentage or auto;
margin-left: length percentage or auto;
margin-right: length percentage or auto;
margin-bottom: length percentage or auto;

As you can also see in the above example you have 3 choices of values for the margin property

·         length
·         percentage
·         auto
You can also declare all the margins of an element in a single property as follows:

margin: 10px 10px 10px 10px;

If you declare all 4 values as I have above, the order is as follows:

1. top
2. right
3. bottom
4. left
If only one value is declared, it sets the margin on all sides. (see below)

margin: 10px;

If you only declare two or three values, the undeclared values are taken from the opposing side. (see below)

margin: 10px 10px; /* 2 values */
margin: 10px 10px 10px; /* 3 values */

You can set the margin property to negative values. If you do not declare the margin value of an element, the margin is 0 (zero).

margin: -10px;

Elements like paragraphs have default margins in some browsers, to combat this set the margin to 0 (zero).

p {margin: 0;}

Note: You do not have to add px (pixels) or whatever units you use, if the value is 0 (zero).

You can see in the example below, the elements for this site are set to be 20px (pixels) from the body

Body
{
margin: 20px;
background: #eeeeee;
font-size: small;
font-family: Tahoma, Arial, "Trebuchet MS", Helvetica, sansserif;
text-align: left;
}

Chapter 6: CSS Spans

Chapter 6: CSS Spans

Spans are very similar to divisions except they are an inline element versus a block level element. No line break is created when a span is declared.

You can use the span tag to style certain areas of text, as shown in the following:

<span class="italic">This text is italic</span>

Then in my CSS file:
.italic{
font-style: italic;
}
The final result is: This text is italic.
The purpose of the last 2 chapters was to provide you with a basis for using CSS in an (X)HTML file. For a more detailed explanation of XHTML please visit W3Schools.com

Chapter 5: CSS Divisions



Chapter 5: CSS Divisions

Ok so you have finished the first 4 chapters in my series. You have learned the very basics of CSS, how the syntax works and a bit about classes and IDs. Now we are gonna take a quick break from CSS and focus on the (X)HTML side of using it.

Divsions
Divisions are a block level (X)HTML element used to define sections of an (X)HTML file. A division can contain all the parts that make up your website. Including additional divisions, spans, images, text and so on. You define a division within an (X)HTML file by placing the following between the <body></body> tags:

<div>
Site contents go here
</div>

Though most likely you will want to add some style to it. You can do that in the following fashion:

<div id="container">
Site contents go here
</div>

The CSS file contains this:

#container{
width: 70%;
margin: auto;
padding: 20px;
border: 1px solid #666;
background: #ffffff;
}

Now everything within that division will be styled by the "container" style rule, I defined within my CSS file. A division creates a line break by default. You can use both classes and IDs with a division tag to style sections of your website.