CSS Box Model
CSS Box Model
The easiest way to understand these components is to use one of the most
versatile tools available to us as web designers: the <div> element.
Introducing the <div> Element
The <div> ("division") element groups other elements on the
screen.
By setting the width and height attributes via CSS, we can reserve
a precise amount of space on our page for specific content.
The actual content is nested and contained within the opening <div>
and closing </div> tags.
When we apply CSS styling directly to the <div> element, all the
elements contained within that <div> will inherit that style.
By using multiple <div> elements as building blocks, we can design
an entire web page layout.
Example <div> Element
Let's create a box on the screen and add a border around it:
<style type="text/css">
.box200 {
width: 200px;
height: 200px;
border: 1px solid black;
color: blue;
}
</style>
...
This is before the box.
<div class="box200">
This is a 200 by 200 pixel box with
a 1px border.
</div>
This is after the box.
With the border set, we can see the
...
exact space taken up on the page.
Notice that there is almost no space separating the text from the box border. Elements
such as paragraphs, headers, and lists automatically insert padding and margins, but
plain text does not do so.
Adding Padding and Margin
Let's create some space between elements by adding both padding and
margin:
<style type="text/css">
.box200 {
width: 200px;
height: 200px;
border: 1px solid black;
color: blue;
padding: 10px;
margin: 10px;
}
</style>
...
The 10 pixel padding adds buffer space, on all four sides, between the
content and border. The 10 pixel margin adds buffer space, on all four sides,
between the border and surrounding elements.
242px high
242px wide
Calculating Total Dimensions
When we are planning our page, we have to calculate exactly how much
screen space a <div> or any other element will use. The formula is:
Total element width = defined width + left padding + right padding + left
border + right border + left margin + right margin.
Total element height = defined height + top padding + bottom padding + top
border + bottom border + top margin + bottom margin.
Padding, borders, and margins do not have to be the same on all four sides,
as they are in this example. Let's see how to set these individually.
Setting Individual Margins
We can set the specific margin sizes by using these four properties:
margin-top:
margin-right:
margin-bottom:
margin-left:
Again, these are rarely used. Instead, the same shorthand method is
employed:
If the three border properties will be identical on all four sides, we can use
a single-line border shorthand, as we did in an earlier lesson:
border: 5px solid blue;
Example of Customized Settings
Here we have set custom padding, borders, and margins:
<style type="text/css">
.box200 {
width: 200px;
height: 200px;
color: blue;
padding: 40px 10px 0px 10px;
margin: 25px 5px;
border-width: 2px 5px;
border-style: solid dotted;
border-color: red blue green maroon;
}
</style>