• Every element has an underlying box. Source: Rollins 2018.
    Every element has an underlying box. Source: Rollins 2018.
  • Terminology of CSS box model in the original CSS1 specification. Source: Lie and Bos 1996, sec. 4.1.
    Terminology of CSS box model in the original CSS1 specification. Source: Lie and Bos 1996, sec. 4.1.
  • Before IE6, IE box model didn't conform to W3C box model. Source: Wikipedia 2020a.
    Before IE6, IE box model didn't conform to W3C box model. Source: Wikipedia 2020a.
  • Terminology of CSS box model in the CSS2.1 specification. Source: Bos et al. 2011.
    Terminology of CSS box model in the CSS2.1 specification. Source: Bos et al. 2011.
  • Main elements of the CSS Box Model. Source: Manisha 2019.
    Main elements of the CSS Box Model. Source: Manisha 2019.
  • Illustrating the effect of border, padding and margin. Source: Rollins 2018.
    Illustrating the effect of border, padding and margin. Source: Rollins 2018.
  • With 'box-sizing: border-box', box width and height stay the same. Source: Rollins 2018.
    With 'box-sizing: border-box', box width and height stay the same. Source: Rollins 2018.
  • Effect of inline, inline-block and block on a span element. Source: Adapted from MDN Web Docs 2020a.
    Effect of inline, inline-block and block on a span element. Source: Adapted from MDN Web Docs 2020a.
  • Illustrating margin collapsing. Source: Seifi 2008.
    Illustrating margin collapsing. Source: Seifi 2008.

CSS Box Model

Avatar of user arvindpdmn
arvindpdmn
1203 DevCoins
1 author has contributed to this article
Last updated by arvindpdmn
on 2020-10-14 07:00:36
Created by arvindpdmn
on 2020-10-11 10:58:19

Summary

Every element has an underlying box. Source: Rollins 2018.
Every element has an underlying box. Source: Rollins 2018.

CSS is used to style HTML documents. An important insight is that,

Every element in web design is a rectangular box

No matter how complex the design, at the level of elements, it's just a bunch of boxes. The "box" is an underlying representation. We don't actually see a box unless the designer chooses to show it's border.

Each box has width and height. It's border may or may not be visible. Content resides within the box. Content maybe surrounded by extra spacing. Each box is separated from its neighbouring boxes by defined distances. The CSS Box Model formalizes these ideas in terms of CSS properties and the values they can accept.

Understanding the box model is essential for any web designer or developer. Browsers have built-in tools that enable developers visualize these boxes during development and debugging.

Milestones

Dec
1996
Terminology of CSS box model in the original CSS1 specification. Source: Lie and Bos 1996, sec. 4.1.

W3C publishes Cascading Style Sheets, level 1, simply called CSS1, to express styling of HTML documents. It borrows from desktop publishing terminology. It states that each formatted element is treated as a rectangular box surrounded by optional padding, border and margin areas. Top and bottom margins can collapse.

Aug
2001
Before IE6, IE box model didn't conform to W3C box model. Source: Wikipedia 2020a.

Microsoft releases Internet Explorer 6, which conforms to the W3C CSS box model. Earlier releases of IE implemented a modified box model that ignored border and padding when computing the total width or height of an element: total width = margin-left + width + margin-right. Effectively, earlier versions of IE reduced content size to make room for border and padding. This behaviour is possible in CSS3 via box-sizing: border-box;.

Aug
2002
Terminology of CSS box model in the CSS2.1 specification. Source: Bos et al. 2011.

First draft of CSS2.1 is published, which is a revision of CSS2 from 1997. CSS2.1 becomes a W3C Recommendation in June 2011. For clarify, it adds the terms border edge and padding edge. Inner edge is also called content edge. Outer edge is also called margin edge. Unlike in CSS1, margin, padding and border properties can be inherited.

Oct
2002

W3C publishes the first working draft of CSS Box Model Module Level 3. Subsequent revisions of this draft appear in 2007, 2018 and 2020. The main change is to bring clarity for vertical writing modes.

Apr
2020

