This blog covers the basics of the Box Model with a CodePen example at the bottom to guide you along. Jump To CodePen
Box Model
In Chrome Developer tools, you can find a box model you can use to debug your CSS.
The Box Model refers to the boxes that are generated with every HTML element. The browser sees each HTML element as a rectangular box. There are 4 parts of the Box Model:
- Content
- Padding
- Border
- Margin
Inline & Block Level Elements
This link will give you more information on inline and block elements and will also show you all HTML elements with their level type:
Inline Elements
HTML Inline elements take as much space as the text in the content box. They stay on the same line, next to each other until the line runs out of available space, then the next inline element begins on a new line. The width
and height
properties do not affect inline-level elements.
Block Elements
HTML block elements span the entire width of their container but they keep the height of the text in the content box. They always begin on a new line and stack on top of each other, unlike inline-level elements. The width
and height
properties will affect block-level elements but will not affect inline-level elements.
The display
Property
The display property can be used to change the default behavior of inline
/block
level elements with three properties: inline
, block
, or inline-block
.
- Changing the
display
of aninline
element toblock
will allow you to usewidth
andheight
properties. - Changing the
display
of ablock
element toinline
will allow you to place elements side by side instead of stacked on top of each other, but will also makewidth
andheight
ineffective on those elements. - Changing the
display
of eitherinline
orblock
toinline-block
will give characteristics of both to the elements.Width
andheight
values will now affect the elements just like how it affectsblock
level elements, but the elements will now stand next to each other side by side, likeinline
level elements.
More information on the display
property: w3schools display property
- The Content box is where the content resides. Content can be text or images.
- Padding surrounds the Content Box. It gives space inside of the element around the content box.
- Border goes between margin and padding. It surrounds the padding and content box.
- Margin is the space that surrounds the whole element.
All four of these boxes together determine the total area of an element.
The 5 properties of the Box model are: Width, Height, Padding, Border, Margin.
Width
Adjusts the width
of the content box. More information on the width property can be found here: w3schools width property
Height
Adjusts the height
of the content box. More information on the height property can be found here: w3schools height property
Padding
Padding will add or remove space within the element but will do so around the content box. More information on padding
properties can be found here: w3schools padding property
Border
Border is added between padding
and margin
. More information on border
properties can be found here: w3schools border property
Margin
Margin will add or remove space around the element. More information on margin
properties can be found here: w3schools margin property
For Box Properties, we will use the percentage and length data types.
- When percentages are used, it is defined by the size of its containing(or parent) element. If there was a
div
withwidth
of500px
, and the element inside of thediv
uses awidth
of50%
, thewidth
of the inside element would be250px
.
Pixels should be the only Absolute length unit you use for websites.
See CSS Best Practices: https://gist.github.com/basham/2175a16ab7c60ce8e001
Recommendations of unit types per media type:
Media | Recommended | Occasional use | Infrequent use | Not recommended |
---|---|---|---|---|
Screen | em, rem, % | px | ch, ex, vw, vh, vmin, vmax | cm, mm, in, pt, pc |
em, rem, % | cm, mm, in, pt, pc | ch, ex | px, vw, vh, vmin, vmax |
Relative units play nicely with both screen and print media types and should be the most frequently used CSS units.
Unit | Description |
---|---|
% | percentage, relative to the same property of the parent element |
em | relative to font size of the element |
rem | relative to font size of the root element |
ch | relative to width of the "0" (ZERO, U+0030) glyph in the element's font |
ex | relative to x-height of the font |
Physical units (e.g. cm, mm, in, pc, and pt)
should only be used for print style sheets,
while pixels (px) should only be used for the screen.
While there are consistent conversions among all of these
absolute length units,
depending on the device, CSS units can actually mean different things.
For example, while 1cm
in CSS should print as one physical centimeter,
there's no guarantee that 1cm
in CSS results in one physical centimeter
on the screen.
Unit | Description | cm | mm | in | pc | pt | px |
---|---|---|---|---|---|---|---|
cm | centimeter | 1cm |
10mm |
||||
mm | millimeter | 1/10cm |
1mm |
||||
in | inch | 2.54cm |
25.4mm |
1in |
6pc |
72pt |
96px |
pc | pica | 1/6in |
1pc |
12pt |
16px |
||
pt | point | 1/72in |
1/12pc |
1pt |
4/3px |
||
px | pixel | 1/96in |
1/16pc |
3/4pt |
1px |
Viewport-percentage length units should be used with caution, as there is still some lingering bugs with their implementation.
Unit | Description |
---|---|
vw | relative to 1% of viewport's width |
vh | relative to 1% of viewport's height |
vmin | relative to 1% of viewport's smaller dimension |
vmax | relative to 1% of viewport's larger dimension |
Assume the root font size is 16px
but don't require it to be so. Either declare the font size as 100%
or don't declare the font-size
property at all.
html {
font-size: 100%;
}
Most likely use px
, as most of the time, the border shouldn't need to scale.
.Component {
border-bottom: 1px solid #ccc;
}
Font-size should only be applied at the lowest possible child elements, in order to minimize its cascading impact on relative units. While both of the following two examples are essentially equivalent, only the first is recommended.
DO:
.Component {
}
.Component-heading {
font-size: 1.2em;
}
.Component-body {
font-size: 0.9em;
}
DO NOT:
.Component {
font-size: 1.2em;
}
.Component-heading {
font-size: 1em;
}
.Component-body {
font-size: 0.75em;
}
In order to ensure consistent use of whitespace throughout the application,
given a component could be used in a variety of contexts,
it may be best to use rem
for margin and padding than em
.
.Component {
margin: 1rem 0;
padding: 1rem;
}
Only use em
within media query definitions, never pixels.
Until there's wider rem
support within media queries,
rem
should be avoided in media queries as well.
@media (min-width: 20em) {
.Component {
background-color: blue;
}
}
Relative length units are dependent upon the length of another element.
Using relative units can be useful for creating flexible layouts that display well in different screen sizes.
-
em
andrem
are defined by the font size of the parent element and the root element. -
em
inherits font size from its parent -
rem
inherits font size from the root element
More on CSS Units: CSS Absolute & Relative Units
Here is the example that covers these properties and the box model here in CodePen.
Hit 'Edit on CodePen' to follow along. 👍
Top comments (0)