Display and Box Model
display Property
- In the HTML articles on this website tags are divided into block-level and inline, which reflects how the built-in browser stylesheets render them.
- CSS allows us to change how the elements are rendered, overriding the browser's default styling.
- This is especially relevant to the box model, which is the other focus of this article, because the properties that are involved with the box model respond best to elements rendered as block or inline-block.
| Property | Usage and Effect | Values | Additional Notes |
|---|---|---|---|
| display | Determines how an element is rendered. |
|
This property is not inherited (inner tags will not pick it up from outer tags), although instructing a parent to display: none will cause all children and descendants to be removed from rendering.
It is more accessible to use data table markup and appropriate attributes if you are working with tabular data, compared to a bunch of divs displayed as a table. |
display Example
See the Pen display by Jason Withrow (@jwithrow) on CodePen.
What is the CSS Box Model?
- The CSS Box Model is a key aspect of CSS and is at the heart of how CSS formats and presents information.
- First, every element contains content (such as images and/or text) that is located within a defined width and/or height content box.
- Wrapping around the content box is a padding box (invisible to the user; background colors go through this padding by default, although that can be changed), then a border box, and finally a margin box that is outside the border and that is also invisible to the user.
- The border box is the divide between the inside of an element and the outside of the element.
- Inside the border box are the padding box and content box
- Outside is the margin box
- Any horizontal padding is in addition to the width set for the content box; any vertical padding is in addition to the height set for the content box.
- Important Considerations:
- Using the box model (specifying
width,height,padding,border, and/ormargin) always works best on block-level elements or elements instructed to display as block or inline-block. - For inline elements, or elements displayed as inline,
width,height, andmarginare ignored,bordercan render oddly, andpaddingcan even run into some issues in old browsers. - Many block-level elements have a default
marginon the top and bottom greater than0; some (such as blockquotes and lists) even have them on the left and right sides. - The
paddingandborder, on the other hand, generally default to0for the majority of elements. - To disable a default
paddingormarginit can be set to0, which does not need a sizing unit (such as px) specified.
- Using the box model (specifying
border-related Properties
- When specifying border widths the keywords (
thin,medium,thick) are not ideal because they are not precise. - Specifying a border width in pixels is generally recommended for precise control, although the borders will become quite thin at high screen resolutions.
- When the
emandremsizing unit is used for border widths and for sizing other parts of the box model (in other words, not for text sizing) the resulting space is calculated differently.- A
widthof1emcreates an em square, which is a square shape of equal height and width, with the height of the square equal to the height of the font for the element that has the border (so 12px font size for the element and 1em border width means a 12px wide border). - A
widthof1remcreates an rem square, which is a square shape of equal height and width, with the height of the square equal to the height of the font for the root element (generally 16px, so the border is 16px wide and 16px tall).
- A
| Property | Usage and Effect | Values | Recommendations |
|---|---|---|---|
| border-top-color | Determines the color for the top border of an element. |
|
The border-color shorthand property is usually more efficient. |
| border-right-color | Determines the color for the right border of an element. |
|
The border-color shorthand property is usually more efficient. |
| border-bottom-color | Determines the color for the bottom border of an element. |
|
The border-color shorthand property is usually more efficient. |
| border-left-color | Determines the color for the left border of an element. |
|
The border-color shorthand property is usually more efficient. |
| border-color | Shorthand for border-top-color, border-right-color, border-bottom-color, and border-left-color properties. |
Important details about the number of values provided:
|
If possible, use the border shorthand (or a variant such as border-top) rather than this property; they are more efficient.
|
| border-top-style | Determines the style for the top border of an element. |
|
The border-style shorthand property is usually more efficient. |
| border-right-style | Determines the style for the right border of an element. |
|
The border-style shorthand property is usually more efficient. |
| border-bottom-style | Determines the style for the bottom border of an element. |
|
The border-style shorthand property is usually more efficient. |
| border-left-style | Determines the style for the left border of an element. |
|
The border-style shorthand property is usually more efficient. |
| border-style | Shorthand for border-top-style, border-right-style, border-bottom-style, and border-left-style properties. |
Important details about the number of values provided:
|
Some border styles need to have a width of a few pixels before they can render properly. For example, a 1px double border will render as a solid line because at least 3 pixels of width are necessary for proper rendering.
Browsers differ in how they color the tops and bottoms of borders. Extensive cross-browser, cross-platform testing is advised. If possible, use the |
| border-top-width | Determines the width for the top border of an element. |
|
The border-width shorthand property is usually more efficient. |
| border-right-width | Determines the width for the right border of an element. |
|
The border-width shorthand property is usually more efficient. |
| border-bottom-width | Determines the width for the bottom border of an element. |
|
The border-width shorthand property is usually more efficient. |
| border-left-width | Determines the width for the left border of an element. |
|
The border-width shorthand property is usually more efficient. |
| border-width | Shorthand for border-top-width, border-right-width, border-bottom-width, and border-left-width properties. |
Important details about the number of values provided:
|
The border-width shorthand property is usually more efficient than specifying individual border widths for various sides. |
| border-top | Combines the other border-related properties into one sequence that applies to the top border of an element. |
The properties are: border-width border-style border-color
Note that each property can only be given a single value; sequencing is up to you. |
Use the border shorthand if all sides need to be consistent. |
| border-right | Combines the other border-related properties into one sequence that applies to the right border of an element. |
The properties are: border-width border-style border-color
Note that each property can only be given a single value; sequencing is up to you. |
Use the border shorthand if all sides need to be consistent. |
| border-bottom | Combines the other border-related properties into one sequence that applies to the bottom border of an element. |
The properties are: border-width border-style border-color
Note that each property can only be given a single value; sequencing is up to you. |
Use the border shorthand if all sides need to be consistent.
This can be used for anchors (links) as an alternative to |
| border-left | Combines the other border-related properties into one sequence that applies to the left border of an element. |
The properties are: border-width border-style border-color
Note that each property can only be given a single value; sequencing is up to you. |
Use the border shorthand if all sides need to be consistent. |
| border | Combines the other border-related properties into one sequence that applies to all border sides of an element. |
The properties are: border-width border-style border-color
Note that each property can only be given a single value; sequencing is up to you. |
Highly recommended (it is the shorthand of shorthands when it comes to border appearance, combining the border-top, border-right, border-bottom, and border-left shorthand properties). |
border Example
See the Pen Box Model: Borders by Jason Withrow (@jwithrow) on CodePen.
padding-related Properties
- Unlike margins, which sometimes collapse into each other (in cases of vertical overlap), padding values from different elements always add together (referred to as concatenation).
| Property | Usage and Effect | Values | Recommendations |
|---|---|---|---|
| padding-top | Determines the padding space at the top of an element. | Number and unit (e.g., px, em, rem, %) | Use the padding shorthand if multiple sides need to be consistent. |
| padding-right | Determines the padding space on the right side of an element. | Number and unit (e.g., px, em, rem, %) | Use the padding shorthand if multiple sides need to be consistent. |
| padding-bottom | Determines the padding space at the bottom of an element. | Number and unit (e.g., px, em, rem, %) | Use the padding shorthand if multiple sides need to be consistent. |
| padding-left | Determines the padding space on the left side of an element. | Number and unit (e.g., px, em, rem, %) | Use the padding shorthand if multiple sides need to be consistent. |
| padding | Shorthand for padding-top, padding-right, padding-bottom, and padding-left properties. |
Number and unit (e.g., px, em, rem, %)
Specifying one or more values:
|
Highly recommended; this shorthand property is usually more efficient than specifying individual paddings for various sides. |
padding Example
See the Pen Box Model: Padding by Jason Withrow (@jwithrow) on CodePen.
margin-related Properties
- It is possible to supply a negative margin value to an element (e.g.,
margin-top: -5px;) in order to shift the element's location on the page (a -5px margin-top would shift the element up the page by 5 pixels). - Top and bottom margins of elements following each other collapse rather than combine, so that only the larger margin value is used.
- Some other margin considerations occur when nesting elements that all have margins; the top and bottom margins will collapse if there is no border or padding to separate the elements.
- An empty element (no content) with top and bottom margins will also have those vertical margins collapse into each other, if no padding or border is specified, so that the vertical space is equal to one of those margins. For example, an empty paragraph will collapse to a single line of space, because the top and bottom margins default to 1em, so after collapsing 1em of vertical space remains. If this is immediately adjacent (following or preceding) an element with vertical margins on the appropriate side, it will then collapse into that element's margin.
- Horizontal (left / right) margins concatenate (add together).
| Property | Usage and Effect | Values | Recommendations |
|---|---|---|---|
| margin-top | Determines the margin space above the element. | Number and unit (e.g., px, em, rem, %) | Keep in mind that many block-level elements have default top margins greater than 0.
Overlapping vertical margins (top and bottom margins) from separate elements collapse into each other. Use the |
| margin-right | Determines the margin space to the right of an element. | Number and unit (e.g., px, em, rem, %) | Overlapping horizontal margins (right and left margins) from separate elements will concatenate (add together).
Use the |
| margin-bottom | Determines the margin space below the element. | Number and unit (e.g., px, em, rem, %) | Keep in mind that many block-level elements have default bottom margins greater than 0.
Overlapping vertical margins (top and bottom margins) from separate elements collapse into each other. Use the |
| margin-left | Determines the margin space to the left of an element. | Number and unit (e.g., px, em, rem, %) | Overlapping horizontal margins (right and left margins) from separate elements will concatenate (add together).
Use the |
| margin | Shorthand for margin-top, margin-right, margin-bottom, and margin-left properties. |
Number and unit (e.g., px, em, rem, %)
Specifying one or more values:
|
Highly recommended; the margin shorthand property is usually more efficient than specifying individual margins for various sides. |
margin Example
See the Pen Box Model: Margin by Jason Withrow (@jwithrow) on CodePen.
width Property
| Property | Usage and Effect | Values | Recommendations |
|---|---|---|---|
| width | Determines the horizontal space for whatever is inside the element's innermost box (the content box).
The total horizontal space occupied by the element is the sum of the |
Number and unit (e.g., px, em, rem, %) or auto (the default) |
This is very useful, since by default block-level elements fill up their parent element and that is not always what you want.
Refrain from setting a width on the body element, as that should fill the browser window. |
width Example
See the Pen Untitled by Jason Withrow (@jwithrow) on CodePen.
height Property
| Property | Usage and Effect | Values | Recommendations |
|---|---|---|---|
| height |
Determines the vertical space for whatever is inside the element's innermost box (the content box).
The total vertical space occupied by the element is the sum of the |
Number and unit (e.g., px, em, rem, %) or auto (the default) |
I seldom specify heights, because browsers do not enforce that as a maximum height. Specifying a height sets that as the minimum height, but sufficient amounts of content can then exceed that height (see the sections of this article related to overflow and minimum heights / widths).
As a result, I usually prefer to let content set the height of an element, especially if the element contains text. Only in the case of an element with no text content (perhaps just showing a background image) would I set the |
height Example
See the Pen Box Model: Height by Jason Withrow (@jwithrow) on CodePen.
calc() for Length Units
- It is possible for CSS to determine values on-the-fly using
calc(), for examplewidth: calc(100% - 2px). - Addition, subtraction, multiplication, and division are supported. It is even supported in other contexts, such as determining colors, but our focus is on the box model.
- This comes in very handy when you're working with a mixture of percentages and other sizing units, because the percentage is recalculating as the window is resized and the other sizing units (e.g., px, em, rem) are not changing.
Some Box Model Applications
- Disabling the default spacing assigned to
body: Be sure to set bothmarginandpaddingto0(some browsers have a default padding for body greater than0). - Inserting vertical space between list items: Apply to
li; margins work better here than padding. Useful because list items otherwise have no extra spacing between them and the extra space improves readability. - Centering a block-level container on the page (or within a parent element that is wider): Make sure the element has a
widthspecified and then set right and leftmarginvalues toauto, which splits the available horizontal space outside the element between the two sides (thus centering the element). The element needs to be able to accept the width, so it needs to be displayed as block or inline-block. - Turning off the default blue borders around linked images: Setting the
borderfor the image (or, to impact all images, styleimgas the selector) as eithernoneor0will disable the blue border. - Disabling margin space above and below headings (setting it to
0): This standardizes appearance across browsers and regardless of the surrounding markup.
Box Model Applications Example
See the Pen Box Model: Applications by Jason Withrow (@jwithrow) on CodePen.
width and the Body Tag
- Do not set a
widthon the<body>tag. - In practice,
<body>should be considered the browser window, so it is the user's decision how large to make the window, or the window size may be pre-determined by the device being used (e.g., smartphones). - There are a wide variety of browsers in existence, which are used in a wide variety of environments and devices (desktops/laptops, tablets, phones, car infotainment systems, kitchen appliances, watches, etc.).
- Even if modern desktop/laptop browsers seem fine with you assigning a width to body, you're not testing across a wide enough spectrum of browsers and environments, and there will be issues you're not aware of.
- Bottom line: don't assign a width to body. If you need to set a width for a layout, add a container element within body and give that element a width.
Overflow and Resizing
Overflow determines how elements that are able to accept width and height (block-level or displayed as block or inline-block) handle content exceeding those defined boundaries.
| Property | Usage and Effect | Values | Recommendations |
|---|---|---|---|
| overflow | Determines how content is rendered (and how the element responds) when the content exceeds the dimensions of the element. |
|
This property is not inherited (inner tags will not pick it up from outer tags).
When defaulting to This is not a concern if the dimension where scrolling will occur (height or width) is not specified. |
| overflow-x | Determines how content is rendered when the content exceeds the width of the element. |
|
Primarily used to disable the horizontal scrollbar. Sometimes applied to the body element if there is a small horizontal scrollbar appearing that cannot be eliminated through other means. |
| overflow-y | Determines how content is rendered (and how the block-level element responds) when the content exceeds the height of the block-level element. |
|
Primarily used to disable the vertical scrollbar. |
| resize | Allows an element to be resized by the user. The element must have an overflow value set to something other than visible. Resizing can be horizontal, vertical, or both directions. |
|
Browser support varies; be sure to check if your target browsers are supportive of resize. |
overflow Example
See the Pen Overflow by Jason Withrow (@jwithrow) on CodePen.
Minimum and Maximum Widths and Heights
- For these to work properly the element must be block-level or displayed as a block (or inline-block).
- Negative values are not allowed.
| Property | Usage and Effect | Values |
|---|---|---|
| min-height | Determines the minimum height for the element. The element can become taller, as content requires.
In situations where the |
Number and unit (e.g., px, em, %) |
| max-height | Determines the maximum height for the element.
In situations where the In situations where |
Number and unit (e.g., px, em, %) |
| min-width | Determines the minimum width for the element. The element can become wider, as content requires.
In situations where the |
Number and unit (e.g., px, em, %) |
| max-width | Determines the maximum width for the element.
In situations where the In situations where |
Number and unit (e.g., px, em, %) |
box-sizing and Changing the Box Model
- It's safe to say that a number of web developers were unhappy with the box model described elsewhere in this article.
- They didn't like that the width and height properties were applied to the innermost box (the content box) and that padding and border were outside of those dimensions and adding to them.
- They much preferred to have the opposite occur, and have width and height be applied to a different box (such as the border box) so that padding and border-width would be subtracted from the width and height when rendering occurred.
- In essence, they wanted to flip the box model from adding to subtracting, and that's where the box-sizing property comes into play. This property is very commonly used.
| Property | Usage and Effect | Values | Recommendations |
|---|---|---|---|
| box-sizing | Specifies the box model to use (the original box model, where the height and width apply to the content box, or an alternate box model). |
|
Switching to border-box is helpful if you have a container element that is sized as a percentage and it will have fixed-size padding and/or border applied to it (such as 15px padding and 2px border), as those then subtract from the percentage dynamically as the window is resized. |
box-sizing Example
See the Pen box-sizing by Jason Withrow (@jwithrow) on CodePen.