Free Download Tutorial

16.6.08

CSS Tutorial : Understanding Box Model

Today’s tutorial is going to be all theory. The box model is an inevitable part of CSS, and understanding it is critical if you want to do anything remotely interesting. Like most of CSS, it’s a lot simpler than it sounds. Here it is:

The box model governs the way margins, padding and borders are applied to block level elements

Before discussing the box model, it is important to understand the difference between inline and block level elements. Inline elements are things like links, s and s—any element that appears “inline” in a piece of text without inserting any newlines. Block level elements on the other hand always start on a new line and cause any content following them to start on a new line as well. They include things like

,

and the various header tags. Block level elements can contain both block level and inline elements, but inline elements can only contain other inline elements. An inline element can be “promoted” to behave like a block level element using the display: block; property, and the reverse can be achieved using display: inline;—note however that doing this does not change the rules about what kind of elements something can contain. The box model principally applies to block level elements (although some of it works with inline elements as well).

As you can see from the diagram, the box model deals with the width, margin, padding and border around an element. You can specify a width using the width property; width can be specified in pixels, ems or percentages (and other units as well but these are the most common) and applies to the area on the diagram labelled “content area”. It should be noted that the overall width of the box up to the borders (the dark grey area on the diagram) is actually the width, plus the left padding, plus the right padding, plus the width of the left and right borders.

source : http://simonwillison.net/2003/May/26/theBoxModel/

Free Web Hosting with Website Builder
 

Web Site Terms and Condition of Use / Privacy Policy