
| introduction | selectors | properties | advanced | real world CSS text style | text layout | background | border | margin | padding | page layout | element type | user interface | values |
With cascading style sheets, any element can have a margin. A margin is the space between an element and the elements to its top, left, bottom and right. The margin properties are:
The margin properties set the size of the top, left, bottom, right and all margins respectively.
Margins can be specified as either a percentage, a length or using the keyword auto.
Margins can be negative values.
A percentage margin value sets the affected margin to that percentage of the width of the parent element. For instance, a margin-right: 20% sets the width of the right margin to 20% of the width of the element which contains the element. Note that that this is true even for margin-top and margin-bottom.
We cover length values in detail in our section on values. Essentially though there are two types of length value. There are absolute values such as px. If you use one of these, the actual length of the margin will always be the same. You design much more flexible pages if you use a relative value, like em. If you use a relative value the size of the margin will always be proportional to the calculated size of the content of the element. This is to be encouraged.
If no margin value is specified, the margin of an element is zero. That is, there is no space between an element and adjacent elements. Unfortunately most browsers do tend to set default margins on elements such as paragraphs. You can overcome this by specifically setting the margin for these types of element to 0.
An element does not inherit the margin property of the element which contains it.
Margins can be used to achieve similar effects to the positioning properties which we discuss below and padding (we talk about padding next).
You can center an element in its parent, without using text-align on the parent element by giving the element a left-margin and a right-margin of auto.
Using percentage values for your margins is highly recommended for the design of flexible, accessible web pages. A percentage value for a margin ensures that the actual drawn size of the margin will always remain proportional to the size of the viewing portal.
| Internet Explorer | Netscape Navigator | Opera | WebTV | ||||||||
| Windows | Macintosh | (All Platforms) | (All Platforms) | ||||||||
| 4.0 | 5.0 | 5.5 | 6.0 | 4.0 | 4.5 | 5.0 | 4.x | 6.0 | 3.5 | 5.0 | 1.0 |
| B | B | Y | Y | B | B | Y | B | Y | Y | Y | N |
| Netscape Navigator 4.x: generally margins should not be set on list items. Either they don't show at all, or they are incorrectly applied and can cause the item to draw strangely. This is fixed in Netscape 6. In older versions of all browsers, applying margins to inline elements, such as <img> was fraught with danger as it often caused all sorts of strange redrawing problems, and never seemed to work correctly anyway. This is fixed on the most recent versions of all browsers, which all draw margins on inline elements correctly.Netscape Navigator 6 and Opera 5 both draw negative top and bottom margins somewhat strangely. Negative left and right margins are fine.
|
|||||||||||
| introduction | selectors | properties | advanced | real world CSS text style | text layout | background | border | margin | padding | page layout | element type | user interface | values |