Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape

CSS Box Model

CSS Box Model

CSS CODES & EXAMPLES

CSS Box Model

Understand how content, padding, border and margin work together to control the size, spacing and layout of every HTML element.

01

What is the CSS Box Model?

The foundation of spacing and element dimensions

Every HTML element is treated as a rectangular box by CSS. The CSS Box Model consists of four main areas: Content, Padding, Border and Margin.

01

Content

The actual text, image or other content inside the element.

02

Padding

Space between the content and the border.

03

Border

The visible line surrounding the padding and content.

04

Margin

Space outside the border that separates elements.

02

Box Model Structure

Visual representation

MARGIN
BORDER
PADDING
CONTENT
03

Content

The innermost part of the box

content.css
.box {
  width: 300px;
  height: 150px;
}
CONTENT AREA
04

Padding

Space inside the element

Padding creates space between the content and the border. It increases the space inside an element.

padding.css
.box {
  padding: 30px;
}
CONTENT
05

Padding on Individual Sides

Control top, right, bottom and left spacing

padding-sides.css
.box {
  padding-top: 20px;
  padding-right: 30px;
  padding-bottom: 20px;
  padding-left: 30px;
}
TOP
RIGHT
BOTTOM
LEFT
CONTENT
06

Border

Draw a boundary around an element

border.css
.box {
  border: 3px solid #1769d5;
}
BORDER
08

Border Radius

Create rounded corners

border-radius.css
.box {
  border-radius: 20px;
}
5px
15px
30px
50%
09

Margin

Space outside an element

Margin creates space outside the border and separates an element from surrounding elements.

margin.css
.box {
  margin: 30px;
}
MARGIN
ELEMENT
10

margin: auto

Center a block element horizontally

margin-auto.css
.box {
  width: 300px;
  margin: 0 auto;
}
CENTERED ELEMENT
11

Margin on Individual Sides

Set spacing separately

margin-sides.css
.box {
  margin-top: 20px;
  margin-right: 30px;
  margin-bottom: 20px;
  margin-left: 30px;
}
ELEMENT
12

Margin & Padding Shorthand

Write multiple sides in a shorter form

shorthand.css
/* 1 value */
.box {
  padding: 20px;
}

/* 2 values */
.box {
  padding: 20px 30px;
}

/* 3 values */
.box {
  padding: 10px 20px 30px;
}

/* 4 values */
.box {
  padding: 10px 20px 30px 40px;
}
1 Value All sides
2 Values Top/Bottom • Left/Right
3 Values Top • Left/Right • Bottom
4 Values Top • Right • Bottom • Left
13

box-sizing

Control how width and height are calculated

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

.box {
  width: 300px;
  padding: 30px;
  border: 5px solid #1769d5;
}
content-box Default box model
border-box Width includes padding + border
14

Total Box Size

Understand the mathematical calculation

Element Width Content + Left Padding + Right Padding + Left Border + Right Border
Element Height Content + Top Padding + Bottom Padding + Top Border + Bottom Border
calculation.css
.box {
  width: 300px;
  padding: 20px;
  border: 5px solid black;
}

/*
Total width with content-box:

300 + 20 + 20 + 5 + 5
= 350px
*/
See also  HTML Semantic Elements
15

Practical Course Card

Box Model in a real website component

C

Computer Fundamentals

Learn computer basics, hardware, software, memory, operating systems and applications.

View Course →
W

Web Designing

Learn HTML, CSS, responsive layouts and practical website development.

View Course →
T

Tally Prime

Learn accounting, GST, inventory and practical business applications.

View Course →
16

Quick Reference

Important Box Model properties

PropertyExamplePurpose
widthwidth: 300px;Set content width
heightheight: 200px;Set content height
paddingpadding: 20px;Space inside the box
borderborder: 1px solid;Boundary around the box
marginmargin: 20px;Space outside the box
border-radiusborder-radius: 10px;Rounded corners
box-sizingborder-boxIncludes padding and border in declared size
17

Practice Task

Build a professional course card

🎯 Practice: Course Card

Create a course card using the CSS Box Model.

  • Set width to 320px.
  • Add padding: 25px;.
  • Add a 2px border.
  • Use border-radius: 15px;.
  • Add margin: 20px;.
  • Use box-sizing: border-box;.
  • Add a course title, description and button.
18

CSS Box Model Live Example

Run the practical example

LIVE PREVIEW
`;document.getElementById( "sncecBMPreview" ).srcdoc = code;}document.addEventListener( "DOMContentLoaded", function(){sncecBMRun();} );

HTML Buttons

  • 17/08/2026

HTML Div & Span

  • 17/08/2026

CSS Borders

  • 17/08/2026

Leave a Reply

Your email address will not be published. Required fields are marked *