The verdict is in. CSS Layout is great!

I’ve been working with CSS for a while now, and I have to say, it’s pretty awesome. I love being able to separate content from presentation, and to keep all my styles in an external stylesheet.

I’ve had a pretty good grasp on the basics for a while now, but I needed to learn more about how to control layout with my CSS. Understanding CSS layout meant first exploring the parts of the CSS box model.

The CSS Box Model

There are lots of great resources online to help you learn the CSS Box Model. I like the CSS Tricks article The CSS Box Model by Chris Coyier. To quote the author:

At the risk of over-repeating myself: every element in web design is a rectangular box.

That’s right! Every HTML element is considered by the browser to be a rectangular box.

illustration of the CSS box model

The CSS Box Model consists of four properties: content, padding, border, and margin. I've included a graphic from Coyier’s article to illustrate this principle.

It’s pretty easy to understand the Content portion of the box model. The content is whatever your HTML consists of. It could be a paragraph full of text, or a bulleted list, or an image.

Beginning developers may have some trouble keeping the other parts of the box model straight, so let’s examine them one by one.

Look! I'm a CSS Wizard now!!!

Padding

Padding immediately surrounds the content area. Padding is often used to give the content area some breathing room by separating the content from the surrounding border area.

Border

The border area of the box is the outermost part of the box. You can think of it as giving the rectangular box an outline. Borders tend to be hidden on most CSS elements by default, but once visible different styles like color and thickness can be applied to them.

Margin

Finally, the margin area exists outside of the box. Margin is the space around an element that separates it from other elements.

It’s important to remember that padding and border affect the size of a CSS box, but margin doesn’t.