Doc Steve
Web Coding Service

Fully Accessible Web Code, Custom Written by Hand
Specializing in html, xml, css, and U.S.§508

Web Programming Samples and References

[ Sample Pages Home ]

Cascading Style Sheets
(CSS2)

The CSS Box

Border, Padding, and Margin

[ Skip Index ]
[ General Examples ] [ Additional Border Examples ] [ Tables ] [ Horizontal Rule Control ] [ Notes ] [ References ]

[ TOP ]

General Examples

General format of margin, padding, and border:

(All examples, unless otherwise noted, are contained within a white box with shaded sides)

<div style="margin : 0em ; border : 0em ; padding : 0em ">
<div style="margin : 0em">
<div style="padding : 0em">
<div style="border : 0em">


Specific Usage Examples

Margin Alone:
<div style="margin:0">
OR
<div style="margin:1em">
Padding Alone:
<div style="padding:0em">
OR
<div style="padding:1em">
Border Alone:
<div style="border:solid">
Margin Top/Bottom & Right/Left:
<div style="margin:0em 3em">
Margin Top/Right/Bottom/Left (Example One):
<div style="margin:0em 1em 1em 3em">
Margin Top/Right/Bottom/Left (Example Two):
<div style="margin:1.5em 1em 0.5em 1em">
Margin & Padding:
<div style="margin:1em; padding:1em">
Padding & Border:
<div style="padding:1em; border:solid">
Margin & Border:
<div style="margin:1em; border:solid">
Margin, Padding, & Border:
<div style="margin:1em; padding:1em; border:solid">

[ TOP ]

Additional Border Examples

From the World Wide Web Consortium:

The <border-style> value type, which may take one of the following values:

none
No border; the computed border width is zero.
hidden
Same as 'none', except in terms of border conflict resolution for table elements.
dotted
The border is a series of dots.
dashed
The border is a series of short line segments.
solid
The border is a single line segment.
double
The border is two solid lines. The sum of the two lines and the space between them equals the value of 'border-width'.
groove
The border looks as though it were carved into the canvas.
ridge
The opposite of 'groove': the border looks as though it were coming out of the canvas.
inset
The border makes the box look as though it were embedded in the canvas.
outset
The opposite of 'inset': the border makes the box look as though it were coming out of the canvas.

Playing with these we get the following (all are padded-out 5pt to make them a bit more readable with a 1-em margin below):

<div style=". . .border:5px > [no border]
<div style=". . .border:5px none gray"> [no border]
<div style=". . .border:5px hidden gray"> [no border]
<div style=". . .border:5px dotted gray">
<div style=". . .border:5px dashed gray">
<div style=". . .border:5px solid gray">
<div style=". . .border:5px double gray">
<div style=". . .border:5px inset gray">
<div style=". . .border:5px outset gray">
<div style=". . .border:5px groove gray">
<div style=". . .border:5px ridge gray">
<div style=". . .border:1em solid"> [solid black border, 1-em width all around (still padded-out to make it a bit more readable)]
<div style=". . .border:1em solid red"> [ditto the above, but red]
<div style=". . .border:2em solid red"> [ditto the above, but 2-em all around]
<div style=". . .border-bottom: 1em solid black; border-left: 1em solid black"> [left & bottom only, 1-em]
<div style=". . .border-right: 1em solid black; border-left: 1em solid black"> [left & right only, 1-em]
<div style=". . .border-right: 2em solid white; border-left: 2em solid white"> [left & right, white, 2-em]

[ TOP ]

Table Example

(1) Effect of Margins in Tables

The following style is used:

  .margin {
    color: red ;
    margin-top: 2em ;
    margin-right: 3em ;
    margin-bottom: 2em ;
    margin-left: 3em ; }
Table 1.1
<td class="margin">xxx<td>
x xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx x
Table 1.2
<td><div class="margin">xxx<div><td>
x xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx x

Note that assigning the class to the <td> element has no effect, but assigning the class within the <td> element via (in this example) a <div> element produces the anticipated effect.

(2) Effect of Padding in Tables

The following style is used:

    .padding {
      color: red ;
      padding-top: 2em ;
      padding-right: 3em ;
      padding-bottom: 2em ;
      padding-left: 3em ; }
Table 2.1
<td class="padding">xxx<td>
x xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx x
Table 2.2
<td><div class="padding">xxx<div><td>
x xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx x

