CIS 421CSS › Box Model

CSS Box Model

What is the box model?

Every element in the XHTML page produces a box, which is only visible if you have the background or borders turned on

This class, called boxmodelexample, is defined as:
.boxmodelexample {
border-style:ridge;
background:#f4dc7d; /* defines color */
padding:10px;
margin:1em; }

The same boxmodel class applied to header 2 tags

top

Styles allow the definition of 3 aspects of the box model:

Margin

  • determines the spacing between elements: can be set to same height all around (e.g., 5 em) or each margin defined separately -- list starts with top margin, proceeds clockwise
    margin:0 5em 10em 3em; defines top margin=0; right margin=10em, bottom margin=10em, left margin 3em.
    margin:5em 10em; set the top and bottom margins to 5em and the right and left margins to 10em
  • Margins are outside the box model element so they increase the amount of space a box model element occupies

Collapsing Margins

  • The Stylin' book states (p.108):
    • "Vertical margins collapse"
    • "When top and bottom margins meet, they overlap until one of the margins touches the border of the other element."
  • This means that if one box model element follows another box model element on the HTML page, the margin separating the two elements vertically will be the larger margin separating them (i.e., the bottom margin of the first element or the top margin of the following element.

Padding

  • Padding, which defines distance between the content of the box and the edge of the box -- set same all around or for each side separately

Border

  • Border:
    thickness (thick, medium, thin, or any unit such as 3px);
    style (hidden, dotted, dashed, solid, double, groove, ridge, inset, outset) and
    color (set with RGB or hex codes) -- can be defined for entire box or each side individually
  • Because browser can have margins and padding predefined, good practice to set margins and padding to 0 in body tags.
  • Padding adds to the size of the box model element. A 400pixel wide element with padding set to 20 pixels all around is really 440 pixels wide

top

Positioning the box model element

Determines the reference point for position of the element box.

top

Four positioning values

Static

  • the default
  • each element is laid out one after the other
  • margins create the space in between each
  • same as no positioning

Relative

  • Moves an element from its default position
  • saves the space it would normally occupy for the element

Absolute

  • takes element out of flow of the document.
  • Space previously occupied by element is gone.
  • Element is positioned with respect to top-level element of page, which is the body element

Fixed

  • similar to absolute positioning
  • except element's positioning context is the browser window, so element does not move when page is scrolled.
  • works in Firefox and IE7 but not in IE6

top

Contextual Positioning

When you move an element using properties of top, right, bottom, you are positioning the element relative to some other element, and the other element is known as the positioning content.

Floating and Clearing

In a page layout with columns, typically the main column is not the left-most column. The left-most column contains navigational links and the main content column sits next to it. CSS div tags can define the size and positioning of these columns.

To make things easier for the disabled viewer, it is better to have the content of the main column in the upper part of the HTML document above the content for the left-side navigation, so the user does not have to read through the navigation hyperlinks.

To solve this layout problem you can define div containers for different content areas of the page and position them using div position properties

top

Float property

The Float property lets you move an element out of its normal document flow, so that it can sit next to another element. This accomplishes the same purpose as layout tables.

image style

img {
margin:10px;
float:left;
}

Dreamweaver 4 textbook The float property lets text flow around images, similar to the HTML property align. This image style reads img {margin:10px; float:left;}

It puts a 10 pixel margin around all 4 sides of the image, and floats it on the left side of the column. If the float is not cleared by a Clear Property then the paragraph will wrap around the bottom of the image. If you want the image and the text to be in their own columns, you need to clear the float.

top

The Clear Property

Without clearing the float, each insertion of an image and some associated text wraps like this:

The clearfloats style

.clearflo {clear:both;}

Dreamweaver 4 textbookThe first book is about Dreamweaver 4 -- really out of date stuff

Dreamweaver MX textbookThe second book is about Dreamweaver MX, also out of date.

Don't Make me ThinkThe third book is the usability text for this class-- not out of date.

To create CSS layout in which the images are in one column and the associated text in a column next to it (just like table layout), add in a style that clears the float.

To do that, put the content of each image/text pair inside a clearfloats class that clears the float.

Dreamweaver 4 textbookThe first book is about Dreamweaver 4 -- really out of date stuff -- interesting how the image extends beyond the lower margin of the block. In this case the float has not been cleared.

top

Try it

top