CSS box model
sizes + margins + borders + paddings + overflows
Last updated
sizes + margins + borders + paddings + overflows
Last updated
Each element has the following basic properties, all of which have a numerical value (usually in pixels or percentage of its parent container):
width (left to right length)
height (top to bottom length)
padding (inner white space)
margin (outer white space)
border (boundary space that separates the inner white space and outer white space)
In addition, there exists:
overflow (determines whether content inside a limited container spills over its border or not), most commonly taking on the values:
scroll (the browser will give the container a scroll bar)
auto (if the content exceeds the size of the container)
hidden (the content will "disappear" after the end of the container)
visible (the content will "spill over" after the end of the container)
overflow-x (restricts the overflow value to the horizontal dimension)
overflow-y (restricts the overflow value to the vertical dimension)
Self-explanatory, the width
determines the left-right length of an element, e.g.:
Also, the height determine the top-bottom length of an element, e.g.:
In addition to pixels and percentages, we could also use different units!
If we had specified the height and width, the min-height
, min-width
, max-height
and max-width
properties can override any height and width already placed on the selector:
Padding refers to the inner whitespace of a container; it literally pads the content from the container's edges!
In the above example, the two declaration blocks have identical properties and values; the latter functions as a shorthand version of the former ... it moves clockwise from the top!
Also, the following long-form and short-hand remain popular among developers, as top and bottom (and left and right) often have the same values:
Margin refers to the outer whitespace of a container; the container's size does include this region that separates the container from other elements!
We notice in the above that the notation of margins imitate those of the paddings; also:
...for an HTML that has:
When two margins meet, the smaller margin combines with the larger margin; in the example above, a 50px space will appear between the two <div>
elements, instead of 75px!
Using auto
for a margin in the left-right value centers it horizontally:
The border refers to the space between the (outer) margin and the (inner) padding:
As with paddings and margins, the border property has a shorthand but its own notation!
This "border thickness" property usually has an amount in pixels (px
) but can use essentially any other units of distance!
The border style refers to the border's appearance and takes one value out of a closed set
Self-explanatory and can take on any of the CSS color notations
Furthermore, the border can have rounded edges with the border-radius
property:
A border can become a circle with a border-radius
value of 50%
:
We can even choose which corner to set our border "radii":
(Of course, the latter serves as the "clockwise shorthand" for the former in the above example!)
The overflow refers to how much content the container will display:
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) direction
Overflow-y
will apply to the vertical (top-bottom) direction