Escaping the Box Model

tagged CSS by Jon

Not knowing how padding, borders and margins interact with each other across the various browsers is a guarantee that you are going to struggle with CSS layouts. Having a good understanding of the box model will save you hours of figuring out why your layout keeps breaking. Even better, knowing how to break out of the box model will allow you to create layouts that were never possible before.

Note: This is a concept based tutorial so don’t expect to learn much syntax.

What is the Box Model

The CSS box model consists of four different elements. The core of the box is the content that could be something like an image or a paragraph of text which is surrounded by the optional padding, border and margin.

Diagram 1: Diagram of the box model
Content
The content area is the space that a rendered element uses. Rendered means after the browser has applied any styles and has displayed it visually on the screen. This can include things numbers or bullets on a list. Pretty much anything that takes up space in a layout is considered content. All content has an invisible box around surrounding it.
Padding
Padding is the layer immediately surrounding the content. It’s purpose is to add space between the content and it’s borders. It will share the same background as the content and increase the overall width and height. This is important to remember. If your content is 100 pixels wide and you add 20 pixels of padding the overall width will stretch to 140 pixels. So if you need to maintain the 100 pixel width with the additional padding, set the content to 60 pixels wide.
Border
Border has many of the same attributes as padding. However, it has it’s own background color and can be set to be displayed as dotted, dashed, double, grooved and a few others. Borders will also increase the overall size just like the padding.
Margin
Margins make up the outer most layer in the box model. They are completely invisible and should be used to add space between elements. It’s purpose it to add space between other content areas. Margin has an unique attribute that many people aren’t aware of. It will collapse when it is next to other margins. I’ll explain this in more detail later.

Collapsing Margins

Diagram 2: Diagram of box model and collapsing margin

Margins have the attribute of collapsing when they touch other margins. For example, I have a document with a bunch of paragraphs. To separate the paragraphs I gave them a 40 pixel margin on the top and bottom. Now, you would assume that the 40 pixel margin on the top paragraph added to the 40 pixels of the top margin on the lower paragraph would equal 80 right? Nope. The margins will collapse on each other and make a 40 pixel space between the two paragraphs.

Breaking Out

Knowing how the box model works will help you to create good solid layouts that won’t break every time you breath on it. But the best part of the box model is how it can be shaped with CSS. In the past, with table based layouts, you were stuck with grid based designs where every content element was stuck inside of a box. With CSS you can shape the box model to make elements extend outside of the box to create overlapping and create effects like drop shadows.

Here are the basics of how to extend an element outside of the box.

Diagram 3: Diagram of how to break out of the box model

The HTML of the diagrams above would look something like this:

  1.  
  2. <div id="box">
  3.      <img src="example.gif" />
  4.      <p>Ut mattis diam vel augue…</p>
  5. </div>
  6.  

In diagram a, there is an image and a paragraph of text inside of a containing div with a set width and height.

This is how you would apply the CSS

Diagram b

  1.  
  2. #box img {
  3.      float: left;
  4. }
  5.  

The image in diagram b has been floated to the left so that the paragraph wraps around it. It wasn’t necessary to make an entire step out of this, but my diagram would have looked funny with only 3 boxes :/

Diagram c

  1.  
  2. #box img {
  3.      float: left;
  4.      position: relative;
  5.      top: -50px;
  6.      left: -50px;
  7. }
  8.  

In diagram c the image was set to a relative position and given a negative 50 pixels to the top as well as to the left. Notice that even though the image is now overlapping the top right corner, the space it was taking up is still there. The paragraph is still wrapping around that area.

Diagram d

  1.  
  2. #box {
  3.      position: relative;
  4. }
  5.  
  6. #box img {
  7.      position: absolute;
  8.      top: -50px;
  9.      left: -50px;
  10. }
  11.  

diagram d displays a different effect. If I replace the image’s relative position with an absolute one it will no longer take up any space inside the box and the text will no longer try to wrap around it. To make sure that the image is contained inside the div I need to give it a relative position. The float would no longer be needed with an absolute position.

Absolutely positioned elements placed inside relatively positioned containers is a technique worth experimenting with. It’s a way of creating layers.

A practical application of this would be giving an image a dropshadow. You could do this by creating a shadow “layer” behind an image and then giving the image a negative offset to reveal the shadow.

Leave your mark...

If you liked this article please leave a comment and share it.

Add a comment Digg Stumble Upon Reddit

Gravatar
Michael
December 3rd, 2007

Nice tute. One of the better and easier to understand tutes on the dreaded box-model.

Thanks.

Gravatar
Dominic
January 4th, 2008

Nice tutorial, however can you fix one thing for me?
Under border you say “Padding has many of the same attributes as padding.”
Nice website too , i stumledupon it.

Gravatar
Fernando F.Oliveira
January 4th, 2008

Nothing better that visual examples showing how to do CSS .
Greate article to the beginners like me.

Gravatar
Naveen
January 4th, 2008

Neat. There’s a mistake though. In the description of BORDER -

Border
Padding has many of the same attributes as padding.

It should be -

Border
Border has many of the same attributes as padding.

Gravatar
Jon
January 4th, 2008

@Naveen and Dominic

Thanks guys! I appreciate your sharp eyes. During the day I work a full time job so most of these articles were written in the wee hours of the night/morning when I’m not quite all with it.

Gravatar
Mia
January 7th, 2008

This is an awesome tutorial, I’ve always had problems with this stuff but you made it easy to understand, thank you =)

Gravatar
Rob Diana
January 12th, 2008

Nice, simple, and straight forward tutorial. Showing code with the visual result is very helpful.

Gravatar
Shimul
April 23rd, 2008

Really nice tutorial… really liked this.. :)

Gravatar
pepen
June 20th, 2008

awesome! thanks

Gravatar
pusula para icat elmas
July 15th, 2008

css layer examples / properties and layer attributes
http://css-lessons.ucoz.com/css-layer-properties.htm

Gravatar
Richard Slater
September 13th, 2008

Nice Article, I have always struggled with getting it right with the CSS box model, this article explains it clearly and concisely!

Gravatar
Makati Goji Juice
October 9th, 2008

Nice and informative article…

Gravatar
ArdianYS Prestashop
January 23rd, 2010

Thanks for sharing it. This info is usefull, arrived here by google.

Gravatar
HeikkiHallantie
January 30th, 2010

It was nice and simple.

Thanks

Gravatar
Barbara
May 21st, 2010

Hi, your tutoriel is not only written well, but also beautiful to look!
If only many people could be inspired by it…
Thanks

Gravatar
Dimitris
June 11th, 2010

Vertical margins collapse, however horizontal do not and it can get more complicated than just that.

Add a comment

This comment system uses Gravatars for customized avatars. Get your account here.

If this is your first comment, it will be held for moderation. I'll get to it as fast as I can. Thanks :p