the background color and image expand under the border as well, the background
may protrude through the b order if it is dotted or dashed. The margin represents
white space outside of the border and it sepa rates one element from the other. The
backgr ound color and image don’t go under the margin, so the margin is c ompletely
transparent.
Mike: You mean white?
Professor: No, transparent. If an element is drawn inside an other element, then that
other element’s color is seen through the contained element’s margin.
The perimeter of ea ch of the four boxes of the CSS box is called an edge. For exam ple,
the rectangle marking off the paddin g box is called a padding edge. As you can see
in the above figure, the margin, border, and padding can be b roken down into four
segments: top, right, bottom, and left. You can set each of them individually using
correspo nding CSS properties.
I would like you to pay special attention to the difference between the margin and
padding since the two are often confused. Both add space around the content and
you use them to separate one element from another. Unless you apply a bor der or a
backgr ound color or image, it is actually ha rd to tell whether space around the con -
tent is caused by the padding or the m argin. With a border or background, h owever,
the difference becomes significant. Since the paddin g separates the content from the
border, it keeps the content from appearing jammed inside the box. Margins, on the
other hand, provide white space between elements and relax the tension between them,
which gives a page a lighter appearance .
Maria: So far it looks quite straightforward, but I’m afraid things will become com-
plicated in practice.
Professor: I’m afraid that you’re right. Some of the conce pts you’re going to learn
today are pretty difficult to grasp. So, let’s dive into some examples, so you can g et a
clearer picture of the idea.
Consider, for example, the next fragments of CSS and HTML code:
.example-box {
width: 60px;
height: 60px;
padding: 10px;
border: 5px solid;
margin: 20px;
}
<body>
<div class="example-box">I am a box.</div>
</body>
We will investigate the behavior of the code in Google Chrome. Having loaded the
code into the browser, you pre ss Ctrl+Sh ift+I, or select
Tools→Developer Tools
5.2. CSS Box Model 75