W3C publishes the first public working draft of CSS Box Model Module Level 4. Property margin-trim is introduced with three possible values: none, in-flow and all. Often we include margins between siblings but we wish to avoid margins before/after first/last sibling since they border the parent container. This property when used on an element trims margins of children that border the container's edges.

Discussion

  • Which are the essential elements of the CSS Box Model?
    Main elements of the CSS Box Model. Source: Manisha 2019.
    Main elements of the CSS Box Model. Source: Manisha 2019.

    Suppose there's a text element <div>Hello World!</div>. Let's assume that content begins at the left edge of the box and flows to the right. Content "Hello World!" is placed within the box. The box has a size defined by height and width. These dimensions are calculated from the size of the parent element, size of the content or defined explicitly for the element.

    The edge of the box is called the border. Border can be invisible, thin (few pixels) or thick (many pixels). It can be a solid line, a dotted line, and so on. It can be in a chosen colour.

    If we include extra spacing within the box along its edges, this is called padding. An element usually appears next to other elements (parent or siblings). The separation from one element to the next is defined by margin.

    Border, padding and margin can all have different values for each side of the box. Some border properties are inherited by default. Padding and margin are not inherited by default unless declared explicitly.

  • How is margin different from padding?
    Illustrating the effect of border, padding and margin. Source: Rollins 2018.
    Illustrating the effect of border, padding and margin. Source: Rollins 2018.

    Margin occurs outside the box to separate elements. Padding occurs within the box to add spacing between box edge and content. To put it differently, margin controls rendering of an element and its neighbours, whereas padding controls rendering of an element and its children.

    Because padding area is within the box, CSS properties applied to the box affect the padding area as well. For example, if the element has green background colour, the padding area will have green background but the margin background colour is not affected. In fact, the margin background colour will be determined by that of the parent element.

    Non-zero padding increases the overall dimensions of the element. Margin adds spacing outside the box. Adding a margin is like shifting the entire box whereas adding padding is like shifting the content within the box.

    Padding can't be negative but margin can be negative. With a negative margin, an element can "jump out" of its parent or overlap with another element.

  • How are width and height of a box determined?
    With 'box-sizing: border-box', box width and height stay the same. Source: Rollins 2018.
    With 'box-sizing: border-box', box width and height stay the same. Source: Rollins 2018.

    Total width of an element is calculated as margin-right + border-right + padding-right + width + padding-left + border-left + margin-left, where width is the box width. The calculation for height is similar, using top and bottom rather than right and left.

    If the element has CSS declaration box-sizing: border-box; then total width changes to margin-right + width + margin-left. Padding and border sizes don't affect total box dimensions. Content area shrinks.

    Box width and height can be specified in CSS, for example width: 50% and height: 100px. Values are either absolute (pixels, points, inches, etc) or relative (percentage of parent, percentage of viewport, fraction of font size, etc).

    If width and height are not specified, they're determined by the content. For block elements, width is 100% of the parent by default. It's better to specify them if the element size is essential to the overall layout.

    If width and height are specified, but if the content requires more space than available, rendering behaviour is controlled by overflow property, which can take values visible, hidden, scroll and auto.

  • How do I use display CSS property?
    Effect of inline, inline-block and block on a span element. Source: Adapted from MDN Web Docs 2020a.
    Effect of inline, inline-block and block on a span element. Source: Adapted from MDN Web Docs 2020a.

    CSS display property is important since it affects how the box is rendered. Common values for display include:

    • none: Element is not displayed.
    • inline: Element is displayed on the same line as adjacent elements. Height and width are determined by the content and can't be set explicitly. This is the default for span, a, img, em, strong, i, small, etc. Only left and right margins are applicable. Padding is applicable on all four sides but top and bottom padding may bleed into lines above and below.
    • inline-block: Similar to inline but height and width can be set. Box model properties are applicable here. The problem of vertical bleeding seen in inline is solved with inline-block.
    • block: Element is displayed on a separate line and takes up 100% width of the parent. This is the default for div, h1, p, li, section, etc.
  • What is margin collapsing?
    Illustrating margin collapsing. Source: Seifi 2008.
    Illustrating margin collapsing. Source: Seifi 2008.

    Consider two sibling elements, each set with margin of 20px. With inline-block display, elements are separated by 40px since margins are additive. However, if we change to block display, adjacent margins collapse into a single margin of 20px. Note that padding is not subject to collapsing.

    Collapsed margin size is the largest of the individual margins. If there are negative margins involved, collapsed margin is the most positive or the most negative margin. When all margins are negative, it collapses to the most negative margin.

    Margin collapsing happens for adjacent siblings; when there's no content separating parent and descendants; for empty blocks; and more complex scenarios when these conditions are combined.

    It's been said that margin collapsing is one of design mistakes in CSS. There are rules to when margins can collapse but there are also many exceptions to the rules. This makes it difficult and confusing for a web developer. One way to overcome this complexity is to consistently set right and bottom margins and zero out top and left margins, such as, margin: 0 40px 40px 0;.

  • Could you share some tips for working with CSS Box Model?

    Here are some useful tips for beginners:

    • If width is not set, it defaults to 100%. Padding and border will push inwards. If width is explicitly set to 100%, padding and border will push outwards as normal.
    • Left and right margins can take the value auto to center the element horizontally. This is not possible with padding. For centering vertically, use display: table-cell; vertical-align: middle; for the element and display: table for its parent. CSS Flexbox offers a simpler approach.
    • To inherit only right margin and fix other margins, use margin: 10px 0 20px 15px; margin-right: inherit;.
    • Use box-shadow with rgba values to improve design quality.
    • To fit images to a certain width without exceeding the original width or distorting it, use max-width: 100%; height: auto;.
    • Element height depends on content height and remains unaffected by height: 100%. Height can be controlled using padding, such as height: 0; padding-bottom: 100%;.
    • Columns with different content can be made to have the same height using large padding and negative margin. Suppose bottom padding is 10px, we can use margin-bottom: -99999px; padding-bottom: 100009px;. CSS Flexbox offers a simpler approach.
  • Which are some CSS modules that are relevant to the CSS Box Model?

    CSS Intrinsic & Extrinsic Sizing is the module that talks about box-sizing property that's directly relevant to the box model. This property can take values content-box (default) and border-box.

    The box model relates to only one element. When designing layout, the use of CSS properties margin, display and float are helpful. However, there are CSS3 modules that work with the box model and simplify layout design:

    • CSS Flexbox: Children of a container can adjust their sizes to fit the container. This can happen in one dimension, either horizontally or vertically. By nesting, two-dimensional layouts can be achieved.
    • CSS Grid: A two-dimensional layout system that's easier to work with than Flexbox. This is a modern alternative to the <table> tag from the early days of HTML.
    • CSS Multi-column: Content flows into multiple columns with gap and rule between columns.

    Another useful module is CSS Box Alignment that works with layout modules to align elements within their container parent. Elements can be positioned within the parent, spaces between elements can be controlled, and containers can be configured to align the elements within.

