Web Development /CSS — Styling Modern Websites / Lesson
Web Development

CSS Box Model, Margin, Padding and Borders

Understand the CSS box model and learn how width, height, padding, borders, margins, box sizing, overflow and spacing work together to control webpage layouts.

🎯

Learning Objectives

["Understand the CSS box model.","Identify the content, padding, border and margin areas of an element.","Understand how width and height are calculated.","Use padding to create internal spacing.","Use margin to create external spacing.","Apply individual and shorthand margin values.","Apply individual and shorthand padding values.","Create and style borders.","Control border width, style and color.","Use border-radius to create rounded corners.","Understand box-sizing: content-box and border-box.","Calculate the effective size of an element.","Understand margin collapsing at a basic level.","Use overflow to control content that exceeds an element.","Create consistent spacing systems for layouts.","Debug unexpected element sizes using browser developer tools."]

📖

Lesson Content

Introduction

One of the most important concepts in CSS layout is the box model. Every HTML element displayed by the browser can be understood as a rectangular box.

The CSS box model explains how the content, padding, border and margin of an element combine to determine its size and spacing.

Understanding the box model is essential for building accurate and responsive website layouts.

1. What Is the CSS Box Model?

The CSS box model consists of four main areas:

  1. Content
  2. Padding
  3. Border
  4. Margin

Conceptually, the structure looks like this:

+--------------------------------------+
|               Margin                 |
|   +------------------------------+   |
|   |            Border            |   |
|   |   +----------------------+   |   |
|   |   |       Padding        |   |   |
|   |   |   +--------------+   |   |   |
|   |   |   |    Content   |   |   |   |
|   |   |   +--------------+   |   |   |
|   |   +----------------------+   |   |
|   +------------------------------+   |
+--------------------------------------+

Each layer serves a different purpose.

2. The Content Area

The content area contains the actual content of an element.

For example, in a paragraph:

<p>Welcome to Dwallnet Technologies.</p>

The text is part of the content area.

When you set:

.box {
    width: 300px;
    height: 150px;
}

with the default box-sizing: content-box, the width and height refer to the content area.

3. Padding

Padding creates space between the content and the border.

.card {
    padding: 20px;
}

This creates 20 pixels of internal space around the content.

Padding is useful when text or other content feels too close to the edge of a box.

4. Padding on Individual Sides

You can control each side separately.

.card {
    padding-top: 20px;
    padding-right: 25px;
    padding-bottom: 20px;
    padding-left: 25px;
}

This gives you precise control over the internal spacing.

5. Padding Shorthand

CSS allows padding to be written using shorthand notation.

One value:

padding: 20px;

This applies 20 pixels to all four sides.

Two values:

padding: 20px 30px;

The first value applies to the top and bottom, while the second applies to the left and right.

Three values:

padding: 20px 30px 15px;

The values represent:

  • Top
  • Left and right
  • Bottom

Four values:

padding: 10px 20px 30px 40px;

The order is:

top → right → bottom → left

This is commonly remembered as moving clockwise around the element.

6. Border

The border sits between padding and margin.

A basic border can be created with:

.card {
    border: 1px solid #ddd;
}

A border normally has three main components:

  • Border width
  • Border style
  • Border color

7. Border Width

You can control border thickness using border-width.

.box {
    border-width: 2px;
}

Individual sides can also be controlled:

.box {
    border-top-width: 3px;
    border-right-width: 1px;
    border-bottom-width: 3px;
    border-left-width: 1px;
}

8. Border Style

A border needs an appropriate style to become visible.

.box {
    border-style: solid;
}

Common border styles include:

  • solid
  • dashed
  • dotted
  • double
  • none

Example:

.dashed {
    border: 2px dashed #0b5ed7;
}

9. Border Color

The border color can be specified separately.

.box {
    border-color: #0b5ed7;
}

You can also use a shorthand declaration:

.box {
    border: 2px solid #0b5ed7;
}

10. Border Radius

