PSD to HTML PSD to HTML

CSS Box Model: The Foundation For Improving Your CSS

TalkSpot Free Website, Free Hosting, Create Website, Free Webpage
Google Buzz InstantShift - 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.

Please feel free to join us and you are always welcome to share your thoughts even if you have more reference links related to other money saving tips that our readers may like.

Don’t forget to Subscribe to our RSS-feed subscribe to our RSS-feed and Follow us on Twitter 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.

  • Normal flow — In CSS 2.1, normal flow includes block formatting of block boxes, inline formatting of inline boxes, relative positioning of block or inline boxes, and positioning of run-in boxes.
  • Floats — In the float model, a box is first laid out according to the normal flow, then taken out of the flow and shifted to the left or right as far as possible. Content may flow along the side of a float.
  • Absolute Positioning — In the absolute positioning model, a box is removed from the normal flow entirely (it has no impact on later siblings) and assigned a position with respect to a containing block.
  • Relative Positioning — In the relative positioning model, a box is shifted relative to its position in the normal document flow. Other elements are not affected and lay where they would had no positioning been applied.

Boxes will flow around a floated element, they will act as though absolutely positioned elements are not there, and they will treat relatively positioned elements as though no positioning was applied at all.


Calculating the Widths and Heights of Boxes

instantShift - CSS Box Model

When you set a width or height in CSS you’re setting the width or height of the content area portion of a box only. The width and height of the containing block (box) is defined as follows:

width of containing block (box) = ‘margin-left’ + ‘border-left-width’ + ‘padding-left’ + ‘width’ + ‘padding-right’ + ‘border-right-width’ + ‘margin-right’

height of containing block (box) = ‘margin-top’ + ‘border-top-width’ + ‘padding-top’ + ‘height’ + ‘padding-bottom’ + ‘border-bottom-width’ + ‘margin-bottom’

Notice that the width and height of the block includes both the left and right or top and bottom margin and padding as well as the border width.

When one of these values is not specified browser defaults are used. One issue with cross browser development is that different browsers may use different default values for the properties defining a box. The reason to use CSS reset files is to ensure that all default values are the same across browsers.


Issues with IE6 and the Box Model

Older versions of Internet Explorer, including IE6 (without a proper doctype) have a box model bug and calculate the width property inaccurately.

Width should only refer to the width of the content area. Older versions of IE and IE6 in quirks mode calculate the width as being the width of the content area + left and right paddings + the width of the right and left borders

Similarly it includes paddings and borders when calculating the height property.

instantShift - CSS Box Model

For the most part you shouldn’t have to deal with this issue any more as long as you use a proper doctype. Beware though that IE6 is still used and if you fail to specify a doctype then IE6 will revert to quirks mode and calculate widths and heights inaccurately as described in this section


Summary

The CSS box model is the foundation upon which the rest of CSS is based. Remember that every element in the document tree is defined by a rectangular box described by this box model. Boxes are of one of two types, block and inline, each with its own rules about where it lays and where elements that come after it lay.

instantShift - CSS Box Model

When a box is set via display: none the space that box occupied collapses.

When a box is set via visibility: hidden the box is not seen, but it still holds its space.

Floats and positioned elements take boxes out of the normal document flow and affect where they sit and where the elements around them also sit.

Older versions of Internet Explorer inaccurately calculate the width of the visible content area of a box and consequently the box itself. It’s an issue you shouldn’t counter often, but one to still be aware of.

I hope this post has helped you to understand better how the box model works, but if you have any questions please ask.

Images Credits

Find Something Missing?

While writing this article, it’s always a possibility that we missed some other great tips related to CSS box model. Feel free to share it with us.




Author : Steven Bradley

Steven Bradley is a web designer and WordPress developer living in Boulder Colorado. He can be found around the web by the username @vangogh and blogs at Van SEO Design. He also owns and operates a small business forum helping people learn how to run and market their business better.

2 Articles posted by Steven Bradley.
gravatar

If you enjoyed reading, consider sharing it on one of these social bookmarking sites.


RSS Enjoy this Post? Subscribe to InstantShift.

RSS Feed   RSS Feed     Email Feed  Email Feed