Sample Code

  • /* Source: https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/The_box_model
       Accessed 2020-10-13
    */
     
    /* Using shorthand properties */
    .box {
      width: 350px;
      height: 150px;
      margin: 10px;
      padding: 25px;
      border: 5px solid black;
    }
     
    /* Using longhand properties */
    .box {
      margin-top: -40px;
      margin-right: 30px;
      margin-bottom: 40px;
      margin-left: 4em;
     
      padding-top: 0;
      padding-right: 30px;
      padding-bottom: 40px;
      padding-left: 4em;
     
      border: 1px solid #333333;
      border-top-style: dotted;
      border-right-width: 20px;
      border-bottom-color: hotpink;
    }

References

  1. Ambriz, Juan Carlos Arias. 2019. "10 Practical CSS Tricks every developer should know." Blog, Flexiple, August 20. Accessed 2020-10-11.
  2. Atkins, Tab (ed). 2019. "CSS Values and Units Module Level 4." W3C Working Draft, January 31. Accessed 2020-10-13.
  3. Atkins, Tab, and Elika J. Etemad, (eds). 2019. "CSS Intrinsic & Extrinsic Sizing Module Level 3." W3C Working Draft, May 22. Accessed 2020-10-11.
  4. Atkins, Tab, Elika J. Etemad, and Rossen Atanassov, (eds). 2018. "CSS Flexible Box Layout Module Level 1." W3C Candidate Recommendation, November 19. Accessed 2020-10-11.
  5. Atkins, Tab, Elika J. Etemad, Rossen Atanassov, and Oriol Brufau, (eds). 2020. "CSS Grid Layout Module Level 1." W3C Candidate Recommendation, August 18. Accessed 2020-10-13.
  6. Bos, Bert. 2016. "20 Years of CSS." W3C, December 17. Accessed 2020-10-13.
  7. Bos, Bert, Tantek Çelik, Ian Hickson, and Håkon Wium Lie, (eds). 2011. "Chapter 8: Box model." In: Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification, W3C Recommendation, June 7. Updated 2016-04-12. Accessed 2020-10-11.
  8. Coyier, Chris. 2017. "The CSS Box Model." CSS-Tricks, January 28. Accessed 2020-10-11.
  9. Etemad, Elika J, (ed). 2020a. "CSS Box Model Module Level 3." W3C Working Draft, April 21. Accessed 2020-10-11.
  10. Etemad, Elika J, (ed). 2020b. "CSS Box Model Module Level 4." W3C First Public Working Draft, April 21. Accessed 2020-10-11.
  11. Etemad, Elika J., and Tab Atkins, (eds). 2020. "CSS Box Alignment Module Level 3." W3C Working Draft, April 21. Accessed 2020-10-11.
  12. George, James. 2016. "20 Essential CSS Tricks Every Designer Should Know." Web Designer Depot, October 28. Accessed 2020-10-11.
  13. Howe, Shay. 2014. "Opening the Box Model." Lesson 4 in: Learn to Code HTML & CSS. Accessed 2020-10-11.
  14. Johansson, Roger. 2006. "Internet Explorer and the CSS box model." 456 Berea Street, December 21. Accessed 2020-10-13.
  15. Laxmi, Asha. 2017. "CSS Inheritance: An Introduction." Sitepoint, August 7. Accessed 2020-10-13.
  16. Lie, Håkon Wium and Bert Bos. 1996. "Cascading Style Sheets, level 1." W3C Recommendation, December 17. Updated 2018-09-13. Accessed 2020-10-11.
  17. Lie, Håkon Wium, Florian Rivoal, and Rachel Andrew. 2019. "CSS Multi-column Layout Module Level 1." W3C Working Draft, October 15. Accessed 2020-10-14.
  18. MDN Web Docs. 2019a. "Introduction to the CSS basic box model." MDN Web Docs, Mozilla, September 25. Accessed 2020-10-11.
  19. MDN Web Docs. 2019b. "Mastering margin collapsing." MDN Web Docs, Mozilla, July 16. Accessed 2020-10-11.
  20. MDN Web Docs. 2020a. "The box model." MDN Web Docs, Mozilla, May 26. Accessed 2020-10-11.
  21. MDN Web Docs. 2020b. "box-sizing." MDN Web Docs, Mozilla, July 29. Accessed 2020-10-11.
  22. MDN Web Docs. 2020c. "overflow." MDN Web Docs, Mozilla, September 28. Accessed 2020-10-13.
  23. Manisha. 2019. "The Box Model." Hackernoon, February 4. Accessed 2020-10-13.
  24. Mellul, David. 2018. "4 CSS tricks I’ve learnt the hard way." ITNEXT, on Medium, June 8. Accessed 2020-10-11.
  25. Microsoft Docs. 2016. "Margin and Padding." Xamarin, Microsoft Docs, April 27. Accessed 2020-10-11.
  26. Ming, Samantha. 2020. "CSS Inline vs Inline-Block vs Block." July 18. Accessed 2020-10-13.
  27. Mossesgeld, Rico. 2017. "Eight CSS Tips for Advanced Layouts and Effects." Toptal, June 9. Accessed 2020-10-11.
  28. Rollins, Nick. 2018. "Padding vs Margin: The Definitive Guide." UX Engineer, October 8. Accessed 2020-10-11.
  29. Seifi, Joe. 2008. "Understanding & Taming Collapsing Margins in CSS." July 18. Accessed 2020-10-13.
  30. Surma. 2019. "CSS Grid – Table layout is back. Be there and be square." Google Developers, January 14. Accessed 2020-10-13.
  31. W3C. 2020. "CSS Box Model Module Level 3 Publication History." W3C. Accessed 2020-10-11.
  32. Wikipedia. 2020a. "CSS box model." Wikipedia, July 31. Accessed 2020-10-11.
  33. Wikipedia. 2020b. "Internet Explorer 6." Wikipedia, September 27. Accessed 2020-10-11.
  34. fantasai. 2020. "Incomplete List of Mistakes in the Design of CSS." CSS Working Group Wiki, March 10. Accessed 2020-10-13.