The border-radius property rounds the corners of an element.

.card {
    border-radius: 12px;
}

A larger value creates more rounded corners.

A circular shape can be created when the element has equal width and height:

.avatar {
    width: 80px;
    height: 80px;
    border-radius: 50%;
}

11. Margin

Margin creates space outside an element.

.card {
    margin: 20px;
}

Unlike padding, margin does not create space inside the element. It creates separation between the element and surrounding elements.

12. Margin on Individual Sides

Each side can be controlled separately.

.card {
    margin-top: 20px;
    margin-right: 30px;
    margin-bottom: 20px;
    margin-left: 30px;
}

13. Margin Shorthand

Margin supports the same shorthand pattern as padding.

One value:

margin: 20px;

Two values:

margin: 20px 30px;

Three values:

margin: 10px 20px 30px;

Four values:

margin: 10px 20px 30px 40px;

The order remains:

top → right → bottom → left

14. Centering an Element with Margin

A block element with a defined width can often be centered horizontally using:

.container {
    width: 80%;
    margin-left: auto;
    margin-right: auto;
}

The shorthand version is:

.container {
    width: 80%;
    margin: 0 auto;
}

The auto margins allow the browser to distribute available horizontal space equally.

15. Width and the Box Model

Consider:

.box {
    width: 300px;
    padding: 20px;
    border: 5px solid black;
}

With the default content-box, the total width becomes:

Content width
+ left padding
+ right padding
+ left border
+ right border

Therefore:

300px
+ 20px
+ 20px
+ 5px
+ 5px
= 350px

The visible box becomes 350 pixels wide.

16. box-sizing: content-box

The default value of box-sizing is usually content-box.

.box {
    box-sizing: content-box;
}

With content-box, the declared width applies only to the content area.

17. box-sizing: border-box

With:

.box {
    box-sizing: border-box;
}

The declared width includes:

  • Content
  • Padding
  • Border

For example:

.box {
    width: 300px;
    padding: 20px;
    border: 5px solid black;
    box-sizing: border-box;
}

The total visible width remains 300 pixels.

18. The Universal Box-Sizing Rule

Many modern websites use:

* {
    box-sizing: border-box;
}

This makes sizing more predictable because declared widths and heights include padding and borders.

A commonly used variation is:

html {
    box-sizing: border-box;
}

*,
*::before,
*::after {
    box-sizing: inherit;
}

19. Height and the Box Model

The same box model principles apply to height.

.box {
    height: 200px;
    padding: 20px;
    border: 5px solid black;
}

With content-box, the padding and borders increase the overall height.

With border-box, they are included within the declared height.

20. Margin vs Padding

One of the most common beginner mistakes is confusing margin and padding.

Padding creates space inside an element.

Margin creates space outside an element.

Example:

.card {
    padding: 20px;
    margin: 30px;
}

The padding separates the content from the card’s border.

The margin separates the card from surrounding elements.

21. Visualizing Margin and Padding

        MARGIN
    +-------------+
    |   BORDER    |
    |  +-------+  |
    |  |PADDING|  |
    |  | +---+ |  |
    |  | | C | |  |
    |  | +---+ |  |
    |  +-------+  |
    +-------------+

Thinking about the box model visually makes CSS layout much easier to understand.

22. Margin Collapse

In normal block layout, vertical margins between some block elements can collapse rather than simply adding together.

For example:

.first {
    margin-bottom: 30px;
}

.second {
    margin-top: 20px;
}

The resulting vertical separation may be 30 pixels rather than 50 pixels because the margins can collapse.

Margin collapsing mainly affects vertical margins in normal block flow.

Understanding this behavior helps explain why some vertical spacing appears different from what you initially calculate.

23. Negative Margins

CSS permits negative margins.

.card {
    margin-top: -20px;
}

Negative margins can move elements closer together or cause overlap.

They should be used carefully because excessive use can make layouts difficult to understand and maintain.

24. Overflow

