CSS box model
// updated 2025-05-01 08:21
The box model refers to the representation of an HTML element when shown on a browser, in terms of its:
width
(left to right length)height
(top to bottom length)padding
(inner spacing)border
(boundary that separates an element's inner and outer space)margin
(outer spacing)
In addition, there exist:
overflow
(whether an element's content spills over its borders) with values:scroll
(the browser will give the container a scroll bar)auto
(...scroll bar only if the content exceeds the container size)hidden
(the content will "disappear" beyond the container)visible
(the content will "spill over" beyond the container)
overflow-x
(restricts the overflow value to the horizontal dimension)overflow-y
(restricts the overflow value to the vertical dimension)
Width and height
Self-explanatory, the width
refers to the left-right length of an element, e.g.:
1div.example {
2 width: 250px;
3}
Also, the height
refers to the top-bottom length of an element, e.g.:
1div.example {
2 height: 100%;
3}
Min and max
If we had previously specified the height
and the width
, the following properties could still override those dimensions already placed on the selector:
min-height
min-width
max-height
max-width
For example:
1div.example {
2 height: 140px;
3 width: 90px;
4}
5
6div.example {
7
8 /* ensures the div is at least 50px tall */
9 min-height: 50px;
10
11 /* ensures the div is at most 100px tall */
12 max-height: 100px;
13
14 /* ensures the div is at least 100px wide */
15 min-width: 100px;
16
17 /* ensures the div never extends beyond the full width of its container */
18 max-width: 100%;
19
20}
These "min" and "max" properties work especially better if the initial height and width properties used percentages; at some point:
- a 25%
width
might be "too narrow" (in which case, we would use amin-width
in pixels) - a 100%
width
might be "too wide" (in which case, we would use amax-width
in pixels)!
Paddings
The padding refers to the inner whitespace of a container; it literally pads the content from the container's edges:
1div.example {
2 padding-top: 10px;
3 padding-right: 20px;
4 padding-bottom: 30px;
5 padding-left: 40px;
6}
Shorthand
Also, the following shorthand notations remain popular among developers, as each pair of "top and bottom" and "left and right" paddings often have the same values:
1div.example {
2 padding-top: 20px;
3 padding-right: 10px;
4 padding-bottom: 20px;
5 padding-left: 10px;
6}
7
8/* clockwise shorthand */
9div.example {
10 padding: 20px 10px 20px 10px;
11}
12
13/* top-bottom left-right shorthand */
14div.example {
15 padding: 20px 10px;
16}
Margins
The margin refers to the outer whitespace of a container; the container's size does include this region that separates the container from all other adjacent HTML elements!
1div.example {
2 margin-top: 10px;
3 margin-right: 20px;
4 margin-bottom: 10px;
5 margin-left: 20px;
6}
7
8div.example {
9 margin: 10px 20px;
10}
We notice in the above that the notation of margins imitate those of the paddings; also:
1div.topcontainer {
2 margin-bottom: 50px;
3}
4
5div.bottomcontainer {
6 margin-top: 25px;
7}
...that is, for an HTML that has:
1<div class="topcontainer">...</div>
2<div class="bottomcontainer">...</div>
Collapsing margins
When two margins meet, the smaller margin combines with the larger margin:
- a
50px
space will appear between the two<div>
elements instead of75px
!
"Auto" centering margins
Using auto
for the margin in the left-right value centers it horizontally:
1div.centered {
2 margin: 0 auto;
3}
Recall that this is really just shorthand for:
1div.centered {
2 margin-top: 0;
3 margin-right: auto;
4 margin-bottom: 0;
5 margin-left: auto;
6}
Borders
The border refers to the space between the outer margin and the inner padding:
1div.example {
2 border-width: 10px;
3 border-style: solid;
4 border-color: red;
5}
6
7div.example {
8 border: 10px solid red; /* width + style + color */
9}
As with paddings and margins, the border property has a shorthand but its own notation!
Border width
This "border thickness" property usually has an amount in pixels (px
) but can use essentially any other units of distance!
Border style
The border style refers to the border's appearance and takes one value out of a closed set:
1border-style: none;
2border-style: hidden;
3border-style: dotted;
4border-style: dashed;
5border-style: solid;
6border-style: double;
7border-style: groove;
8border-style: ridge;
9border-style: inset;
10border-style: outset;
11
12/* CLOCKWISE SHORTHANDS */
13
14/* all four sides are solid */
15border-style: solid;
16
17/* top and bottom are dotted | left and right are solid */
18border-style: dotted solid;
19
20/* top is hidden | left and right are double | bottom is dashed */
21border-style: hidden double dashed;
22
23/* top is none | right is solid | bottom is dotted | left is dashed */
24border-style: none solid dotted dashed;
Border color
Self-explanatory and can take on any of the CSS color notations:
1div.example {
2 border-color: red;
3 border-color: #990000;
4 border-color: rgb(255, 0, 0);
5 border-color: hsl(0, 50%, 50%);
6}
Border radius
Furthermore, the border can have rounded edges with the border-radius
property:
1div.example {
2 border-radius: 5px;
3}
A border can become a circle with a border-radius
value of 50%
:
1div.example {
2 border-radius: 50%;
3}
We can even choose which corner can have border "radii":
1div.example {
2 border-top-left-radius: 10%;
3 border-top-right-radius: 20%;
4 border-bottom-right-radius: 30%;
5 border-bottom-left-radius: 40%;
6}
7
8div.example {
9 border-radius: 10% 20% 30% 40%;
10}
The latter selector, of course, represents the "clockwise shorthand" for the former selector in the example above!
Overflows
The overflow refers to how much content the container will display:
1div.example {
2 /* browser will show a scrollbar on the div */
3 overflow: scroll;
4}
5
6div.example {
7 /* browser will show a scrollbar only when necessary */
8 overflow: auto;
9}
10
11div.example {
12 /* content will 'disappear' after the end of the container */
13 overflow: hidden;
14}
15
16div.example {
17 /* content will 'spill over' after the end of the container */
18 overflow: visible;
19}
There also exist overflow-x
and overflow-y
to limit the overflow value in, respectively, the horizontal and vertical directions!
overflow-x
will apply to the horizontal (left-right) directionoverflow-y
will apply to the vertical (top-bottom) direction