Note that assigning the class to the <td> element or within the <td> element via (in this example) a <div> element both produce the anticipated effect (although note the slight differences in the rows if the browser window's width is slightly adjusted).


(3) Effect of Borders in Tables

The following style is used:

  .border {
    color: red ;
    border-color: #ccffff ;
    border-width: 2em ;
    border-style: double }
Table 3.1
<td class="border">xxx<td>
x xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx x
Table 3.2
<td><div class="border">xxx<td>
x xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx x

Note that assigning the class to the <td> element or within the <td> element via (in this example) a <div> element both produce the anticipated effect.




[ TOP ]

Horizontal Rule Control

The traditional way of controlling the position, shading, and size of horizontal rules has been to do something like this:

<hr align="left" width="5" size="5px" noshade="noshade" />

Which would produce this:

In the strict modes of html 4.1 and xhtml 1.0 and in xhtml 1.1, none of those attributes exist, so all control of the horizontal rule must be accomplished by use of CSS. This is not altogether intutive, but once the techniques are identified it is farily easy. For the above example, the following code was used:

 
<div style="
    margin-top: 1em ;
    margin-left: 0 ;
    margin-right:75% ; 
    background: #808080 ; 
    color: #808080 ;
    border-top: solid 5px "></div>
 

The horizontal rule may be created by either a <div> element or a <hr /> element, so this also works:

 
<hr style="
    margin-top:1em ;
    margin-left:0 ;
    margin-right:75% ; 
    background: #808080 ;
    color: #808080 ;
    border: 1px solid ;
    height:5px " />
 

In the above examples, there are some duplication of controls to account for differences in how Netscape/Mozilla and Internet Explorer draw the line.

The prevailing philosophy at W3C is to move away from using specialized markup to draw the page but instead use the containing elements of the content to format the rendering. Thus, instead of of the following code fragment . . .

<hr />
<p>Some irrelevant confabulations here.</p>

which would be rendered thus:


Some irrelevant confabulation here.

the following would be recommended:

<p  style="
    margin : 0.5em 0 1em ;      /* space the paragraph with respect to page and surrounding content */ 

    border-top-color : #cccccc ; 
    border-top-style : groove ; /* format horizontal rule */ 
    border-top-width : 3px ;  

    padding-top : 1em           /* position paragraph content with repect to horizontal rule */ 
    ">
Some other irrelevant confabulation here.</p>

which would be rendered thus:

Some other irrelevant confabulation here.

That notwithstanding, legacy pages are full of <hr /> elements, and it may be easier for developers to modify them to conform to html/xhtml strict standards than to replace them outright. That stated, here are some more examples using the <hr... format:

Example One

 
<hr 
  style=" 
    height: 3px;
    border-color: #888; 
    border-style :inset ; 
    border-width: 1px " />
 

This is pretty much the default <hr />, which simply entered as <hr /> looks like this:

 
 


Example Two

With adjustment of the margins it may be moved across the page, with more control than simply left/center/right:

Example Two: A

 
<hr
  style="
    margin: 0px 2% " /> 
 

Example Two: B

 
<hr 
  style=" 
    margin: 0px 25% " />
 

Example Two: C

 
<hr 
  style=" 
    margin: 0px 25em 0px 10% " />
 

Example Two: D

 
<hr 
  style=" 
    margin: 0px 10% 0px 55% " /> 
 


Example Three

A thin, unshaded line:

 
<hr style=" 
    margin: 0 auto 0 ; width: 100% ; height: 1px ;
    color : #333333 ; 
    shade : noshade ; 
    background-color : #ffffcc ;
    border-top    : 1px solid ;
    border-bottom : 1px solid  " />
 


Example Four

A thicker, unshaded line:

 
<hr 
  style="
    color: #666;
    background-color: #666;
    height: 3px;
    border: none " />
 


Example Five

A few additional examples:

 
<hr 
  style="
    color : #fff ; 
    height: 5px ; 
    border: 2px inset #000 " />
 
 
<hr 
  style="
    margin: 0 auto 0 ; 
    width: 30% ; 
    height: 3px " />
 
 
<hr 
  style="
    margin: 0 auto 0;
    width: 10% ; 
    height: 3px ;
    background    : gray   ;
    border-top    : gray  1px solid ;
    border-bottom : gray  1px solid " />
 

[ TOP ]

Notes

  1. Use padding instead of <blockquote> or tables for formatting. For example,
    <span class="padding">
    -- OR --
  2. Use margin instead of <blockquote> or tables for formatting. For example,
    <span class="margin">
    (unless, of course, it really is a blocked-quote)
    For the two above examples, the following style was used:
      <style type="text/css">
        .margin {
          color: red ;
          margin-top: 2em ;
          margin-right: 3em ;
          margin-bottom: 2em ;
          margin-left: 3em ; }
        .padding {
          color: red ;
          padding-top: 2em ;
          padding-right: 3em ;
          padding-bottom: 2em ;
          padding-left: 3em ; }
      </style>
    
  3. Simple indent of the first line of a paragraph is accomplished with "text-indent".
  4. The background style of the various areas of a box are determined as follows:

    Thus,

      <style type="text/css">
        body {
          background-color: white }
        td {
          background-color: #ffffff }
      </style>
    

    produces backgrounds of white in general, but yellow in table data cells (     ) .

  5. Padding may have a background color, unlike Margin, which is transparent:
    here it is
    (Note: the above is aligned under the "P" in "Padding" as part of the indent for the <li> elements.)

    The style for the above is as follows:
    <style type="text/css">
      .padding_background_demo {
        font-weight: bold ; 
        font-size: 20px ; 
        color: blue ; 
        background: #ccffcc ; 
        padding: 1em 5em }
    </style>
    
    Notes:

[ TOP ]

References:

- -World Wide Web Consortium
- -Cascading Style Sheets 2
- -Chapter 8, Box model
- -Margin Properties
- -Padding Properties
- -Border Properties

Document: http://
Revised:
[ TOP ]
[ HOME ]

Made with Cascading Style Sheets | Valid CSS! | Valid XHTML 1.1! | Level Triple-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0 | Bobby WorldWide Approved AAA


Contact DocSteve


Copyright © 2004 - 2010
Steve Sconfienza, Ph.D.
All Rights Reserved