Sept. 20, 1998
Web pages not only look better with a little white space in the margins, but it's less tiring to read shorter lines than it is to read lines that span the entire screen. Developers have resorted to every trick they could think of to achieve margins - from tables, to transparent gif spacers. Some have even created whole pages as a gif file, putting their text into the graphic. CSS has covered this aspect of design with the margin-property.
Margins may be applied to all
block elements, and may be specified
for an entire document by applying them to the
BODY element.
This page has the BODY properties set to a white background with a wide red
border. The three boxes below demonstrate the nesting effect created by
applying the same margin-left value to all three paragraphs and placing
one inside the other via the DIV tag. An
image
of the effect is provided for those who don't have CSS capable browsers.
.ex1 {
color : black;
background : #E6E6FA;
border : .5em solid #330099;
margin-left : 75px;
}
.ex2 {
color : black;
background : #FFFFFF;
border : solid;
margin-left : 75px;
}
.ex3 {
color : black;
background : #E6E6FA;
border : .5em solid #330099;
margin-left : 75px;
}
Put it all together in HTML:
<DIV class="ex1">
Document content.
<DIV class="ex2">
Document content.
<DIV class="ex3">
Document content.
</DIV></DIV></DIV>
'margin-top', 'margin-right', 'margin-bottom', 'margin-left'Top, right, bottom and left margins may be set independently of each other. Note: In some browsers, the line below that states "Be careful you don't extend your content right off of the page when using negative values!" will actually run off of the page so that the beginning of the sentence is lost!
This can get to be fun as we play with the different margin settings. Negative values may be applied to the margin values giving the effect you see here of the list background extending outside of the paragraph block. Negative values may be implemented differently, or may not display at all, depending upon your browser. Use care when specifying a negative value. Be careful you don't extend your content right off of the page when using negative values!
.ex5 {
color : black;
background : #FFFFFF;
border : solid;
margin-left : -75px;
}
In this example I've added a right margin of 100px to this box; notice how it doesn't extend all the way to the right as the example above does.
.ex6 {
color : black;
background : #FFFF66;
border : .5em solid #CC9933;
margin-left : 75px;
margin-right : 100px;
}
An image of the effect.
marginThe margin property is shorthand for setting all four margin sides at the same time.
Margin Values
Notes:
1.W3C Additional Resources:
Putting Style Sheets in Perspective: Marginally Speaking Putting Style Sheets in Perspective: text-indent
| |||||||
|
|---|
|