Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape

CSS Padding

CSS Padding

CSS CODES & EXAMPLES

CSS Padding

Learn how to control the space inside HTML elements using CSS padding. Understand individual padding sides, shorthand values, responsive spacing, box sizing and practical website layouts.

01

What is CSS Padding?

Control the space inside an element

CSS padding creates space between an element’s content and its border. Unlike margin, which creates space outside an element, padding creates space inside the element.

Important Properties:padding padding-top padding-right padding-bottom padding-left
02

Basic Padding

Add equal space on all four sides

basic-padding.css
.box {
  padding: 30px;
}
padding: 30px
03

Individual Padding Sides

Control each side independently

You can define padding separately for the top, right, bottom and left sides.

individual-padding.css
.box {
  padding-top: 20px;
  padding-right: 30px;
  padding-bottom: 40px;
  padding-left: 50px;
}
TOP
20px
RIGHT
30px
BOTTOM
40px
LEFT
50px
CONTENT
04

Padding Shorthand — Two Values

Vertical and horizontal padding

padding-two-values.css
.box {
  padding: 20px 40px;
}
20px Top & Bottom
40px Left & Right

With two values, the first value applies to top and bottom, while the second applies to left and right.

05

Padding Shorthand — Three Values

Top, horizontal and bottom padding

padding-three-values.css
.box {
  padding: 10px 30px 50px;
}
10pxTop
30pxLeft & Right
50pxBottom
07

Padding vs Margin

Understand the difference

P

Padding

Creates space inside the element, between content and border.

padding: 20px;
M

Margin

Creates space outside the element, separating it from other elements.

margin: 20px;
08

Padding and the CSS Box Model

Understand content, padding, border and margin

Margin
Border
Padding
Content
box-model.css
.box {
  width: 300px;
  padding: 30px;
  border: 5px solid #1769d5;
  margin: 20px;
}
09

box-sizing: border-box

Make layouts easier to control

By default, padding and border can increase the rendered size of an element. With box-sizing: border-box;, the declared width and height include the padding and border.

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

.box {
  width: 300px;
  padding: 30px;
  border: 5px solid #1769d5;
}
300px Width
Padding Included
10

Padding in Buttons

Create comfortable clickable areas

button-padding.css
.btn {
  padding: 12px 24px;
  background: #1769d5;
  color: white;
  border-radius: 8px;
}
12

Padding in Navigation

Improve navigation links and clickable areas

navigation-padding.css
.nav-link {
  padding: 10px 15px;
  text-decoration: none;
}
13

Responsive Padding

Adjust inner spacing for mobile devices

responsive-padding.css
.section {
  padding: 60px 30px;
}

@media (max-width: 600px) {
  .section {
    padding: 35px 15px;
  }
}
Desktop Padding
Mobile Padding
14

Padding with Percentage

Use percentage values for flexible spacing

percentage-padding.css
.box {
  padding: 5%;
}
padding: 5%
15

Padding with CSS Variables

Create reusable spacing values

padding-variable.css
:root {
  --space: 25px;
}

.card {
  padding: var(--space);
}
–space: 25px

Reusable Spacing

CSS variables make spacing systems easier to maintain.

16

CSS Padding Live Preview

Run a complete padding example

LIVE PREVIEW
17

CSS Padding Quick Reference

Important properties at a glance

PropertyExamplePurpose
paddingpadding: 20px;Set padding on all sides
padding-toppadding-top: 20px;Space inside at the top
padding-rightpadding-right: 20px;Space inside on the right
padding-bottompadding-bottom: 20px;Space inside at the bottom
padding-leftpadding-left: 20px;Space inside on the left
box-sizingbox-sizing: border-box;Include padding and border in size
Responsive Padding@media (...)Change spacing on smaller screens
`;document.getElementById( "sncecPaddingPreview" ).srcdoc = code;}document.addEventListener( "DOMContentLoaded", function(){sncecPaddingRun();} );

HTML Paragraph

  • 17/08/2026

HTML Div & Span

  • 17/08/2026

HTML Runner

  • 16/08/2026

Leave a Reply

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