demosthenes.info

I’m Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with , and . To receive more information, including news, updates, and tips, you should follow me on Twitter.

featured articles

popular favourites

Page Layouts Made Easier With CSS3 box-sizing

A lot of frustration in creating web page layouts results from a misunderstanding of the CSS box model. To be fair, this isn’t entirely the fault of web developers or designers; blame can be equally apportioned to the oddities of the CSS spec itself.

Very simply, the width and height of an HTML element is not what most people assume it is. By default, the width of an element is the width of its content, not necessarily the width of the containing box: padding, border and outline are added to the width you set for any element. This leads to situations like the following:

Let’s say we have the opening two paragraphs from Mary Shelly’s Frankenstein, each provided with a different id:

  1. <p id="first">I am by birth a Genevese; and my family is one of the most
  2. distinguished of that republic…
  3. <p id="second">As the circumstances of his marriage illustrate his character…

We want to have both paragraphs to have a width of 50%, floated side-by-side on the page:

  1. p { width: 50%; }
  2. p#first { float: left; }
  3. p#second { float: right; }

Floated paragraphsAll looks fair and well to this point, as you can see to the right. Trouble occurs when we add a border to the paragraphs. As you will see, adding just a few pixels is enough to throw the paragraphs out of alignment:

  1. p { width: 50%; border: 1px solid #000; }

Paragraphs misalignedThis gives the result you see to the left. Historically, this behavour is one of the reasons I have preferred to use display: table-cell and its related properties to create column layouts in web pages. But changing the box-sizing model with a addition can fix this particular issue:

  1. p { width: 50%; border: 1px solid #000; box-sizing: border-box; }

border-box calculates each element’s width to the outside of its border. This means we can add padding to the paragraphs and still retain their alignment:

  1. p { width: 50%; border: 1px solid #000; box-sizing: border-box; padding: 3em; }

Paragraphs border-boxNote, however, that adding margin to the paragraphs will throw them out again, as the box-sizing model does not accommodate space outside the element.

box-sizing can also be set to padding-box, which takes into account padding but not border. The default box-sizing value is content-box.

As the traditional box-model can be so counter-intuitive and problematic, and this new setting so useful, it is suggested that setting box-sizing to border-box should be part of an opening CSS reset, with vendor prefixes added to support older versions of browsers:

  1. * { -moz-box-sizing: border-box;
  2. -webkit-box-sizing: border-box;
  3. box-sizing: border-box; }

You must be signed up in order to leave comments.

web developer guide

featured comment

by Aisling Brock in New Business Card Design

what i'm reading

A Feast for Crows: A Song of Ice and Fire: Book Four
A Feast for Crows: A Song of Ice and Fire: Book Four

what i'm watching

Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]
Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]

what i'm playing

Borderlands
Borderlands

what i'm hearing

Planets
Planets

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.