Formatting block-level element boxes

The following HTML tags can be treated as block-level elements:

There are the following elements to control:

The block-level elements are handled differently by earlier versions of Netscape (4.7) and Internet Explorer (prior to 5.0). The following set of nested boxes will be correctly displayed by Mozilla, Explorer 6.0, and Netscape 6.0 or above.

box margins

box padding within the border

This is the box body. The body can contain various HTML tags, for example:
  • paragraphs
  • lists
  • tables




Controlling margins

The margin is the space between the block-level element and the parent element. there are four attributes that control the margin size:

AttributesDescription
margin-top: value the space between the top of the box and the top margin
margin-right: value the space between the right side of the box and the right margin
margin-bottom: value the space between the bottom of the box and the bottom margin
margin-left: value the space between the left side of the box and the left margin

where the value is measured in absolute units (inches, em) or percentages of the parent element.

Example:

li {margin-left: 2em; margin-right: 2em; margin-top: 1em}

The four attributes can be combined in a single attribute with the syntax:

margin: margin-top margin-right margin-bottom margin-left




Setting padding size

Padding refers to the amount of space between the element and its border. Four attributes are used to control the size of the element's padding:

padding-top : value
padding-right : value
padding-bottom : value
padding-left : value

One difference to keep in mind is that when padding is expressed as a percentage, it is the percentage of the width of the block-level element rather than the parent element, as is the case for margins.




Formatting the border

The syntax for the border attribute is:

border: border-width border-style border-color

There is also a possibility to format individual borders by using the attributes border-top, border-right, border-bottom, and border-left, respectively. For each individual border a set up to 3 style elements should be supplied.

Warning: Earlier versions of Internet Explorer and Netscape display the boxes differently. Always make sure to provide the box width.

The situation seems to be somehow better with non-nested blocks. The following blocks are displayed approximately the same by Internet Explorer and Netscape Navigator. However, Opera browser (version 5.12) does not show the green background.

Examples of various settings of border-style are shown below:

double line
dashed line
dotted line
solid line
inset line
outset line
ridged line
grooved line
none

Putting all together, the top set of nested boxes is produced by the following code:

<div style="padding : 15px 40px 40px 40px;
            background-color : white">
<h4 style="text-align:center">box margin</h4>
<div style="border : 7px solid red;
            padding : 15px 40px 40px 40px;
            background-color : lightgreen">
<h4 style="text-align:center">box padding</h4>
<div style="background-color : yellow">
This is the box body. The body can contain various HTML tags, for example:
<ul>
  <li> paragraphs
  <li> lists
  <li> tables
</ul>
</div> </div> </div>