CSS box model explained

Box Model
Long Name:CSS Box Model Module Level 3
Native Name:CSS Box Model Module Level 3
Native Name Lang:en
Status:W3C Candidate Recommendation Snapshot
Version:Level 3
Version Date:[1]
Organization:World Wide Web Consortium
Committee:CSS Working Group
Base Standards:CSS
Domain:CSS

In web development, the CSS box model refers to how HTML elements are modeled in browser engines and how the dimensions of those HTML elements are derived from CSS properties. It is a fundamental concept for the composition of HTML webpages.[2] The guidelines of the box model are described by web standards World Wide Web Consortium (W3C) specifically the CSS Working Group. For much of the late-1990s and early 2000s there had been non-standard compliant implementations of the box model in mainstream browsers. With the advent of CSS2 in 1998, which introduced the box-sizing property, the problem had mostly been resolved.

Specifics

The Cascading Style Sheets (CSS) specification describes how elements of web pages are displayed by graphical browsers. Section 4 of the CSS1 specification defines a "formatting model" that gives block-level elements—such as p and blockquote—a width and height, and three levels of boxes surrounding it: padding, borders, and margins.[3] While the specification never uses the term "box model" explicitly, the term has become widely used by web developers and web browser vendors.

All HTML elements can be considered "boxes", this includes div tag, p tag, or a tag. Each of those boxes has five modifiable dimensions:

According to the CSS1 specification, released by W3C in 1996 and revised in 1999, when a width or height is explicitly specified for any block-level element, it should determine only the width or height of the visible element, with the padding, borders, and margins applied afterward.[4] Before CSS3, this box model was known as W3C box model, in CSS3, it is known as the content-box.

The total width of a box is therefore left-margin + left-border + left-padding + width + right-padding + right-border + right-margin. Similarly, the total height of a box equals top-margin + top-border + top-padding + height + bottom-padding + bottom-border + bottom-margin.

For example, the following CSS code.myClass would specify the box dimensions of each block belonging to 'myClass'. Moreover, each such box will have total height 160px and width 260px.

CSS3 introduced the Internet Explorer box model to the standard, known referred to as border-box.[5]

History

Before HTML 4 and CSS, very few HTML elements supported both border and padding, so the definition of the width and height of an element was not very contentious. However, it varied depending on the element. The HTML width attribute of a table defined the width of the table including its border.[6] On the other hand, the HTML width attribute of an image defined the width of the image itself (inside any border).[7] The only element to support padding in those early days was the table cell. Width for the cell was defined as "the suggested width for a cell content in pixels excluding the cell padding."[8]

In 1996, CSS[9] introduced margin, border and padding for many more elements. It adopted a definition width in relation to content, border, margin and padding similar to that for a table cell.[10] This has since become known as the W3C box model.

At the time, very few browser vendors implemented the W3C box model to the letter. The two major browsers at the time, Netscape 4.0 and Internet Explorer 4.0 both defined width and height as the distance from border to border.[11] This has been referred to as the traditional[12] or the Internet Explorer box model.[13]

Internet Explorer in "quirks mode" includes the content, padding and borders within a specified width or height; this results in a narrower or shorter rendering of a box than would result following the standard behavior.[14]

The Internet Explorer box model behavior was often considered a bug, because of the way in which earlier versions of Internet Explorer handle the box model or sizing of elements in a web page, which differs from the standard way recommended by the W3C for the Cascading Style Sheets language.[15] [16] As of Internet Explorer 6, the browser supports an alternative rendering mode (called the "standards-compliant mode") which solves this discrepancy. However, for backward compatibility reasons, all versions still behave in the usual, non-standard way by default (see quirks mode). Internet Explorer for Mac is not affected by this non-standard behavior.

Workarounds

Internet Explorer versions 6 and onward are not affected by the bug if the page contains certain HTML document type declarations. These versions maintain the buggy behavior when in quirks mode for reasons of backward compatibility.[17] For example, quirks mode is triggered:

Various workarounds have been devised to force Internet Explorer versions 5 and earlier to display Web pages using the W3C box model. These workarounds generally exploit unrelated bugs in Internet Explorer's CSS selector processing in order to hide certain rules from the browser. The best known of these workarounds is the "box model hack" developed by Tantek Çelik, a former Microsoft employee who developed this idea while working on Internet Explorer for the Macintosh. It involves specifying a width declaration for Internet Explorer for Windows, and then overriding it with another width declaration for CSS-compliant browsers. This second declaration is hidden from Internet Explorer for Windows by exploiting other bugs in the way that it parses CSS rules. The implementation of these CSS "hacks" has been further complicated by the public release of Internet Explorer 7, which has had some issues fixed, but not others, causing undesired results in pages using these hacks.[17]

Box model hacks have proven unreliable because they rely on bugs in browsers' CSS support that may be fixed in later versions. For this reason, some Web developers have instead recommended either avoiding specifying both width and padding for the same element or using conditional comment and/or CSS filters to work around the box model bug in older versions of Internet Explorer.[19]

Support for Internet Explorer's box model

Web designer Doug Bowman has said that the original Internet Explorer box model represents a better, more logical approach.[20] Peter-Paul Koch gives the example of a physical box, whose dimensions always refer to the box itself, including potential padding, but never its content.[12] He says that this box model is more useful for graphic designers, who create designs based on the visible width of boxes rather than the width of their content.[21] Bernie Zimmermann says that the Internet Explorer box model is closer to the definition of cell dimensions and padding used in the HTML table model.[22]