Milestones

Dec
1996
Terminology of CSS box model in the original CSS1 specification. Source: Lie and Bos 1996, sec. 4.1.

W3C publishes Cascading Style Sheets, level 1, simply called CSS1, to express styling of HTML documents. It borrows from desktop publishing terminology. It states that each formatted element is treated as a rectangular box surrounded by optional padding, border and margin areas. Top and bottom margins can collapse.

Aug
2001
Before IE6, IE box model didn't conform to W3C box model. Source: Wikipedia 2020a.

Microsoft releases Internet Explorer 6, which conforms to the W3C CSS box model. Earlier releases of IE implemented a modified box model that ignored border and padding when computing the total width or height of an element: total width = margin-left + width + margin-right. Effectively, earlier versions of IE reduced content size to make room for border and padding. This behaviour is possible in CSS3 via box-sizing: border-box;.

Aug
2002
Terminology of CSS box model in the CSS2.1 specification. Source: Bos et al. 2011.

First draft of CSS2.1 is published, which is a revision of CSS2 from 1997. CSS2.1 becomes a W3C Recommendation in June 2011. For clarify, it adds the terms border edge and padding edge. Inner edge is also called content edge. Outer edge is also called margin edge. Unlike in CSS1, margin, padding and border properties can be inherited.

Oct
2002

W3C publishes the first working draft of CSS Box Model Module Level 3. Subsequent revisions of this draft appear in 2007, 2018 and 2020. The main change is to bring clarity for vertical writing modes.

Apr
2020

W3C publishes the first public working draft of CSS Box Model Module Level 4. Property margin-trim is introduced with three possible values: none, in-flow and all. Often we include margins between siblings but we wish to avoid margins before/after first/last sibling since they border the parent container. This property when used on an element trims margins of children that border the container's edges.

Tags

See Also

Further Reading

  1. MDN Web Docs. 2020a. "The box model." MDN Web Docs, Mozilla, May 26. Accessed 2020-10-11.
  2. Howe, Shay. 2014. "Opening the Box Model." Lesson 4 in: Learn to Code HTML & CSS. Accessed 2020-10-11.
  3. Etemad, Elika J, (ed). 2020a. "CSS Box Model Module Level 3." W3C Working Draft, April 21. Accessed 2020-10-11.
  4. Ming, Samantha. 2020. "CSS Inline vs Inline-Block vs Block." July 18. Accessed 2020-10-13.
  5. Rollins, Nick. 2018. "Padding vs Margin: The Definitive Guide." UX Engineer, October 8. Accessed 2020-10-11.
  6. Shepherd, Richard. 2011. "CSS3 Flexible Box Layout: Everything I Wish I Knew When I Started." Smashing Magazine, September 19. Accessed 2020-10-11.

Article Stats

Author-wise Stats for Article Edits

Author
No. of Edits
No. of Chats
DevCoins
2
0
1203
1793
Words
0
Chats
2
Edits
0
Likes
112
Hits

Cite As

Devopedia. 2020. "CSS Box Model." Version 2, October 14. Accessed 2020-10-21. https://devopedia.org/css-box-model