CSS Box Model
- Summary
-
Discussion
- Which are the essential elements of the CSS Box Model?
- How is margin different from padding?
- How are width and height of a box determined?
- How do I use display CSS property?
- What are some variants of the box model?
- What is margin collapsing?
- Could you share some tips for working with CSS Box Model?
- Which are some CSS modules that are relevant to the CSS Box Model?
- Milestones
- Sample Code
- References
- Further Reading
- Article Stats
- Cite As
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.
Discussion
-
Which are the essential elements of the CSS Box Model? 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? 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? Total width of an element is calculated as
margin-right + border-right + padding-right + width + padding-left + border-left + margin-left
, wherewidth
is the box width. The calculation for height is similar, using top and bottom rather than right and left.If element has CSS declaration
box-sizing: border-box;
then total width changes tomargin-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%
andheight: 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 valuesvisible
,hidden
,scroll
andauto
. -
How do I use display
CSS property?CSS
display
property is important since it affects how the box is rendered. Common values fordisplay
include:none
: Element is not displayed. Unlikevisibility:hidden
that hides an element but leaves an empty space,display:none
leaves no empty space.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 are some variants of the box model? While the box model is often displayed as inline, inline-block or block, there are other variants. A specialization of the box model is
display:table
. This obeys block flow. Cells flow into rows and columns. Table box has margins but not padding. Cells have padding but not margins. Propertiesborder-collapse
andtable-layout
are applicable.Another variation is
display:list-item
, equivalent to using<li>
tag. Propertylist-style-type
is applicable.It's possible to take out an element from normal flow and put it in a separate layer. This is achieved with property
position
. Common values includeabsolute
(relative to closest ancestor),relative
(relative to itself in normal flow) andfixed
(relative to viewport, unaffected by scrolling). Such boxes don't affect the position of other boxes. These boxes can overlap freely. Propertyz-index
controls how they overlap, with larger values coming on top.Another way to take out an element from normal flow is with
float
. Common values includeleft
,right
,inline-start
andinline-end
. Unlikeposition
, boxes affect adjacent content in the flow. For example, a box floated left will indent adjacent content to the right. -
What is margin collapsing? 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, usedisplay: table-cell; vertical-align: middle;
for the element anddisplay: 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
withrgba
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 asheight: 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 valuescontent-box
(default) andborder-box
.The box model relates to only one element. When designing layout, the use of CSS properties
margin
,display
andfloat
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.
Milestones
1996
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.
2001
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;
.
2002
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.
2002
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.
Sample Code
References
- Ambriz, Juan Carlos Arias. 2019. "10 Practical CSS Tricks every developer should know." Blog, Flexiple, August 20. Accessed 2020-10-11.
- Atkins, Tab (ed). 2019. "CSS Values and Units Module Level 4." W3C Working Draft, January 31. Accessed 2020-10-13.
- Atkins, Tab, and Elika J. Etemad, (eds). 2019. "CSS Intrinsic & Extrinsic Sizing Module Level 3." W3C Working Draft, May 22. Accessed 2020-10-11.
- 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.
- 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.
- Bos, Bert. 2016. "20 Years of CSS." W3C, December 17. Accessed 2020-10-13.
- 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.
- Bowers, Michael, Dionysios Synodinos, and Victor Sumner. 2011. "Pro HTML5 and CSS3 Design Patterns." Apress. doi: 10.1007/978-1-4302-3781-5. Accessed 2020-12-09.
- Coyier, Chris. 2017. "The CSS Box Model." CSS-Tricks, January 28. Accessed 2020-10-11.
- Etemad, Elika J, (ed). 2020a. "CSS Box Model Module Level 3." W3C Working Draft, April 21. Accessed 2020-10-11.
- Etemad, Elika J, (ed). 2020b. "CSS Box Model Module Level 4." W3C First Public Working Draft, April 21. Accessed 2020-10-11.
- Etemad, Elika J., and Tab Atkins, (eds). 2020. "CSS Box Alignment Module Level 3." W3C Working Draft, April 21. Accessed 2020-10-11.
- George, James. 2016. "20 Essential CSS Tricks Every Designer Should Know." Web Designer Depot, October 28. Accessed 2020-10-11.
- Howe, Shay. 2014. "Opening the Box Model." Lesson 4 in: Learn to Code HTML & CSS. Accessed 2020-10-11.
- James, Oliver. 2020. "Advanced Positioning." No. 9 of HTML & CSS Is Hard, Interneting is Hard. Accessed 2020-12-09.
- Johansson, Roger. 2006. "Internet Explorer and the CSS box model." 456 Berea Street, December 21. Accessed 2020-10-13.
- Laxmi, Asha. 2017. "CSS Inheritance: An Introduction." Sitepoint, August 7. Accessed 2020-10-13.
- 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.
- 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.
- MDN Web Docs. 2019a. "Introduction to the CSS basic box model." MDN Web Docs, Mozilla, September 25. Accessed 2020-10-11.
- MDN Web Docs. 2019b. "Mastering margin collapsing." MDN Web Docs, Mozilla, July 16. Accessed 2020-10-11.
- MDN Web Docs. 2020a. "The box model." MDN Web Docs, Mozilla, May 26. Accessed 2020-10-11.
- MDN Web Docs. 2020b. "box-sizing." MDN Web Docs, Mozilla, July 29. Accessed 2020-10-11.
- MDN Web Docs. 2020c. "overflow." MDN Web Docs, Mozilla, September 28. Accessed 2020-10-13.
- MDN Web Docs. 2020d. "position." MDN Web Docs, Mozilla, November 28. Accessed 2020-12-09.
- MDN Web Docs. 2020e. "float." MDN Web Docs, Mozilla, October 5. Accessed 2020-12-09.
- Manisha. 2019. "The Box Model." Hackernoon, February 4. Accessed 2020-10-13.
- Mellul, David. 2018. "4 CSS tricks I’ve learnt the hard way." ITNEXT, on Medium, June 8. Accessed 2020-10-11.
- Microsoft Docs. 2016. "Margin and Padding." Xamarin, Microsoft Docs, April 27. Accessed 2020-10-11.
- Ming, Samantha. 2020. "CSS Inline vs Inline-Block vs Block." July 18. Accessed 2020-10-13.
- Mossesgeld, Rico. 2017. "Eight CSS Tips for Advanced Layouts and Effects." Toptal, June 9. Accessed 2020-10-11.
- Rollins, Nick. 2018. "Padding vs Margin: The Definitive Guide." UX Engineer, October 8. Accessed 2020-10-11.
- Seifi, Joe. 2008. "Understanding & Taming Collapsing Margins in CSS." July 18. Accessed 2020-10-13.
- Surma. 2019. "CSS Grid – Table layout is back. Be there and be square." Google Developers, January 14. Accessed 2020-10-13.
- W3C. 2020. "CSS Box Model Module Level 3 Publication History." W3C. Accessed 2020-10-11.
- Wikipedia. 2020a. "CSS box model." Wikipedia, July 31. Accessed 2020-10-11.
- Wikipedia. 2020b. "Internet Explorer 6." Wikipedia, September 27. Accessed 2020-10-11.
- fantasai. 2020. "Incomplete List of Mistakes in the Design of CSS." CSS Working Group Wiki, March 10. Accessed 2020-10-13.
Further Reading
- MDN Web Docs. 2020a. "The box model." MDN Web Docs, Mozilla, May 26. Accessed 2020-10-11.
- Howe, Shay. 2014. "Opening the Box Model." Lesson 4 in: Learn to Code HTML & CSS. Accessed 2020-10-11.
- Etemad, Elika J, (ed). 2020a. "CSS Box Model Module Level 3." W3C Working Draft, April 21. Accessed 2020-10-11.
- Ming, Samantha. 2020. "CSS Inline vs Inline-Block vs Block." July 18. Accessed 2020-10-13.
- Rollins, Nick. 2018. "Padding vs Margin: The Definitive Guide." UX Engineer, October 8. Accessed 2020-10-11.
- Shepherd, Richard. 2011. "CSS3 Flexible Box Layout: Everything I Wish I Knew When I Started." Smashing Magazine, September 19. Accessed 2020-10-11.