Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › CSS Snippets › Reference › padding

CSS Reference

padding

Controls the inner spacing of an element. The space between the content and the border.

Last updated: October 2026

Definition: The padding property controls the inner spacing of an element. It is the space between the element's content and its border. It sits inside the border and affects the element's own background color, making it visually distinct from margin.

🎬 Live Example

See how padding increases the space inside the blue box, showing the background color.

No padding
Content only
padding: 16px
Content with padding
padding: 32px
Content with more padding
padding: 32px 64px (vertical | horizontal)
Asymmetric padding

💡 Notice how the background color expands with the padding. This is the key difference from margin.

📝

Basic Syntax

.element {
    padding: 20px;              /* All sides */
}

.element {
    padding: 10px 20px;         /* Vertical | Horizontal */
}

.element {
    padding: 10px 20px 30px;    /* Top | Horizontal | Bottom */
}

.element {
    padding: 10px 20px 30px 40px;  /* Top | Right | Bottom | Left */
}

Same clockwise shorthand rules as margin. No negative values allowed.

🎯 All padding Properties

Property Purpose
paddingShorthand for all four sides.
padding-topTop padding only.
padding-rightRight padding only.
padding-bottomBottom padding only.
padding-leftLeft padding only.
1

The Shorthand Rules

/* 1 value: all sides */
padding: 10px;

/* 2 values: vertical | horizontal */
padding: 10px 20px;

/* 3 values: top | horizontal | bottom */
padding: 10px 20px 30px;

/* 4 values: top | right | bottom | left */
padding: 10px 20px 30px 40px;

Use the shorthand to reduce CSS. Use individual properties when only one side changes.

2

padding for Buttons (Accessibility)

Padding is critical for clickable elements. It increases the touch target size.

.btn {
    padding: 12px 24px;
    background: #3b82f6;
    color: white;
    border-radius: 8px;
    border: none;
    cursor: pointer;
}

💡 WCAG recommendation: Touch targets should be at least 44×44 pixels. Padding is the easiest way to achieve this on buttons and links.

3

box-sizing — The Critical Companion

By default, padding adds to the element's width. This breaks layouts.

/* Default: content-box (padding adds to width) */
.box {
    width: 300px;
    padding: 20px;
    /* Total width: 300px + 40px = 340px */
}

/* Better: border-box (padding included inside width) */
.box {
    box-sizing: border-box;
    width: 300px;
    padding: 20px;
    /* Total width: 300px */
}

Best practice: Always add *, *::before, *::after { box-sizing: border-box; } at the top of your CSS. This makes padding and border included in width declarations, which is almost always what you want.

4

Common padding Values

/* Remove default padding (lists, buttons) */
padding: 0;

/* Small button padding */
padding: 0.5rem 1rem;

/* Standard button padding */
padding: 0.75rem 1.5rem;

/* Card padding */
padding: 1.5rem;

/* Large section padding */
padding: 4rem 2rem;

/* Vertical rhythm */
padding: 1rem 0;

/* Asymmetric (top/bottom more than sides) */
padding: 2rem 1rem;

🎯 padding vs margin — Quick Reference

Feature padding margin
PositionInside borderOutside border
Affects backgroundYesNo
Negative allowedNoYes
CollapsesNoYes (vertical)
Increases click areaYesNo
Common useButtons, cards, containersSpace between elements

⚠️ Common Mistakes

♿ Accessibility Notes

❓ Frequently Asked Questions

What is the CSS padding property?

The padding property controls the inner spacing of an element — the space between the element's content and its border. It is inside the border and affects the element's own background color.

What is the difference between padding and margin?

Padding is inner space — inside the border, part of the element's box. Margin is outer space — outside the border, between the element and its neighbors. Padding affects background color; margin does not.

Does padding affect element size?

By default, yes. With box-sizing: content-box (the default), padding adds to the element's total width and height. With box-sizing: border-box, padding is included inside the declared width.

Can padding be negative?

No. Padding cannot be negative. Use negative margin instead if you need to pull content closer.

Should I use padding for buttons?

Yes. Padding increases the clickable area of a button, which is a both usability and accessibility win. Aim for at least 8px of padding on each side for touch targets.

🎯 What's Next?

← All CSS Reference