Course

Web Development
  • 1.1 Introduction to the Web
  • 2.1 HTML Basics
  • 2.2 Text & Content
  • 2.3 Layout & Grouping
  • 2.4 Lists & Tables
  • 2.5 Media & Embedding
  • 2.6 Forms
  • 2.7 Semantic HTML
  • 2.8 Accessibility & SEO
  • 3.1 CSS Introduction
  • 3.2 CSS Syntax
  • 3.3 CSS Selectors
  • 3.4 Typography
  • 3.5 Colors & Backgrounds
  • 3.6 Text Styling
  • 3.7 Box Model
  • 3.8 Units
  • 3.9 Display & Positioning
  • 3.10 Layout Systems
  • 3.11 Responsive Design
  • 3.12 Animations
  • 3.13 Modern CSS
  • 3.14 Performance & Accessibility
  • 3.15 Tailwind CSS Basics
  • 4.1 Introduction to JavaScript
  • 4.2 Variables & Scope
  • 4.3 Data Types
  • 4.4 Type Conversion
  • 4.5 Operators
  • 4.6 Control Flow
  • 4.7 Loops
  • 4.8 Functions
  • 4.9 Advanced Functions
  • 4.10 Objects & Arrays
  • 4.11 The this Keyword

3.7 Box Model

Updated Jul 12, 2026

3.7 Box Model

Updated Jul 12, 2026

The CSS Box Model is one of the most important concepts in CSS.

Every HTML element is treated as a rectangular box.

Understanding the Box Model is essential for:

  • Layout Design
  • Spacing
  • Alignment
  • Responsive Design

What is the Box Model?

Every HTML element consists of four layers.

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

Structure:

Content
    ↓
Padding
    ↓
Border
    ↓
Margin

Content Area

The content area contains the actual content of an element.

Examples include:

  • Text
  • Images
  • Videos
  • Other HTML elements

Example:

<div>
    Hello World
</div>

Here, Hello World is the content.


Width & Height

The width and height properties define the size of the content area.

Width:

div {
    width: 300px;
}

Height:

div {
    height: 200px;
}

Together:

.card {

    width: 300px;

    height: 200px;

}

Percentage width:

.container {
    width: 100%;
}

Responsive width:

.container {

    width: 100%;

    max-width: 1200px;

}

This is a common approach for responsive layouts.


Padding

Padding is the space inside an element between the content and its border.

Border
   │
Padding
   │
Content

Example:

.box {

    padding: 20px;

}

Without padding:

+------------+
|Hello World |
+------------+

With padding:

+------------------+
|                  |
|  Hello World     |
|                  |
+------------------+

Individual Padding

.box {

    padding-top: 10px;

    padding-right: 20px;

    padding-bottom: 10px;

    padding-left: 20px;

}

Shorthand Padding

One value:

padding: 20px;

Two values:

padding: 10px 20px;
Top & Bottom → 10px
Left & Right → 20px

Four values:

padding: 10px 20px 30px 40px;
Top    → 10px
Right  → 20px
Bottom → 30px
Left   → 40px

Margin

Margin is the space outside an element.

It creates space between neighboring elements.

Element
   ↓
Margin
   ↓
Other Element

Example:

.card {

    margin: 20px;

}

Individual Margin

.card {

    margin-top: 10px;

    margin-right: 20px;

    margin-bottom: 10px;

    margin-left: 20px;

}

Shorthand Margin

margin: 20px;

Centering Elements

A common technique is:

.container {

    width: 500px;

    margin: 0 auto;

}

This centers the element horizontally.


Border

The border surrounds the content and padding.

Syntax:

border:
width
style
color;

Example:

.card {

    border: 2px solid black;

}

Border Components

Width:

border-width: 2px;

Style:

border-style: solid;

Color:

border-color: blue;

Common border styles:

  • solid
  • dashed
  • dotted
  • double
  • none

Examples:

border: 2px solid black;
border: 2px dashed black;
border: 2px dotted black;

Border Radius

The border-radius property creates rounded corners.

Example:

.card {

    border-radius: 10px;

}

Circle:

.avatar {

    width: 100px;

    height: 100px;

    border-radius: 50%;

}

Outline

An outline is a line drawn outside the border.

Unlike borders:

  • Border affects layout.
  • Outline does not.

Syntax:

outline:
width
style
color;

Example:

button {

    outline: 2px solid blue;

}

Visual:

Outline
   ↓
+-----------+
| Border    |
| Content   |
+-----------+

Border vs Outline

Border Outline
Takes space No extra space
Part of Box Model Outside Box Model
Supports border-radius Limited radius support
Common for layout Common for focus states

Example:

input:focus {

    outline: 3px solid blue;

}

Outlines are commonly used for accessibility.


Box Shadow

The box-shadow property adds shadow effects around elements.

Syntax:

box-shadow:
horizontal
vertical
blur
spread
color;

Example:

.card {

    box-shadow:
    0 4px 10px rgba(0,0,0,0.2);

}

Breakdown:

0px   → Horizontal Offset
4px   → Vertical Offset
10px  → Blur Radius
rgba  → Shadow Color

Light shadow:

box-shadow:
0 2px 5px rgba(0,0,0,0.1);

Medium shadow:

box-shadow:
0 5px 15px rgba(0,0,0,0.2);

Strong shadow:

box-shadow:
0 10px 30px rgba(0,0,0,0.3);

Multiple shadows:

box-shadow:

0 2px 5px rgba(0,0,0,0.1),

0 10px 20px rgba(0,0,0,0.2);

Box Sizing

Modern websites almost always use:

* {

    box-sizing: border-box;

}

Content Box (Default)

.box {

    width: 300px;

    padding: 20px;

}

Actual width:

300 + 20 + 20
= 340px

Border Box

.box {

    box-sizing: border-box;

}

Actual width:

300px Total
Padding Included

Modern Reset

* {

    margin: 0;

    padding: 0;

    box-sizing: border-box;

}

Complete Example

HTML:

<div class="card">

    Frontend Development

</div>

CSS:

.card {

    width: 300px;

    padding: 20px;

    margin: 20px;

    border: 1px solid #ddd;

    border-radius: 10px;

    box-shadow:
    0 4px 12px rgba(0,0,0,0.1);

}

Real-World Example

.card {

    background: white;

    width: 350px;

    padding: 24px;

    border-radius: 12px;

    box-shadow:
    0 4px 20px rgba(0,0,0,0.08);

}

This style is commonly used for:

  • Dashboards
  • E-commerce websites
  • Landing pages
  • SaaS applications

Box Model Best Practices

Use box-sizing: border-box.

* {

    box-sizing: border-box;

}

Use padding for internal spacing.

padding: 20px;

Use margin to separate elements.

margin: 20px;

Prefer subtle shadows.

box-shadow:
0 4px 12px rgba(0,0,0,0.1);

Use rounded corners for modern interfaces.

border-radius: 10px;

Key Takeaway

Every HTML element follows the Box Model.

Margin
   ↓
Border
   ↓
Padding
   ↓
Content

Example:

.card {

    width: 300px;

    padding: 20px;

    margin: 20px;

    border: 1px solid #ddd;

    box-shadow:
    0 4px 10px rgba(0,0,0,0.1);

}

Understanding the Box Model is essential for creating clean layouts, proper spacing, responsive designs, and modern user interfaces.

3.6 Text Styling3.8 Units