Every element on a web page is displayed as a rectangular box. Learning how that box is built explains almost every question about spacing and size.

The Four Layers

At the centre is the content, the text or picture itself. Around it is the padding, a space inside the box that separates the content from the border. The border is a line drawn around the padding. Outside the border is the margin, a space that separates the box from its neighbours.

Definition: Box model. The model that treats every element as a box made of content, padding, border and margin.

Each layer can be set with properties such as padding, border and margin, and each can be set separately for the top, right, bottom and left sides, for example margin-top or padding-left.

Shorthand Values

The shorthand margin: 10px 20px; sets the top and bottom to 10 pixels and the left and right to 20 pixels. With four values, the order is top, right, bottom and left, which is clockwise from the top. The same pattern is used by padding. The border is written as width, style and colour, for example border: 2px solid navy;.

Calculating the Size of a Box

By default, the width property sets the width of the content only. The padding and the border are added to it. The property box-sizing changes this. With box-sizing: border-box, the width includes the padding and the border, so the box is exactly as wide as the author wrote.

Definition: Box-sizing. A property that decides whether width and height include padding and border.

Table 2.5: Total width of a box with width 200px, padding 20px and border 5px

box-sizing Content width Total width
content-box (default) 200 px 200 + 20 + 20 + 5 + 5 = 250 px
border-box 150 px 200 px

Margins Between Boxes

Margins create space between elements. The vertical margins of two blocks that touch do not add together. They collapse into the larger of the two. If one paragraph has a bottom margin of 20 pixels and the next has a top margin of 30 pixels, the gap is 30 pixels and not 50. Horizontal margins do not collapse.

Example 2.5: A card with padding, border and margin

<style>
  .card {
    width: 200px; padding: 20px; border: 5px solid #1F3864;
    margin: 10px; background: #DEEAF6; box-sizing: border-box;
  }
</style>
<div class="card">Content is inside padding, inside the border.</div>

Output in the browser:

The card is exactly 200 pixels wide in total because box-sizing is border-box. Without it, the card would be 250 pixels wide.

Common mistakes

  • Forgetting that padding and border add to the width when box-sizing is content-box.
  • Expecting vertical margins of neighbouring blocks to add together.
  • Mixing up margin, which is outside the border, with padding, which is inside it.
  • Reading four-value shorthand in the wrong order instead of top, right, bottom, left.

Key takeaways

  • Every element is a box made of content, padding, border and margin.
  • Four-value shorthand runs clockwise: top, right, bottom, left.
  • Border-box makes the width include padding and border.
  • Vertical margins between touching blocks collapse into the larger one.

Practice questions

  1. List the four layers of the box model from the inside out.
  2. What is the total width of a content-box element with width 100px, padding 10px and no border?
  3. What does border-box change?

Answers to the practice questions

  1. Content, padding, border and margin.
  2. 100 + 10 + 10 = 120 px.
  3. The width then includes the padding and the border.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit II