As Web Developers, the benefits to be afforded by simply taking the time to aquire a fundamental understanding of CSS layouts can not be overstated; for it is these very skills that provide the basis from which designs can be achieved with ease; making the task of designing the Web an enjoyable, and rewarding experience. Conversely, without an understanding of core concepts, one is certain to spend a significant amount of time – often in frustration – attempting to achieve a desired layout.
CSS Layout Fundamentals
In the context of CSS layouts, such fundamental concepts to be considered (in no particular order) include: document flow, positioning, display types, off-sets, floats, overflows, clears, and the like; and, as the title of this article suggests, the Box Model.
The Box Model
I emphasize the importance of the Box Model here in particular, as the default sizing of elements with respect to the Box Model is quite the opposite of what one might expect.
For example, consider the following:
border: 1px solid #333;
As many would assume, any element with the
.box class would render with a 1px border and 20px of padding, at exactly 200x200px. However, by default, this is not how elements are sized, but rather, the actual size of a rendered element is calculated to include both
padding in addition to
height, the calculation for which essentially being:
total width = left border width + left padding +
right padding + right border width;
total height = top border width + top padding +
bottom padding + bottom border width;
This results in nearly all elements (form elements notwithstanding) being measured quite differently then one might have expected. Thus, in the example of the
.box class mentioned above, rather than elements being rendered at the expected 200x200px, as defined by their respective width and height properties, they would instead be rendered at 242x242px.
One could argue that in the majority of cases, this is neither what is expected nor what is desired. Fortunately, CSS3 offers the very useful property box-sizing, which can be used to override the default sizing of elements (content-box), and allow for sizing them more naturally simply by setting
/* set 'border-box' to size as expected */
border: 1px solid #333;
box-sizing: border-box, elements with the
.box class will render at the expected size, 200x200px, as defined by the width and height properties; with the content area being 158px, padding 40px, and border at 2px.
Box-sizing Global Resets
Paul Irish has a great solution for this very problem whereby a global reset is used to ensure all elements are sized with
border-box. I highly recommend this approach as it provides a starting point from which all elements will be sized “naturally”. The reset is simple, safe, and works perfectly well in supported browser:
/* set 'border-box' to size as all elements */
CSS3 background-clip and background-origin
The background-clip and background-origin properties, respectively, can both be used in tandem with
box-sizing; each accepting the same values as that of
box-sizing, allowing for related control of how backgrounds are displayed for elements with respect to the Box Model.
It was quite some time ago while trying to understand how the sizing of elements is determined in more detail that I first learned of the
box-sizing property. In the time since, I have been using
box-sizing:border-box with great results and have really come to appreciate this property.
And so, if you haven’t leveraged
box-sizing: border-box in your designs yet, I strongly recommend giving it a try, as you will likely find it to result in more “naturally” sized elements.