Web Development
HTML Course
CSS Course
JavaScript Course
PHP Course
Python Course
SQL Course
SEO Course

Box Model - The Box Model

The CSS box model is the foundation of web page layout. Every element is essentially a box, which contains content, padding, border, and margin, stacked together like the layers of an onion.

As the browser renders the page, it calculates the styles applied to each box as well as the relationships between all boxes on the page. Before creating CSS layouts, it is essential to fully understand the box model.

Margin
Border
Padding
Content

Every element in a document is structured like a box, with multiple layers that can be adjusted using a set of CSS properties characteristic of the box model, as shown in the image above.

Box Properties

There are also other properties that limit dimensions, such as min-width, max-width, min-height, and max-height.

Margin collapsing – if two boxes touch, the distance between them equals the largest of the two margins, not the sum.

Some important considerations:

By default, background-color / background-image extends to the border. This can be changed using background-clip.

For content exceeding the visible area, scrollbars appear. This behavior is controlled using overflow.

The height of boxes depends on content, unless a fixed value is explicitly set.

The border property ignores percentage dimensions.

Total width = width + padding-right + padding-left + border-right + border-left. box-sizing helps calculate correctly.

Advanced Box Handling Methods

Beyond modifying width, border, padding, and margin, there are other properties that allow us to change box behavior.

Overflow

When we set the size of a box with absolute values (e.g., width: 500px / height: 300px) and there is more content than available space, the content will "overflow" outside the box. To control such situations, we use the overflow property with one of the accepted values:

🔧 CSS Overflow Example

Background clip

The backgrounds of boxes can consist of colors and/or images, layered on top of each other. By default, the background extends to the outer edge of the border, but this behavior can be adjusted using background-clip, which defines how far the background extends inside the box.

🔧 Background-clip Example

Outline

The outline of a box (outline) is like a border, but it is not part of the box model. It behaves similarly to the border (border), surrounds the box without changing its size, and is drawn outside the box, inside the margin.

Be careful when using the outline property. It is usually used for accessibility purposes, to highlight elements such as links when the user clicks on them or navigates using the Tab key.

The outline is always present on all sides of the box and has the same appearance and thickness on each side.

It differs from the border: it is drawn outside it and can overlap other elements without changing the box's dimensions.

The only impact on surrounding elements is possible overlap.

If an element has an outline, it occupies the same space as if it had no outline.

The outline properties are:

  1. outline-width – specifies the thickness
  2. outline-style – specifies the style
  3. outline-color – specifies the color
  4. outline-offset – specifies the space between the element's edge/border and the outline
  5. outline – shorthand to set all properties at once

CSS Box Types

Everything discussed so far applies to boxes representing block-level elements. CSS also has other types of boxes, with different behaviors.

The type of box applied to an element is specified using the display property.

The main values of the display property are:

By default, block elements have display: block; and inline elements have display: inline;. This can be changed by declaring another value.

🔧 Basic Display Properties Example

🧠 Final Quiz – CSS Box Model

Top