Sometimes content becomes larger than the box containing it. The overflow property controls what happens.

.box {
    width: 300px;
    height: 150px;
    overflow: hidden;
}

Common values include:

  • visible
  • hidden
  • scroll
  • auto

25. Overflow Auto

overflow: auto allows the browser to provide scrolling when necessary.

.content-box {
    max-height: 300px;
    overflow: auto;
}

This is useful for containers where the amount of content may vary.

26. Border Radius and Cards

Modern interfaces often combine padding, borders, radius and shadows.

.card {
    padding: 24px;
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    background: #ffffff;
}

This creates a clean card structure.

27. Box Shadows

Although shadows are not technically part of the box model dimensions, they are often used with borders and spacing to improve visual presentation.

.card {
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
}

Shadows should be subtle enough to maintain a clean interface.

28. Consistent Spacing

Professional websites often use a consistent spacing system.

For example:

:root {
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 24px;
    --space-6: 32px;
    --space-7: 48px;
}

Components can then reuse these values.

.card {
    padding: var(--space-5);
}

.section {
    margin-bottom: var(--space-7);
}

29. Box Model and Responsive Design

The box model becomes especially important when creating responsive layouts.

For example:

.container {
    width: 100%;
    max-width: 1100px;
    padding: 20px;
    margin: 0 auto;
}

Using max-width prevents content from becoming excessively wide on large screens.

30. Box Model and Width Calculations

Suppose we have:

.box {
    width: 400px;
    padding: 20px;
    border: 2px solid black;
}

With content-box:

Total width =
400 + 20 + 20 + 2 + 2

Total width = 444px

With border-box:

Total width = 400px

This difference is extremely important when designing multi-column layouts.

31. Practical Card Example

.course-card {
    width: 100%;
    max-width: 360px;
    padding: 24px;
    margin: 0 auto;
    border: 1px solid #dbe3ef;
    border-radius: 14px;
    background: #ffffff;
    box-sizing: border-box;
}

.course-card h3 {
    margin-top: 0;
    margin-bottom: 12px;
}

.course-card p {
    margin-top: 0;
    margin-bottom: 20px;
}

This example uses the box model to create predictable spacing inside and outside the card.

32. Practical Exercise

Create a webpage containing three course cards.

Each card should have:

  • A heading.
  • A paragraph.
  • A button.
  • A border.
  • Rounded corners.
  • Internal padding.
  • External spacing.

Then:

  1. Apply box-sizing: border-box.
  2. Give each card a maximum width.
  3. Use consistent padding.
  4. Use margin to separate cards.
  5. Experiment with border thickness.
  6. Change the border radius.
  7. Add a subtle box shadow.

33. Code Challenge

Build the following course card structure:

<article class="course-card">

    <span class="course-tag">
        Web Development
    </span>

    <h2>
        Become a Web Developer
    </h2>

    <p>
        Learn HTML, CSS and JavaScript
        through practical projects.
    </p>

    <a href="#" class="course-button">
        Start Learning
    </a>

</article>

Use CSS to create:

  • A maximum width of 400px.
  • 24px internal padding.
  • A 1px border.
  • 16px rounded corners.
  • 30px external spacing.
  • A clean background.
  • A subtle shadow.
  • A styled button.

34. Debugging Box Model Problems

When an element appears larger or smaller than expected, inspect the following:

  1. Is box-sizing set to content-box or border-box?
  2. Does the element have padding?
  3. Does it have a border?
  4. Does it have margin?
  5. Is its width fixed, percentage-based or constrained by max-width?
  6. Is content overflowing?
  7. Are margins collapsing?

Browser developer tools can display the box model visually and show the exact content, padding, border and margin values.

35. Lesson Summary

The CSS box model describes how the content, padding, border and margin of an element work together.

Padding creates internal space, while margin creates external space. Borders surround the padding and content areas.

The box-sizing property controls how declared width and height are calculated. Using border-box often makes layouts easier to control.

