Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

CSS box model and normal flow

The box model describes the content area and its surrounding padding, border, and margin. Learn how box-sizing affects dimensions and why the margins of adjacent blocks do not always add together.

Version: Specification scope: CSS Box Model Level 3, CSS Box Sizing Level 3, CSS Backgrounds and Borders Level 3, and CSS Level 2. The usage example’s output was verified in Chromium 157.0.8103.51; this test does not establish browser compatibility or minimum supported versions.

Syntax

box-sizing: content-box | border-box;
/* Common values for width and height (not exhaustive). */
width: <length-percentage [0,∞]> | auto | min-content | max-content | fit-content(<length-percentage [0,∞]>);
height: <length-percentage [0,∞]> | auto | min-content | max-content | fit-content(<length-percentage [0,∞]>);
padding: <length-percentage [0,∞]>{1,4};
border: <line-width> || <line-style> || <color>;
margin: [ <length-percentage> | auto ]{1,4};

The box model describes boxes generated when laying out a document. From inside outward, their areas are content, inner spacing (padding), the border, and outer spacing (margin). Margins are transparent; they are not part of a box’s border or background.

With box-sizing: content-box, specified width and height lengths size the content. The width between the outer border edges is the content width plus left and right padding and borders. With box-sizing: border-box, specified lengths include padding and borders, but never margins. The content size cannot fall below zero, so padding and borders alone can force a larger box.

The box model defines areas and dimensions; flow determines box placement. In normal flow with a horizontal writing mode, blocks in a block formatting context stack vertically, while inline content forms lines. Adjacent vertical block margins can collapse instead of adding together; for two positive collapsing margins, the result is the larger one. Padding and borders do not collapse.

The example compares two empty blocks with otherwise identical dimension declarations. #content has a border-box size of 224 × 64, while #border measures 200 × 40 CSS pixels. These measurements exclude margins. Four-value shorthand declarations for padding or margin use the order top, right, bottom, left.

Examples

<!doctype html>
<meta charset="utf-8">
<title>Box model</title>
<style>
  .box {
    box-sizing: content-box;
    width: 200px;
    height: 40px;
    padding: 10px;
    border: 2px solid black;
    margin: 8px;
  }
  #border { box-sizing: border-box; }
</style>
<div id="content" class="box"></div>
<div id="border" class="box"></div>
<pre id="result"></pre>
<script>
  const ids = ['content', 'border'];
  document.getElementById('result').textContent = ids.map(id => {
    const rect = document.getElementById(id).getBoundingClientRect();
    return `${id}: ${rect.width} x ${rect.height}`;
  }).join('\n');
</script>

Result

content: 224 x 64
border: 200 x 40

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources