CSS padding Property

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 4 Examples
Layout & Box Model

What You’ll Learn

The padding property adds space inside an element, between its content and its border. It is a core box-model tool for readable layouts, buttons, cards, and any component that needs breathing room.

01

Inner Space

Inside the border.

02

Shorthand

1 to 4 values.

03

Side Properties

Top right bottom left.

04

Units

px, em, rem, %.

05

Box Model

Not margin.

06

Margin

Outer spacing pair.

Introduction

The padding property in CSS is used to generate space around an element’s content, inside of any defined borders. This space is often used to enhance the layout and overall design of a webpage, providing better readability and visual aesthetics.

The padding property can be applied to all HTML elements and can be set using various units like pixels, ems, percentages, and more.

Definition and Usage

Apply padding to any element that needs inner spacing: divs, paragraphs, buttons, links, cards, and form fields. Use single-value shorthand for equal spacing on all sides, two values for vertical and horizontal rhythm, or longhand side properties when each edge needs a different amount.

Padding increases the visible size of an element’s background and border box. Unlike margin, padding never collapses and always stays inside the element’s border.

💡
Beginner Tip

Padding creates space inside the border. Margin creates space outside the border. Use padding to keep text away from edges; use margin to separate one box from another.

📝 Syntax

The syntax for the padding property can be written in several ways, depending on which sides of the element you want to specify padding for:

syntax.css
element {
  padding: value; /* Applies to all four sides */
  padding-top: value;
  padding-right: value;
  padding-bottom: value;
  padding-left: value;
}

The value can be a length (like 10px, 1em, etc.), a percentage, or the keyword auto.

Shorthand Rules

  • Single value — Applies the same padding to all four sides.
  • Two values — First applies to top and bottom; second to left and right.
  • Three values — First to top; second to left and right; third to bottom.
  • Four values — Top, right, bottom, left (clockwise).

Basic Example

padding.css
.padded-box {
  padding: 20px;
  background-color: #f0f0f0;
  border: 1px solid #ccc;
}

⚡ Quick Reference

QuestionAnswer
Initial value0
Applies toAll elements except table-row-group, table-header-group, table-footer-group, table-row, table-column-group, and table-column
InheritedNo
AnimatableYes, as a length
Common useButtons, cards, content blocks, form fields, and readable text spacing

🎯 Default Value

The default value of the padding property is 0, meaning no padding is applied by default. Browsers may apply their own default padding to certain elements like <button> or <ul>, which you can reset or override with CSS.

💎 Property Values

ValueExampleDescription
Length unitspadding: 20px;Values such as px, em, rem, etc.
Percentagepadding: 5%;A percentage value relative to the width of the containing element.
autopadding: auto;Automatically calculates the padding, which is rarely used and behaves differently depending on the browser.
padding: 20px; padding: 10px 20px; padding-top: 1rem;

👀 Live Preview

Four boxes with the same content and different pad- utility classes showing increasing inner spacing:

pad-sm
pad-md
pad-lg
pad-xl

Examples Gallery

Apply uniform padding to a box, use two-value shorthand, set individual sides with longhand properties, and style button and card UI with comfortable inner spacing.

🔢 Basic Padding

Start with the reference example — equal padding on all sides of a content box.

Example 1 — Padded Box

In this example, we’ll apply padding to a <div> element to create space around its content.

padding-box.html
<style>
  .padded-box {
    padding: 20px;
    background-color: #f0f0f0;
    border: 1px solid #ccc;
  }
</style>

<div class="padded-box">
  This box has 20px of padding on all sides.
</div>
Try It Yourself

How It Works

The gray background fills the padding area, so you can see the 20px gap between the text and the border on every side.

Example 2 — Two-Value Shorthand

Set vertical and horizontal padding with padding: 10px 20px.

padding-shorthand.css
.note {
  padding: 10px 20px;
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 0.5rem;
}
Try It Yourself

How It Works

With two values, the first number applies to top and bottom, and the second applies to left and right. This is a common pattern for text blocks and alerts.

🛠 Side Properties & UI

Control each edge individually, then apply padding to real-world button and card components.

Example 3 — Individual Side Padding

Use padding-top, padding-right, padding-bottom, and padding-left when each side needs a different value.

padding-sides.css
.custom-pad {
  padding-top: 8px;
  padding-right: 24px;
  padding-bottom: 16px;
  padding-left: 32px;
  background: #dbeafe;
  border: 2px solid #2563eb;
}
Try It Yourself

How It Works

Longhand side properties override the shorthand padding value for that specific edge. Use them when one side needs extra room, such as left padding for an icon.

Example 4 — Button and Card UI

Padding is essential for clickable buttons and readable card layouts.

padding-ui.css
.card {
  padding: 1.25rem;
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 0.75rem;
}

.btn {
  padding: 0.6rem 1.25rem;
  background: #2563eb;
  color: #fff;
  border: none;
  border-radius: 0.5rem;
}
Try It Yourself

How It Works

Cards use generous padding so text does not touch the edges. Buttons use tighter vertical padding and wider horizontal padding for a comfortable tap target.

padding vs margin & side properties

Padding and margin are both spacing tools in the CSS box model, but they work in opposite directions. Use margin for outer spacing between elements. Use padding for inner spacing between content and the border.

The padding shorthand sets all four sides at once. When you need per-edge control, use padding-top, padding-right, padding-bottom, and padding-left. For logical writing-mode spacing, see padding-block next in the series.

padding-companion.css
/* Box model layers: content → padding → border → margin */
.card {
  padding: 1rem;       /* inside the border */
  margin: 1.5rem;      /* outside the border */
  border: 1px solid #e2e8f0;
}

/* Longhand side override */
.icon-label {
  padding: 0.5rem 1rem;
  padding-left: 2.5rem;
}

Padding in the Box Model

Every element is drawn as a box with these layers from inside out:

  • Content — the text, image, or child elements.
  • Padding — space inside the border around the content.
  • Border — the visible edge of the box.
  • Margin — transparent space outside the border that separates this box from others.
box-sizing matters

By default, padding adds to the element’s total width and height. With box-sizing: border-box, padding is included inside the width you set, which makes layout math easier.

♿ Accessibility

  • Size touch targets — Buttons and links need enough padding so they are easy to tap on mobile devices.
  • Improve readability — Text blocks benefit from padding so lines do not touch container edges.
  • Do not hide content — Very large padding on small screens can push important content out of view.
  • Keep focus visible — Padding adjusts inner spacing; it should not replace visible focus outlines on interactive elements.
  • Test zoomed layouts — Users who zoom in rely on comfortable padding to keep text readable.

🧠 How padding Works

1

You set inner spacing

Apply padding with one to four values, or use longhand side properties for precise control.

CSS rule
2

Browser adds space inside

Padding is inserted between the content edge and the border on each side you specify.

Box model
3

Background fills the gap

The element’s background color or image extends into the padding area, making inner spacing visible.

Rendering
=

Comfortable inner spacing

Content breathes inside cards, buttons, and text blocks without crowding the border.

Browser Compatibility

The padding property is supported in all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It has been a fundamental part of CSS for a long time, so compatibility issues are rare.

Universal · All browsers

Core inner spacing everywhere

padding is one of the most fundamental CSS properties. You can rely on it in any project.

99% Global browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Legacy & Chromium
Full support
Opera All versions · Modern & legacy
Full support
padding property 99% supported

Bottom line: Safe to use everywhere. For logical spacing in RTL layouts, see padding-block.

Conclusion

The padding property is an essential tool in web design, allowing developers to create space around an element’s content. It is versatile and can be applied to all HTML elements to improve layout and user experience.

By adjusting the padding, you can enhance the appearance of your content and ensure it is presented in a clean and organized manner. Experiment with different padding values and see how it affects your web design.

💡 Best Practices

✅ Do

  • Use padding to keep text and icons away from borders
  • Prefer rem or em for scalable inner spacing
  • Use two-value shorthand for vertical and horizontal rhythm
  • Combine padding with margin for inner and outer spacing
  • Set box-sizing: border-box when fixed widths include padding

❌ Don’t

  • Confuse padding with margin
  • Use excessive padding that breaks mobile layouts
  • Forget that percentage padding is based on container width
  • Rely on padding: auto for layout (use margin auto instead)
  • Remove all padding from buttons and make tap targets too small

Key Takeaways

Knowledge Unlocked

Five things to remember about padding

Use these points when spacing content inside elements.

5
Core concepts
0 02

Default 0

No spacing.

Default
TRBL 03

Shorthand

1 to 4 values.

Syntax
% 04

Units

px, em, rem, %.

Values
mg 05

Not margin

Inside vs out.

Box model

❓ Frequently Asked Questions

The padding property creates space inside an element, between its content and its border. It improves readability and gives components comfortable inner spacing.
The default value is 0, meaning no inner spacing is applied unless you set padding explicitly.
Padding adds space inside the border, between the content and the border edge. Margin adds space outside the border, between this element and neighboring elements.
Yes. Percentage padding is calculated relative to the width of the containing block, even for top and bottom padding.
Use the longhand side properties when you need different spacing on each edge, such as extra left padding for an icon label or tighter top padding in a compact card header.

Practice in the Live Editor

Open the HTML editor, try different padding values, and see how inner spacing changes your layout instantly.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful