CSS Box Model: The Foundation For Improving Your CSS

The CSS box model lies behind everything you do in CSS. Every element is defined by a rectangular box that encloses that element. Understanding how the box model works is a key to understanding CSS and having greater control over your layout and presentation. Let’s dive right in and talk about what the CSS box model is, how one box affects the boxes around it, and some common browser issues when displaying CSS boxes.

In nutshell, the box model in CSS describes the boxes which are being generated for HTML-elements. In this post below you’ll learn the tips and techniques exactly about CSS box model to achieve best out of CSS development.

You may be interested in the following related articles as well.

Feel free to join us and you are always welcome to share your thoughts that our readers may find helpful.

Don’t forget to subscribe to our RSS-feed and follow us on Twitter — for recent updates.

What is the CSS Box Model?

Every element in the document tree is defined by a rectangular box. The CSS box model describes those boxes and defines the properties each has. The easiest way to understand how the box model works is with a simple image.

instantShift - CSS Box Model

Every element has a width and height associated with its content area. Each element then has an area of padding surrounding the content and a border containing the padding and content area. Finally each element has a margin outside of its border. Paddings, borders, and margins have values for their top, right, bottom, and left sides.

If you think of an element’s edge as being it’s border then padding controls space inside the element and margin controls space outside the element and between neighboring elements. Note that when considering the background property of any element that background extends to the elements border and doesn’t include the margin of the element.

Differences Between Block Boxes and Inline Boxes

If you’re familiar with the CSS display property, you know it has values for block, inline, and none. Block and inline are 2 different types of boxes. Both adhere to the box model with one key difference in the way each is laid out on the page.

instantShift - CSS Box Model

Block level boxes are laid out vertically one after the other. If you have two block level boxes next to each other in your html, the second will sit below the first. Inline boxes on the other hand are laid out horizontally. An inline box will always sit to the right of the box that precedes it, assuming there’s enough room in the containing element.

Inline boxes will wrap though. They will start to the right of the previous box and fill whatever horizontal space is remaining. They will then wrap to the next line and again move to fill the horizontal space. A block level box would automatically drop to the next line before filling any space.

Block and inline cover two of the display properties. The third none says no block exists. If you assign the value of none to any CSS box, that box is completely removed from the normal document flow. Conversely if you set the CSS property visibility to hidden, the box is still there filling the space according to the CSS box model rules. You don’t see it, but it does hold the space.

Floats, Positioning, and the Normal Document Flow

The discussion above about block and inline boxes assumed each was in the normal document flow. Floated and positioned elements are still boxes, but they are removed from the normal document flow in different ways. Both also alter how other elements react to their box.

Like it? Share it.

103 Comments

  1. Pingback: Tweets that mention CSS Box Model: The Foundation For Improving Your CSS | CSS | instantShift -- Topsy.com

  2. Brilliantly written article and lots of useful information. Thank you very much for pointing the details out.

    In my experience, I found the absolute positioning, when used/coded correctly, is the best way for cross-platform compatible layout development. There may be some semantically issues though, depending on your coding but it is not hard to fix.

    And also you pointed out the importance of using CSS resets. Really important indeed. I would be glad if someone points me to the direction where I can find the default padding and margin values per browser. Just curious.

  3. Wow powerful post thx alot of all the ideas and tips

  4. very useful

    thank you

  5. Pingback: Tweets that mention CSS Box Model: The Foundation For Improving Your CSS | CSS | instantShift -- Topsy.com

  6. Pingback: CSS Box Model: The Foundation For Improving Your CSS | TopRoundups

  7. Pingback: CSS Box Model: The Foundation For Improving Your CSS | CSS … | Kerja Keras Adalah Energi Kita

  8. Pingback: CSS Box Model: The Foundation For Improving Your CSS | CSS | instantShift « Netcrema – creme de la social news via digg + delicious + stumpleupon + reddit

  9. Pingback: Tweets that mention CSS Box Model: The Foundation For Improving Your CSS | CSS | instantShift -- Topsy.com

  10. Pingback: CSS Brigit | CSS Box Model: The Foundation For Improving Your CSS

  11. Thanks for sharing. Very useful information.

  12. Pingback: links for 2009-11-16 | Aram on Mason

  13. Great information presented in a very easy to read and understand method. Well Done! … and Thanks!

  14. Thanks all. I’m glad you liked the post.

    @SEOmium – I generally think floats are a better approach to developing a layout cross browser. You can certainly layout a site with absolute positioning, but I find that positioning one element means positioning another element and another. Usually you only need to float one or two key parts of a layout to get it working.

    The cross browser issues are minimal and easy enough to deal with, especially if you use a css reset :)

  15. Pingback: links for 2009-11-16 | AndySowards.com :: Professional Web Design, Development, Programming Freelancer, Hacks, Downloads, Math and being a Web 2.0 Hipster?

  16. Pingback: WPUK » Blog Archive » CSS Box Model: The Foundation For Improving Your CSS | CSS …

  17. Useful overview, thanks!

  18. this is what we need to learn first.. when learning css based layout.

    Your article is superb, and I like to share it others.. Bookmarked.

    I like it verymuch.. thank you..

    Quick Link Now

  19. Pingback: CSS Box Model: The Foundation for CSS based layouts | Canada Web Solutions | Quick Link Now

  20. Pingback: Tech Blog — John Keyes – Linkeyes 17th November 2009

  21. Pingback: links for 2009-11-17 « random thoughts and casual ruminations

  22. Pingback: Darklg Web (darklgweb) 's status on Tuesday, 17-Nov-09 12:13:10 UTC - Identi.ca

  23. Pingback: CSS Box Model: The Foundation For Improving Your CSS | favSHARE.net

  24. This article has been shared on favSHARE.net. Go and vote it!

  25. That should be really helpful, in the near future. Great post that is exactly what I was looking for.

  26. Pingback: CSS Box Model: The Foundation For Improving Your CSS | Design Newz

  27. Pingback: Inspiration from the Net #4 « HU Design

  28. thanks you for your post

  29. very informative. thanks for sharing

  30. Pingback: video | ClickLogin Web Design

  31. Pingback: Useful Resources for CSS and HTML | Programming Blog

  32. Pingback: Silver plated Rise and Fall financial or money market cufflinks with presentation box. Made in the U.S.A | Superstar Book Deals

  33. Pingback: links for 2009-11-19 « toonz

  34. Pingback: CSS Box Model: The Foundation For Improving Your CSS » Expression Web Tips

  35. Pingback: fritz freiheit.com blog » Link dump

  36. Pingback: Enlaces semanales que no he publicado (39) | Cosas sencillas

  37. Pingback: Information Technology for Management: Improving Performance in the Digital Economy | Cheap Technology Buys

  38. Pingback: Master CSS across browsers « luvx3.org

  39. great information…thank you for share

  40. Pingback: How Well Do You Understand CSS Positioning? | Van SEO Design

  41. Pingback: How Well Do You Understand CSS Positioning? | Programming Blog

  42. Pingback: Online Business Management Software and Services » Blog Archive » How Well Do You Understand CSS Positioning?

  43. Brilliantly presented article. You have focused on a small yet critical part of UI. Thanks a ton!

  44. Pingback: This Weeks Twitter Design News Roundup N.12 : Speckyboy Design Magazine

  45. Pingback: This Weeks Twitter Design News Roundup N.12 | Programming Blog

  46. Pingback: Great CSS Resources and Articles for Students | Resources for Web Development Students @ Robin's Blog

  47. Pingback: This Weeks Twitter Design News Roundup N.12 | Download E-Books Free Video Training Courses Softwares

  48. Pingback: Useful Resources for CSS and HTML | Master Design

  49. Pingback: 精品阅读(13)20091127 -- 寂寞如哥

  50. Pingback: Brad Pitt's Gifts to New Orleans – New York Times | Brad Pitt Celebrity Monitor

  51. Pingback: Top Articles On The Web Design Billboard In November’09 | Programming Blog

  52. Pingback: Top Articles On The Web Design Billboard In November’09 | meshdairy

  53. Pingback: This Weeks Twitter Design News Roundup N.12 | Master Design

  54. Pingback: 60+ Ultimate Resources Especially For Designers To Discover The Best Of The Web In November @ SmashingApps

  55. Pingback: 60+ Ultimate Resources Especially For Designers To Discover The Best Of The Web In November : BeginnerPC

  56. Pingback: Wordpress Blog Services - 60+ Ultimate Resources Especially For Designers To Discover The Best Of The Web In November

  57. Pingback: What Are Some Reasons For Society To Accept And Reject Gay Couples? | bloomington hotel

  58. Pingback: OmniDownloads | 60+ Ultimate Resources Especially For Designers To Discover The Best Of The Web In November

  59. Pingback: FreeDownloadSecrets.com » Blog Archive » 60+ Ultimate Resources Especially For Designers To Discover The Best Of The Web In November

  60. very informative..thanks for sharing

  61. Pingback: Improving the Management of Output in an Sap Environment | Cheap Technology Buys

  62. Always interesting to go through articles of Instant shift. This particular article about CSS box model was quite helpful for me.

  63. Pingback: 60 Amazing Resources for Designers :: Reflex Stock Photo Blog

  64. Pingback: Lär dig CSS | Webbdesign

  65. Pingback: 60+ Ultimate Resources Especially For Designers To Discover The Best Of The Web In November | meshdairy

  66. Pingback: 60+ Ultimate Resources Especially For Designers To Discover The Best Of The Web In November | The Apple Tech Blog

  67. Pingback: Required Reading – 12/15/09 « I want my dream SEO job!

  68. Pingback: 4 Most Useful Resources for CSS and HTML for Web Designers Web Design Blog

  69. Thanks a lot friend.

  70. Ie6 is still used out there, thanks for your explanation about ie box model bugs

  71. Thanks a lot, still just learning css

  72. Thanks very much for these tips. I just wonder if these CSS display properties will work the same way in all browsers (IE, Firefox, Safari, Netscape etc). This is the main concern nowadays because of the cross-browser compatibility.

  73. Pingback: TG Developer » 47 CSS Tips & Tricks To Take Your Site To The Next Level

  74. Pingback: 47 CSS Tips & Tricks To Take Your Site To The Next Level - Programming Blog

  75. Pingback: The Meaning Of Shapes: Developing Visual Grammar | Van SEO Design

  76. Pingback: The Meaning Of Shapes: Developing Visual Grammar - Programming Blog

  77. Pingback: Useful Resources for CSS and HTML | Design Reviver

  78. hi ,
    Thank you for sharing , i am web designer beginner i am finding a css menu based resources my friend told me you can try this reference’s so iam landing on your site but i am buried i have no found on your site any css based menu so could you update css menu regarding article .

  79. Pingback: Part 1: How to Turn a Design Image Into a Working Web Page « gabriel catalano | in-perfección

  80. thanks

  81. Thank you for best article,
    best regards…

    [..URL..]

  82. Thanks and Regards

  83. css after i learnd this css i started taking projects from local business owners about website designing , before that i was like i dont want to work on website designing and all.CSS Rocks.

  84. Pingback: Other Sites Where I’ve Posted Recently | Van SEO Design

  85. Thanks for your beneficial article about the usage of CSS box model.It do helps me to improve my understanding about how to use it.

  86. Thank you so much for sharing this sweet stuff with the CSS Box Model. It was more helpful to us to improve our CSS sites. Keep up sharing good stuffs like this, Congrats.

  87. Wonderful posts you have on your blog and I have bookmarked you and will visit yours often.
    I will be sharing this info on my facebook page with my friend and relatives. Thanks.
    Acai Diet News

  88. Hey,thanks for teh share, good content.

  89. Pingback: 2 Column CSS Layout: Fixed Width And Centered | Van SEO Design

  90. Pingback: 47 CSS Tips & Tricks To Take Your Site To The Next Level

  91. Another great post, I appreciate all the work you put into this site, helping out others with your fun and creative works.

  92. Well somehow I got to read lots of articles on your blog. It’s amazing how interesting it is for me to visit you very often.

  93. Well somehow I got to read lots of articles on your blog. It’s amazing how interesting it is for me to visit you very often.

  94. This is beyond doubt a blog significant to follow. You’ve dig up a great deal to say about this topic, and so much awareness. I believe that you recognize how to construct people pay attention to what you have to pronounce, particularly with a concern that’s so vital. I am pleased to suggest this blog.

  95. Pingback: Rearranging Boxes: Why You Should Structure Your HTML To Be Flexible | Van SEO Design

  96. I’ve decided to make it open source so that it’s freely available to anyone interested in learning how mouse gestures are implemented. I will be also be posting new releases regularly so that non-developers can benefit from mouse gestures.

  97. Lately I came to your website and have Been reading along. I thought I would leave my initial comment. Keep writing, cause your posts are impressive!

  98. Wham bam thank you, ma’am, my quseoitns are answered!

  99. Pingback: An Attempted Solution To A Real Responsive Layout Problem | Van SEO Design

  100. I’m finally making a decision to buckle down and tackle CSS after a long break in active web design. Things have changed alot. This post was a great read and really simplified things. I’ll look for more info like this.

  101. Pingback: The CSS Display Property: A Reintroduction To A Familar Friend | Van SEO Design

  102. I mean I just read through the entire article of yours and it was

    quite good but since I’m more of a visual learner,I found that to be

    more helpful well let me know how it turns out! I love what you guys

    are always up too. Such clever work and reporting! Keep up the great

    works guys I’ve added you guys to my blogroll. This is a great

    article thanks for sharing this informative information.

  103. The CSS Box Model is one of the first things you learn about when starting the road to becoming a master of CSS or should be anyway.

Leave a Comment Yourself

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>