CSS, How to Style with Margins

Wednesday, 16. December 2009

When designing a CSS based website it is very useful to understand Margins. I have found that it is much easier to deal with browser compatibility when  one uses proper margin styles. Trying to set a top: -10px seems to cause browser issues that can be easily avoided by using margin-top:-10px.

Margins can be applied to the outside of any block level or inline element. They create space between the edge of an element and the edge of any adjacent elements.

Margins can be applied to the individual sides of an element.

p { margin-top: 0px}
p { margin-right: 5px}

h1 { margin-bottom: 3px}
h1 { margin-left: 4px}

Margins can also be used with a single short tag. If one value is set this value will be applied to all sides of the element.

p { margin: 5px}

If two values are set, the first value will be applied to the top and bottom of the element while the second value will be applied to the left and right margins.

p { margin: 10px 5px}

If three values are set the fist value is applied to the top, the second value is applied to the left and right and finally the third value is applied to the bottom.

p { margin: 10px 5px 7px}

If for values are set the first value is applied to the top, the second value is applied to the right, the third value is applied to the bottom and the forth is applied to the left.
Top, Right, Bottom, Left

p { margin:10px 5px 7px 3px}

CSS, How to Padding Padding and more Padding

Monday, 24. August 2009

Padding can be applied to the outside edges of the content area of any block level or inline element. Padding creates the space between the edge of the element and its content.

Like margins, padding can be applied to individual sides of a box.

p {padding: 10px}
h1 {padding-top:0px}
h2 {padding-right:5px}
h2 {padding-bottom:7px}
h3 {padding-left:10px}

Padding can alse be applied using a single shorthand property. If one padding value is specified, it applies to all sides of an element:

p {padding:5px}

If two values are specified, the top and bottom margins are set to the first value and the right margins are set to the second:

p {padding: 5px 0}

If three values are specified, the top is set to the first value, the left and right are set to the second, and the bottom is set to the third:

p {padding: 10px 0 20px}

If four values are specified, they apply to the top, right, bottom, and left:

p {padding 5px 0px 7px 15px}

CSS, How to deal with background color and images.

Sunday, 23. August 2009

The background-color property sets the background color of an element.

The backgound-image property applies a background image to an element, which will appear on top of any background-color.

body {
background-color:#000
}

h1 {
background-image:url(header.png);
}

CSS, How to deal with margins?

Wednesday, 19. August 2009

Margins can be applied to the outside of any block level or inline element. They create space between the edge of an element and the edge of any adjacent elements.

Margins  can be applied to individual sides of a box:

p { margin-top: 0;}
p { margin-right: -5px;}
p { margin-bottom: 10px;}
p { margin-left: 5px;}

Margins can also be applied using a single shorthand property. If one margin value is specified, it is applied to all sides of an element:

p { margin: 5px;}

If two values are specified, the top and bottom margins are set to the first value and the right and left margins are set to the second.

p { margin: 5px 10px;}

If three values are specified, the top is set to the first value, the left and right ar set to the second, and the botton is set to the third.

p { margin: 5px 10px 3px;}

If four values are specified, they apply to the top, right, bottom, and left.

p { margin: 5px 6px 7px 8px;}

It should be noted that you can use negative values for the margin. This can cause the element to overlap another element.

CSS, How to properly set box width.

Saturday, 15. August 2009

Setting the Box Width

The width of an element is applied to the content area. Other measurements such as padding, border and margins are added to this width.

For example, if an element is specified with width: 200px; the content area is 200px wide. If padding, boder, or margin are applied to the same element, their measurements are added to the overall width.

However, this method does not applie to IE 5 or 6, but who really cares. Anyone using IE 6 needs to upgrade.

boxModel

CSS, How to use Universal Selectors

Thursday, 6. August 2009

Universal selectors are used to select any element. For example , to set the margins and padding on every element to 0, * can be used.

*{
margin: 0;
padding: 0;
}

Universal selectors also can be used to select all elements within another elements. The code below will select any element inside the <p> element.

p * {
color: red;
}

CSS, How to use Shorthand Margins and Padding.

Sunday, 2. August 2009

Margins create space between the edge of an element and the edge of any adjacent elements. Padding creates the space between the edge of the element and its content. The margin and padding shorthand properties also can be used to make CSS code more concise.

The margin property can combine margine-top, margin-right, margin-bottom, and margin-left. The padding property can combine padding-top, padding-right, padding-bottom, and padding-left.

The margin and padding properties also can be used to style different values for each side of an element. Values are applied in the follow order: top, right, bottom, and left – clockwise starting at the top.

The <p> element can be styled with padding-top, padding-right, padding-bottom, and padding-left or with a single padding property.

CSS Longhand

p {

color: maroon;
font: 80% arial, helvetica, sans-serif;
padding-top: 1em;
padding-right: 2em;
padding-bottom: 3em;
padding-left: 4em;

}

CSS ShortHand

p {

color: maroon;
font: 80% arial, helvetica, sans-serif;
padding: 1em 2em 3em 4em;

}