The W3C has included a "box-sizing" property in CSS3. When box-sizing: border-box; is specified for an element, any padding or border of the element is drawn inside the specified width and height, "as commonly implemented by legacy HTML user agents".[23] Internet Explorer 8, WebKit browsers such as Apple Safari 5.1+ and Google Chrome, Gecko-based browsers such as Mozilla Firefox 29.0 and later, Opera 7.0 and later, and Konqueror 3.3.2 and later support the CSS3 box-sizing property. Gecko browsers previous than 29.0 support the same functionality using the browser-specific -moz-box-sizing property.[24] border-box is the default box model used in Bootstrap framework.

External links

Notes and References

  1. Web site: CSS Box Model Module Level 3. W3C. CSS Working Group. CSS Working Group. Level 3. 2020-12-22. 2021-04-09. Elika J.. Etemad.
  2. Web site: CSS . MDN Web Docs. 2019-03-23. 30 March 2019.
  3. Web site: Cascading Style Sheets, level 1 § Formatting model . Wium Lie . Håkon . Håkon Wium Lie . Bos . Bert . Bert Bos . 11 January 1999 . . 26 October 2017.
  4. Web site: Cascading Style Sheets, level 1 . Håkon Wium Lie . Håkon Wium Lie . Bert Bos . Bert Bos . 17 December 1996 . . 2006-12-09.
  5. Web site: CSS – box-sizing. Peter-Paul Koch. quirksmode.org. 2013. 30 March 2019.
  6. Web site: The HTML3 Table Model . Raggett . Dave . Dave Raggett . 23 January 1996 . . 26 October 2017. Web site: RFC 1942 HTML Tables . . The default table width is the space between the current left and right margins..
  7. Web site: 13 Objects, Images, and Applets § Width and height . Raggett . Dave . Le Hors . Arnaud . Jacobs . Ian . 24 December 1999 . . 26 October 2017.
  8. Web site: HTML 3.2 Reference Specification . Raggett . Dave . Dave Raggett . 14 January 1997 . . 26 October 2017.
  9. Web site: Cascading Style Sheets, level 1 . Wium Lie . Håkon . Håkon Wium Lie . Bos . Bert . Bert Bos . 17 December 1996 . . 26 October 2017.
  10. Web site: Cascading Style Sheets, level 1 . Wium Lie . Håkon . Håkon Wium Lie . Bos . Bert . Bert Bos . 17 December 1996 . . 26 October 2017.
  11. Web site: Box model tweaking . Koch . Peter-Paul . . 26 October 2017.
  12. Web site: CSS – box-sizing . Koch . Peter-Paul . QuirksMode . 26 October 2017.
  13. Web site: Internet Explorer and the CSS box model . Johansson . Roger . 21 December 2006 . 456 Berea Street . 26 October 2017.
  14. Web site: CSS Enhancements in Internet Explorer 6 . Lance Silver . March 2001 . . . 2007-06-24.
  15. Book: Shafer, Dan . HTML Utopia: Designing Without Tables Using CSS . 2005 . . Melbourne . 0-9579218-2-9 . 124 & Appendix C .
  16. Book: Shea, David . The Zen of CSS Design . Dave Shea (web designer) . Molly E. Holzschlag . Molly E. Holzschlag . 2005 . Peachpit Press . Berkeley . 0-321-30347-4 . dmy-all .
  17. Web site: Cascading Style Sheet Compatibility in Internet Explorer 7 . 2007-06-24 . Markus Mielke . 26 September 2006 . . . "Pages authored under non-strict mode (quirks) will not change behavior in IE7 and will not be affected by broken CSS filters.Pages authored under non-strict mode (or "quirks mode") will not change behavior in IE7."
  18. Web site: !DOCTYPE . 2007-01-13 . Microsoft. Microsoft Developer Network. "The following examples show how to use the !DOCTYPE declaration to specify the DTD a document conforms to, and to switch Internet Explorer 6 and later to standards-compliant mode."
  19. Web site: Hack-free CSS for IE . Arve Bersvendsen . February 2004 . Arve Bersvendsen's weblog . 2007-01-16 . dead . https://web.archive.org/web/20070115183512/http://virtuelvis.com/archives/2004/02/css-ie-only . 15 January 2007 . dmy-all .
  20. Web site: Vorsprung durch Webstandards | Douglas Bowman declares his love to CSS . Vorsprungdurchwebstandards.de . 2010-07-07 . https://web.archive.org/web/20100614070042/http://www.vorsprungdurchwebstandards.de/interviews/fallinginlovewithcss/douglas-bowman/ . 2010-06-14 . dead .
  21. Web site: Vorsprung durch Webstandards | Peter-Paul Koch declares his love to CSS . Vorsprungdurchwebstandards.de . 2010-07-07 . https://web.archive.org/web/20100227061437/http://www.vorsprungdurchwebstandards.de/interviews/fallinginlovewithcss/peter-paul-koch/ . 2010-02-27 . dead .
  22. Web site: Box Model Enlightenment, Bernie Zimmermann . Bernzilla.com . 4 April 2003 . 2010-07-07 . https://web.archive.org/web/20101227135212/http://www.bernzilla.com/item.php?id=33 . 27 December 2010 . dead .
  23. Web site: CSS3 Basic User Interface Module . World Wide Web Consortium.
  24. Web site: -moz-box-sizing . 2009-04-11 . . Mozilla. 11 April 2009 . It is possible to use this property to emulate the behavior of browsers that do not correctly support the CSS box model specification. .