Margins, padding, borders, border radius and overflow are essential tools for building clean and responsive interfaces.

Once you understand the box model, you can predict how elements occupy space and troubleshoot many common CSS layout problems.

💻

Code Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Dwallnet CSS Box Model</title>

    <style>
        :root {
            --primary: #0b5ed7;
            --dark: #111827;
            --muted: #6b7280;
            --background: #f5f7fb;
            --surface: #ffffff;
            --border: #dbe3ef;
        }

        * {
            box-sizing: border-box;
        }

        body {
            margin: 0;
            padding: 40px 20px;
            font-family: Arial, sans-serif;
            color: var(--dark);
            background: var(--background);
        }

        .container {
            width: 100%;
            max-width: 1100px;
            margin: 0 auto;
        }

        .page-title {
            margin-bottom: 32px;
            text-align: center;
            color: var(--primary);
        }

        .course-grid {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 24px;
        }

        .course-card {
            width: 100%;
            padding: 24px;
            border: 1px solid var(--border);
            border-radius: 16px;
            background: var(--surface);
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.06);
        }

        .course-tag {
            display: inline-block;
            margin-bottom: 12px;
            padding: 6px 10px;
            border-radius: 999px;
            background: #e8f1ff;
            color: var(--primary);
            font-size: 13px;
            font-weight: 700;
        }

        .course-card h2 {
            margin-top: 0;
            margin-bottom: 12px;
        }

        .course-card p {
            margin-top: 0;
            margin-bottom: 24px;
            color: var(--muted);
            line-height: 1.7;
        }

        .course-button {
            display: inline-block;
            padding: 12px 18px;
            border-radius: 8px;
            background: var(--primary);
            color: white;
            font-weight: 700;
            text-decoration: none;
        }

        .course-button:hover {
            background: #063b8f;
        }

        @media (max-width: 850px) {
            .course-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>

<body>

    <main class="container">

        <h1 class="page-title">
            Dwallnet Training Courses
        </h1>

        <section class="course-grid">

            <article class="course-card">

                <span class="course-tag">
                    Web Development
                </span>

                <h2>
                    Become a Web Developer
                </h2>

                <p>
                    Learn HTML, CSS and JavaScript
                    through practical projects.
                </p>

                <a href="#" class="course-button">
                    Start Learning
                </a>

            </article>

            <article class="course-card">

                <span class="course-tag">
                    Data Analysis
                </span>

                <h2>
                    Master Data Analysis
                </h2>

                <p>
                    Learn how to transform raw data
                    into useful information.
                </p>

                <a href="#" class="course-button">
                    Start Learning
                </a>

            </article>

            <article class="course-card">

                <span class="course-tag">
                    Cyber Security
                </span>

                <h2>
                    Learn Cyber Security
                </h2>

                <p>
                    Understand the fundamentals of
                    digital security and protection.
                </p>

                <a href="#" class="course-button">
                    Start Learning
                </a>

            </article>

        </section>

    </main>

</body>
</html>
⭐

Key Takeaways

["Every HTML element can be understood as a CSS box.","The CSS box model consists of content, padding, border and margin.","Padding creates space inside an element.","Margin creates space outside an element.","Borders surround the content and padding areas.","Padding and margin support one, two, three and four-value shorthand syntax.","The order for four-value shorthand is top, right, bottom and left.","The border property combines width, style and color.","Border-radius creates rounded corners.","The default box-sizing behavior is content-box.","With border-box, declared width and height include padding and borders.","Using border-box often makes responsive layouts easier to control.","Margin can be used with auto to center block-level containers.","Vertical margins can collapse in normal block flow.","Overflow controls content that exceeds the dimensions of a box.","Consistent spacing systems make interfaces easier to design and maintain.","Browser developer tools can be used to inspect the box model."]

Enjoying this lesson?

Sign in to your Dwallnet Academy student account to track your learning progress and mark lessons as complete.

Sign In to Track Progress