box model

Jump to: navigation, search

Exploring the CSS box model

This page has been flagged with the following issues:



This article describes how you can use CSS to control the space that an element takes up when it is displayed.

Information: Boxes

When your browser displays an element, the element takes up space. There are four parts to the space that it takes up.

In the middle, there is the space that the element needs to display its content. Around that, there is padding. Around that, there is a border. Around that, there is a margin that separates the element from other elements.





The pale gray shows parts of the layout.


This is what you see in your browser.

The padding, border and margin can have different sizes on the top, right, bottom and left of the element. Any or all of these sizes can be zero.


The padding is always the same color as the element's background. So when you set the background color, you see the color applied to the element itself and its padding. The margin is always transparent.





The element has a green background.


This is what you see in your browser.


You can use borders to decorate elements with lines or boxes. To specify the same border all around an element, use the Template:cssxref("border") property. Specify the width (usually in pixels for display on a screen), the style, and the color.

The styles are:

solid dotted dashed double
inset outset ridge groove

You can also set the style to none or hidden to explicitly remove the border, or set the color to transparent to make the border invisible without changing the layout.

To specify borders one side at a time, use the properties: Template:cssxref("border-top"), Template:cssxref("border-right"), Template:cssxref("border-bottom"), Template:cssxref("border-left"). You can use these to specify a border on only one side, or different borders on different sides.

Border example This rule sets the background color and the top border of heading elements:

h3 {
  border-top: 4px solid #7c7; /* mid green */
  background-color: #efe;     /* pale green */
  color: #050;                /* dark green */

The result looks like:

Note: add screenshot to show what this should look like.

This rule makes images easier to see by giving them a mid-gray border all round:

img {border: 2px solid #ccc;}

The result looks like:

Note: add screenshot to show what this should look like.

Margins and padding

You use margins and padding to adjust elements' positions and to create space around them. Use the Template:cssxref("margin") property or the Template:cssxref("padding") property to set the margin or padding widths respectively.

If you specify one width, it applies all around the element (top, right, bottom and left). If you specify two widths, the first applies to the top and bottom, the second to the right and left. You can specify all four widths in the order: top, right, bottom, left.

Margin and padding example

This rule marks out paragraphs with the class remark by giving them a red border all round. Padding all round separates the border from the text a little. A left margin indents the paragraph relative to the rest of the text:

p.remark {
  border: 2px solid red;
  padding: 4px;
  margin-left: 24px;

The result looks like:

Note: add screenshot to show what this should look like.

More details

When you use margins and padding to adjust the way elements are laid out, your style rules interact with the browser's defaults in ways that can be complex. Different browsers lay elements out differently. The results might look similar until your stylesheet changes things. Sometimes this can make your stylesheet give surprising results.

To get the result you want, you might have to change your document's markup. The next page of this tutorial has more information on this. For detailed information about padding, margins and borders, see the Box model reference page.

Action: Adding borders

  • Edit your CSS file, style2.css. Add this rule to draw a line across the page over each heading:

    h3 {border-top: 1px solid gray;}
  • If you took the challenge on the last page, modify the rule you created, otherwise add this new rule to add space underneath each list item:

    li {
      list-style: lower-roman;
      margin-bottom: 8px;
  • Refresh your browser to see the result:

    Need to add in a screenshot to show what this should look like.

    See also

    Exercise questions

    Add one rule to your stylesheet, making a wide border all around the oceans in a color that reminds you of the sea.


    This article contains content originally from external sources, including ones licensed under the CC-BY-SA license. cc-by-sa-small-wpd.png

    Portions of this content copyright 2012 Mozilla Contributors. This article contains work licensed under the Creative Commons Attribution-Sharealike License v2.5 or later. The original work is available at Mozilla Developer Network: Article