Leave a Comment Subscribe to RSS Feed Subscribe by Email
  1. Gravatar SEOmium November 16, 2009

    Comment Arrow

    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.


  2. Gravatar Gemma November 16, 2009

    Comment Arrow

    Wow powerful post thx alot of all the ideas and tips


  3. Gravatar designfollow November 16, 2009

    Comment Arrow

    very useful

    thank you


  4. Gravatar Tony November 17, 2009

    Comment Arrow

    Thanks for sharing. Very useful information.


  5. Gravatar Edward Caissie November 17, 2009

    Comment Arrow

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


  6. Gravatar Steven Bradley November 17, 2009

    Comment Arrow

    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 :)


  7. Gravatar Tiffany S November 17, 2009

    Comment Arrow

    Useful overview, thanks!


  8. Gravatar Quick Link Now November 17, 2009

    Comment Arrow

    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


  9. Gravatar favSHARE November 17, 2009

    Comment Arrow

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


  10. Gravatar Iowa personal injury attorney November 17, 2009

    Comment Arrow

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


  11. Gravatar Neo November 18, 2009

    Comment Arrow

    thanks you for your post


  12. Gravatar Waheed Akhtar November 19, 2009

    Comment Arrow

    very informative. thanks for sharing


  13. Gravatar denbagus November 23, 2009

    Comment Arrow

    great information…thank you for share


  14. Gravatar Kartik Sehgal November 24, 2009

    Comment Arrow

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


  15. Gravatar Thomas December 7, 2009

    Comment Arrow

    very informative..thanks for sharing


  16. Gravatar Rahul - Web Guru December 8, 2009

    Comment Arrow

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


  17. Gravatar Amal Roy January 22, 2010

    Comment Arrow

    Thanks a lot friend.


  18. Comment Arrow

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


  19. Gravatar africanKing January 28, 2010

    Comment Arrow

    Thanks a lot, still just learning css


  20. Comment Arrow

    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.



  Leave a Trackback
  1. Tweets that mention CSS Box Model: The Foundation For Improving Your CSS | CSS | instantShift -- Topsy.com
  2. Tweets that mention CSS Box Model: The Foundation For Improving Your CSS | CSS | instantShift -- Topsy.com
  3. CSS Box Model: The Foundation For Improving Your CSS | TopRoundups
  4. CSS Box Model: The Foundation For Improving Your CSS | CSS … | Kerja Keras Adalah Energi Kita
  5. CSS Box Model: The Foundation For Improving Your CSS | CSS | instantShift « Netcrema – creme de la social news via digg + delicious + stumpleupon + reddit
  6. Tweets that mention CSS Box Model: The Foundation For Improving Your CSS | CSS | instantShift -- Topsy.com
  7. CSS Brigit | CSS Box Model: The Foundation For Improving Your CSS
  8. links for 2009-11-16 | Aram on Mason
  9. links for 2009-11-16 | AndySowards.com :: Professional Web Design, Development, Programming Freelancer, Hacks, Downloads, Math and being a Web 2.0 Hipster?
  10. WPUK » Blog Archive » CSS Box Model: The Foundation For Improving Your CSS | CSS …
  11. CSS Box Model: The Foundation for CSS based layouts | Canada Web Solutions | Quick Link Now
  12. Tech Blog — John Keyes – Linkeyes 17th November 2009
  13. links for 2009-11-17 « random thoughts and casual ruminations
  14. Darklg Web (darklgweb) 's status on Tuesday, 17-Nov-09 12:13:10 UTC - Identi.ca
  15. CSS Box Model: The Foundation For Improving Your CSS | favSHARE.net
  16. CSS Box Model: The Foundation For Improving Your CSS | Design Newz
  17. Inspiration from the Net #4 « HU Design
  18. video | ClickLogin Web Design
  19. Useful Resources for CSS and HTML | Programming Blog
  20. Silver plated Rise and Fall financial or money market cufflinks with presentation box. Made in the U.S.A | Superstar Book Deals
  21. links for 2009-11-19 « toonz
  22. CSS Box Model: The Foundation For Improving Your CSS » Expression Web Tips
  23. fritz freiheit.com blog » Link dump
  24. Enlaces semanales que no he publicado (39) | Cosas sencillas
  25. Information Technology for Management: Improving Performance in the Digital Economy | Cheap Technology Buys
  26. Master CSS across browsers « luvx3.org
  27. How Well Do You Understand CSS Positioning? | Van SEO Design
  28. How Well Do You Understand CSS Positioning? | Programming Blog
  29. Online Business Management Software and Services » Blog Archive » How Well Do You Understand CSS Positioning?
  30. This Weeks Twitter Design News Roundup N.12 : Speckyboy Design Magazine
  31. This Weeks Twitter Design News Roundup N.12 | Programming Blog
  32. Great CSS Resources and Articles for Students | Resources for Web Development Students @ Robin's Blog
  33. This Weeks Twitter Design News Roundup N.12 | Download E-Books Free Video Training Courses Softwares
  34. Useful Resources for CSS and HTML | Master Design
  35. 精品阅读(13)20091127 -- 寂寞如哥
  36. Brad Pitt's Gifts to New Orleans – New York Times | Brad Pitt Celebrity Monitor
  37. Top Articles On The Web Design Billboard In November’09 | Programming Blog
  38. Top Articles On The Web Design Billboard In November’09 | meshdairy
  39. This Weeks Twitter Design News Roundup N.12 | Master Design
  40. 60+ Ultimate Resources Especially For Designers To Discover The Best Of The Web In November @ SmashingApps
  41. 60+ Ultimate Resources Especially For Designers To Discover The Best Of The Web In November : BeginnerPC
  42. Wordpress Blog Services - 60+ Ultimate Resources Especially For Designers To Discover The Best Of The Web In November
  43. What Are Some Reasons For Society To Accept And Reject Gay Couples? | bloomington hotel
  44. OmniDownloads | 60+ Ultimate Resources Especially For Designers To Discover The Best Of The Web In November
  45. FreeDownloadSecrets.com » Blog Archive » 60+ Ultimate Resources Especially For Designers To Discover The Best Of The Web In November
  46. Improving the Management of Output in an Sap Environment | Cheap Technology Buys
  47. 60 Amazing Resources for Designers :: Reflex Stock Photo Blog
  48. Lär dig CSS | Webbdesign
  49. 60+ Ultimate Resources Especially For Designers To Discover The Best Of The Web In November | meshdairy
  50. 60+ Ultimate Resources Especially For Designers To Discover The Best Of The Web In November | The Apple Tech Blog
  51. Required Reading – 12/15/09 « I want my dream SEO job!
  52. 4 Most Useful Resources for CSS and HTML for Web Designers Web Design Blog
  53. TG Developer » 47 CSS Tips & Tricks To Take Your Site To The Next Level

  • Gravatar

    Your NameMarch 15, 2010

Arrow
Spam protection: Sum of 1 + 10 ?



If you face problem with captcha and it says the sum is incorrect. Make sure...

1. Browser is javascript enabled and accepting cookies.
2. Clear browser cache and try again.



http://www.instantshift.infohttp://www.instantshift.orghttp://www.instantshift.net