CSS Basics: The Ultimate Guide to Box Model Essentials – A Must-Have Skill for Web Designers

Deutsch

The CSS Box Model Explained

In CSS we use the so-called Box Model. The box model is a fundamental principle that is necessary for understanding CSS. Generally, every element in CSS is treated as a rectangular box which consists of

The following figure shows a schematic representation of the box model.

schematic representation of the box model
Box Model Schema

Each HTML element is considered a rectangular area in the box model. The CSS Properties padding, border and margin are the cornerstones for the layout of a web page.

Each box has the following features:

The type of positioning (position) determines what the spacing from the sides will refer to.

Content Box

Text, images, and other media reside in the content area of an HTML element. A particular HTML element is defined by this area, also known as Content Box. Its size can be customized using the width and height properties. If width is not given, then

If the height property is not given, then each element will only be as high as it needs to be for the content it contains.

Typical properties for the content box are:

Padding Box

Padding refers to the internal spacing between the content of an HTML element and its border. The Padding Box belongs to the Content Box. Therefore, the padding values are added to the width and height of the content area. The padding box always has the same background color as the content area. Padding can be the same for all four sides or set individually for each side. You can specify 1 to 4 values for the padding property.

Negative values for padding are not allowed.

Border Box

HTML elements always have a border. Even a border that is not visible (border-width: 0) is part of the element. The Border Box encloses the content box. This means, it encloses both the padding box and the border of the element.

The border box doesn't have a background.

Just like padding, a border cannot have a negative width (thickness).

Margin Box

The outer spacing (margin) is a forced empty space (also known as whitespace) between an element and the parent element as well as adjacent elements. The Margin Box is the area between the border of an element and adjacent elements. In other words, margin is an invisible space around the border that causes a spacing to other elements.

Negative margin values are allowed. However, a negative value for margin can result in overlapping elements.

Unlike Padding, Margin has no effect on the width or height of the element. The total space required for the element is the Content Box plus Border and Margin.

Margin can be the same for all four sides, or it can be set for each side individually. The margin property accepts one, two, three or four values.

Note:
  1. A margin can also be defined for the parent element as well as for the elements adjacent to it.

  2. The margin area is always transparent and cannot be styled to fit your needs. This means that margin areas always have the same background as the parent element.

Example CSS

/* define regions */

div.box1 { /* outer spacing: all sides */ margin: 0.5em; /* inner spacing: all sides */ padding: 0.3em; /* width: 30% of entire window */ width: 30%; /* background: antiqueWhite */ background: #faebd7; /* border: black double solid line */ border: 0.125em double #000000; /* container »enclose«: right beside box1 */ float: left; } div.box2 { /* inner spacing: all sides */ padding: 0.5em; /* width: 100% of »enclose« */ width: 100%; /* background: lightRed */ background: #ff7276; /* border: white dotted line */ border: 0.125em dotted #ffffff; } div.enclose { /* outer spacing: vertical, horizontal */ margin: 0.5em 0.5em; /* width: 60% of entire window */ width: 60%; /* background: marineBlue */ background: #bbe8bb; /* border: grey double solid line */ border: 0.5em double #888888; /* container »enclose«: right beside box1 */ float: left; } /* paragraph inside box1 */ div.box1 p { /* inner spacing: all sides */ padding: 0.5em; /* background paragraph: lightYellowGreen */ background: #dfdfbc; /* text color darkGrey */ color: #404040; /* border: red single solid line */ border: 0.125em solid #dd0000; } /* paragraph inside box2 */ div.box2 p { /* inner spacing: all sides */ padding: 0.5em; /* background paragraph: lightGreen */ background: #bbe8bb; /* text color: black */ color: #000000; }
Example HTML
<div class="box1">
<p>In contrast to previous websites, for example, we no longer have to program two different websites for Internet Explorer and another browser. One page that is suitable for all browsers is sufficient.
</p>
</div>

<div class="enclose">
<div class="box2">
<p>Just when he had shot the thing of his life and was about to disappear with the loot, one of his countless accomplices had the same idea.</p>

<p>Who would follow him late at night? He tried to feel his way in the darkness and froze when he saw a dark figure in the dense fog. Was it really all over now?</p>
</div>
</div>
Output of the HTML code

In contrast to previous websites, for example, we no longer have to program two different websites for Internet Explorer and another browser. One page that is suitable for all browsers is sufficient.

Just when he had shot the thing of his life and was about to disappear with the loot, one of his countless accomplices had the same idea.

Who would follow him late at night? He tried to feel his way in the darkness and froze when he saw a dark figure in the dense fog. Was it really all over now?

This background       represents the screen and is not part of the sample code.