CSS Padding: Complete Guide with Examples, Syntax & Box Model

Learn CSS Padding with simple examples. Understand padding syntax, shorthand, individual sides, units, box model, box-sizing, responsive padding, logical properties, and best practices.

CSS Padding

CSS padding is one of the most important concepts in web design. It controls the space between an element’s content and its border. Proper padding makes a webpage easier to read, improves visual balance, and creates comfortable space around text, images, buttons, forms, cards, and other elements.

Padding is part of the CSS box model. Understanding how it works is essential for creating clean, responsive, and professional web layouts.

What Is CSS Padding?

The CSS padding property is used to create space inside an element, between its content and its border.

For example:

.box {
  padding: 20px;
}

This adds 20px of space on all four sides of the content.

Consider a simple element:

<div class="box">Hello World</div>

.box {
  border: 2px solid black;
  padding: 20px;
}

The text remains inside the element, while the padding creates space between the text and the border.

A simple way to remember it is:

  • Content = the actual text, image, or other material
  • Padding = space around the content, inside the element
  • Border = the visible boundary around the element
  • Margin = space outside the element

CSS Padding Syntax

The basic syntax is:

selector {
  padding: value;
}

Example:

p {
  padding: 15px;
}

This applies 15px of padding to the top, right, bottom, and left sides.

The padding shorthand can also accept multiple values.

element {
  padding: top right bottom left;
}

Padding on All Four Sides

You can apply the same padding to every side:

.box {
  padding: 20px;
}

The result is:

  • Top: 20px
  • Right: 20px
  • Bottom: 20px
  • Left: 20px

This is the simplest and most commonly used form of padding.

Padding With Two Values

When two values are provided:

.box {
  padding: 10px 20px;
}

The first value applies to the top and bottom.

The second value applies to the left and right.

So this means:

Top:    10px
Right:  20px
Bottom: 10px
Left:   20px

This pattern is very useful for buttons and horizontal content areas.

For example:

button {
  padding: 10px 20px;
}

This creates vertical breathing room of 10px and horizontal breathing room of 20px.

Padding With Three Values

When three values are specified:

.box {
  padding: 10px 20px 30px;
}

They are interpreted as:

Top:    10px
Right:  20px
Bottom: 30px
Left:   20px

The left side receives the same value as the right side.

The order is:

top right/left bottom

Padding With Four Values

Four values allow you to specify every side separately:

.box {
  padding: 10px 20px 30px 40px;
}

The order is always:

top right bottom left

So:

Top:    10px
Right:  20px
Bottom: 30px
Left:   40px

A useful memory trick is to imagine moving around the element clockwise, starting from the top.

Individual Padding Properties

CSS also provides four separate properties for controlling each side.

padding-top

The padding-top property controls the space above the content.

.box {
  padding-top: 20px;
}

padding-right

The padding-right property controls the space between the content and the right border.

.box {
  padding-right: 25px;
}

padding-bottom

The padding-bottom property controls the space below the content.

.box {
  padding-bottom: 15px;
}

padding-left

The padding-left property controls the space between the content and the left border.

.box {
  padding-left: 30px;
}

You can combine these properties:

.box {
  padding-top: 10px;
  padding-right: 20px;
  padding-bottom: 15px;
  padding-left: 20px;
}

Padding Shorthand

Instead of writing four individual properties, you can use the padding shorthand.

For example:

.box {
  padding-top: 10px;
  padding-right: 20px;
  padding-bottom: 10px;
  padding-left: 20px;
}

Can be shortened to:

.box {
  padding: 10px 20px;
}

Using shorthand can make CSS shorter and easier to maintain.

Padding Values

Padding values can be specified using different CSS units.

Pixels

Pixels are commonly used when you want a predictable amount of spacing.

.box {
  padding: 20px;
}

Percentage

Percentages can also be used:

.box {
  padding: 5%;
}

For padding percentages, the percentage is calculated relative to the containing block’s inline size. This behavior is important when designing responsive layouts.

em

The em unit is relative to the font size of the relevant element.

.box {
  padding: 1em;
}

This can be useful when spacing should scale with text.

rem

The rem unit is relative to the root element’s font size.

.box {
  padding: 1rem;
}

rem is widely used in modern responsive CSS because it provides consistent spacing based on the document’s root font size.

Viewport Units

Viewport-related units can also be used:

.box {
  padding: 2vw;
}

or:

.box {
  padding: 2vh;
}

These can be useful in designs where spacing should respond to the viewport.

calc()

CSS calc() can combine values:

.box {
  padding: calc(10px + 1vw);
}

This can help create flexible spacing.

Padding and the CSS Box Model

Padding is an important part of the CSS box model.

The box model consists of:

Content
   ↓
Padding
   ↓
Border
   ↓
Margin

For example:

.box {
  width: 300px;
  padding: 20px;
  border: 5px solid black;
  margin: 30px;
}

With the default content-box box-sizing behavior, the declared width applies to the content area. Padding and border are then added to the rendered outer size.

For a simple horizontal calculation:

Content width = 300px
Left padding = 20px
Right padding = 20px
Left border = 5px
Right border = 5px

Total width = 350px

The margin is outside that box and adds additional surrounding space.

box-sizing and Padding

The box-sizing property changes how declared width and height interact with padding and borders.

The common modern approach is:

* {
  box-sizing: border-box;
}

With:

.box {
  width: 300px;
  padding: 20px;
  border: 5px solid black;
  box-sizing: border-box;
}

the declared 300px width includes the content, padding, and border.

This makes sizing easier to control.

A common global rule is:

*,
*::before,
*::after {
  box-sizing: border-box;
}

Padding vs Margin

Padding and margin are often confused, but they have different purposes.

Padding creates space inside the element.

Margin creates space outside the element.

Example:

.card {
  padding: 20px;
  margin: 30px;
}

Here:

  • padding creates space between the card’s content and its border.
  • margin creates space between the card and surrounding elements.

Padding is part of the element’s box, while margin is outside it.

Padding vs Border

Padding and border are also different.

.box {
  padding: 20px;
  border: 2px solid black;
}

The padding creates empty space around the content. The border creates the visible line around that space.

The basic structure is:

Content → Padding → Border → Margin

Padding and Backgrounds

One important feature of padding is that an element’s background normally extends through its padding area.

For example:

.card {
  background-color: lightblue;
  padding: 30px;
}

The blue background fills the content area and the padding area.

This is useful when creating cards, navigation links, buttons, banners, and highlighted sections.

Padding With Text

Padding can make text much easier to read.

Without sufficient padding:

p {
  border: 1px solid black;
}

The text may appear too close to the border.

Adding padding improves the appearance:

p {
  border: 1px solid black;
  padding: 15px;
}

The text now has comfortable internal space.

Padding for Buttons

Padding is commonly used to make buttons comfortable to click.

button {
  padding: 10px 20px;
}

For a button containing text, horizontal padding is often larger than vertical padding.

For example:

button {
  padding: 12px 24px;
}

This can produce a more balanced button.

Padding affects the clickable area because it increases the element’s box.

Padding for Cards

Cards often need internal spacing:

.card {
  padding: 24px;
  border: 1px solid #ddd;
  border-radius: 8px;
}

This keeps headings, paragraphs, images, and buttons from touching the card edges.

Padding for Navigation

Padding is often used in navigation links:

nav a {
  padding: 10px 15px;
}

This creates a larger and more comfortable interactive area than relying only on the text itself.

Padding for Forms

Form controls can use padding to improve readability and usability.

input {
  padding: 10px 12px;
}

For a search field:

input[type="search"] {
  padding: 12px;
}

Padding gives the input text room to breathe and can make controls easier to use.

Padding for Images

Padding can be used around images:

img {
  padding: 10px;
}

If a background or border is applied to the image element, the padding becomes visible around the image.

However, padding is not the same as an image’s internal whitespace. It changes the CSS box around the replaced element.

Negative Padding Is Not Allowed

Unlike margins, padding cannot have negative values.

This is invalid:

.box {
  padding: -10px;
}

CSS padding values must not be negative.

If you need to move or overlap an element, other techniques such as margins, transforms, positioning, or layout properties may be appropriate.

Padding With Zero

You can explicitly remove padding:

.box {
  padding: 0;
}

This is useful for resetting default or previously applied spacing.

You can also reset individual sides:

.box {
  padding-top: 0;
}

Padding With auto

Unlike margin, padding does not accept auto as a valid value.

For example, this does not work as a way to automatically distribute padding:

.box {
  padding: auto;
}

If you need automatic space distribution, look at properties such as margin, Flexbox, or Grid.

Logical Padding Properties

Modern CSS provides logical properties that are especially useful for internationalized and writing-mode-aware layouts.

Instead of:

padding-left: 20px;
padding-right: 20px;

you can use:

padding-inline: 20px;

Instead of:

padding-top: 20px;
padding-bottom: 20px;

you can use:

padding-block: 20px;

There are also individual logical properties:

padding-inline-start
padding-inline-end
padding-block-start
padding-block-end

These properties are useful because they refer to logical directions rather than always meaning physical left, right, top, and bottom.

For example:

.card {
  padding-inline: 20px;
  padding-block: 15px;
}

padding-inline

The padding-inline shorthand controls padding at the start and end of the inline direction.

.box {
  padding-inline: 20px;
}

In a typical left-to-right horizontal writing mode, this corresponds to left and right padding.

padding-block

The padding-block shorthand controls padding at the start and end of the block direction.

.box {
  padding-block: 15px;
}

In a typical horizontal writing mode, this corresponds to top and bottom padding.

Responsive Padding

Padding should work well on different screen sizes.

A fixed value can be useful:

.container {
  padding: 20px;
}

But you may want different spacing on smaller screens:

.container {
  padding: 40px;
}

@media (max-width: 600px) {
  .container {
    padding: 20px;
  }
}

This prevents excessive spacing on small screens.

Fluid Padding With clamp()

Modern CSS provides clamp() for flexible values.

.container {
  padding: clamp(16px, 4vw, 48px);
}

This means the padding can grow with the viewport while remaining within a minimum and maximum range.

It is useful for responsive websites because it can reduce the need for many media queries.

Padding in Flexbox

Padding can be applied to a flex container:

.container {
  display: flex;
  padding: 20px;
}

The padding creates space between the flex container’s content area and its edges.

Remember that padding and gap solve different problems.

.container {
  display: flex;
  gap: 20px;
  padding: 20px;
}

Here:

  • padding creates space between the container edge and its children.
  • gap creates space between flex items.

Padding in CSS Grid

Padding also works naturally with Grid:

.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  padding: 20px;
}

Again, padding controls space around the grid content, while gap controls space between grid items.

Padding and Height

Padding can affect the rendered height of an element.

For example:

.box {
  height: 100px;
  padding: 20px;
}

With the default content-box, the content height is 100px, and the vertical padding is added outside the content height.

With:

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

the declared height includes the padding and border.

This distinction is especially important when working with fixed-height components.

Padding and Width

The same principle applies to width.

With content-box:

.box {
  width: 300px;
  padding: 20px;
}

the padding adds to the declared content width.

With:

.box {
  width: 300px;
  padding: 20px;
  box-sizing: border-box;
}

the padding is included within the declared width.

Padding and Overflow

Large padding can contribute to an element becoming larger than expected.

For example:

.box {
  width: 200px;
  padding: 100px;
}

This may produce a much larger rendered box when box-sizing is content-box.

Using:

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

can make sizing more predictable.

Common Padding Mistakes

Confusing Padding With Margin

A common mistake is using margin when internal space is needed.

If the space should appear inside a box, use padding.

.card {
  padding: 20px;
}

If the space should separate the card from another element, use margin.

.card {
  margin: 20px;
}

Using Too Much Padding

Excessive padding can make a page feel sparse and waste valuable screen space.

For example:

.card {
  padding: 100px;
}

may be excessive for a small card.

Choose spacing based on the component and overall design.

Using Too Little Padding

Very small padding can make text feel cramped:

button {
  padding: 1px;
}

A button may become difficult to use comfortably.

Forgetting Box Sizing

Unexpected dimensions can occur when padding is added to fixed widths or heights.

Using:

*,
*::before,
*::after {
  box-sizing: border-box;
}

is a common way to make sizing more predictable.

Overusing Fixed Values

Using many unrelated pixel values can make a design inconsistent.

A better approach is to establish a spacing system.

For example:

:root {
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
}

Then:

.card {
  padding: var(--space-5);
}

This helps maintain consistency.

CSS Padding Best Practices

Good padding is not simply about adding more space. It should support readability, usability, hierarchy, and visual balance.

Useful practices include:

  1. Use padding to create internal breathing room.
  2. Use margin for external spacing.
  3. Use shorthand when it improves readability.
  4. Use box-sizing: border-box when predictable dimensions are important.
  5. Use responsive values for layouts that change across screen sizes.
  6. Consider rem or design tokens for consistent spacing.
  7. Use logical properties when supporting different writing directions.
  8. Avoid excessive padding.
  9. Give interactive controls enough internal space.
  10. Keep spacing consistent throughout the design.

Practical Example

Here is a simple card using several padding concepts:

<div class="card">
  <h2>CSS Padding</h2>
  <p>
    Padding creates space between an element's content and its border.
  </p>
  <button>Learn More</button>
</div>

.card {
  max-width: 400px;
  padding: 24px;
  border: 1px solid #ddd;
  border-radius: 10px;
}

.card h2 {
  margin-top: 0;
}

.card button {
  padding: 10px 20px;
}

The card has internal spacing, while the button has its own padding.

CSS Padding Example With Responsive Design

.container {
  padding: clamp(16px, 4vw, 48px);
}

.card {
  padding: clamp(16px, 3vw, 32px);
}

This allows padding to adapt to the available viewport size while staying within sensible limits.

CSS Padding Example With Logical Properties

.card {
  padding-block: 20px;
  padding-inline: 24px;
}

This is often preferable to hard-coding physical directions when the layout may support different writing modes.

Padding and Accessibility

Padding can improve accessibility when used correctly.

Interactive elements such as buttons and links should have enough space around their text so they are easier to interact with. Padding can also help create larger clickable areas.

For example:

a {
  padding: 10px 14px;
}

However, padding alone does not guarantee that an interface meets every accessibility requirement. Interactive controls should be evaluated as part of the complete design, including font size, contrast, spacing between controls, keyboard accessibility, and touch usability.

Padding and Performance

Padding itself has very little performance cost in normal CSS use. The main concern is not performance but layout behavior.

Extremely complicated layouts with many nested elements and unnecessary styling can make maintenance harder. A consistent spacing system usually results in cleaner CSS and easier development.

Frequently Asked Questions

What is padding in CSS?

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

What is the difference between padding and margin?

Padding creates space inside an element. Margin creates space outside an element.

How do I add padding to all sides?

Use:

.box {
  padding: 20px;
}

How do I set different padding for each side?

Use four values:

.box {
  padding: 10px 20px 30px 40px;
}

The order is top, right, bottom, and left.

Can CSS padding be negative?

No. Negative padding values are not valid.

Can padding use percentages?

Yes. Padding can use percentage values, and the percentage is based on the containing block’s inline size.

Does padding increase an element’s size?

It can. With box-sizing: content-box, padding is added outside the declared content width or height. With box-sizing: border-box, padding is included within the declared dimensions.

Can padding be used with Flexbox?

Yes. Padding can be applied to both flex containers and flex items.

Can padding be used with CSS Grid?

Yes. Padding works with Grid containers and Grid items.

What is the shorthand order for padding?

The four-value order is:

top right bottom left

What happens with two padding values?

For:

padding: 10px 20px;

10px applies to top and bottom, while 20px applies to left and right.

What happens with three padding values?

For:

padding: 10px 20px 30px;

the values mean:

top = 10px
left/right = 20px
bottom = 30px

What is padding-inline?

padding-inline controls padding along the inline axis. It is useful for writing-mode-aware layouts.

What is padding-block?

padding-block controls padding along the block axis.

Final Thoughts

CSS padding is a simple property, but it plays a major role in professional web design. It creates internal breathing room, improves readability, makes buttons and form controls more comfortable to use, and helps organize visual layouts.

The most important points to remember are:

padding: 20px;

adds the same padding to all four sides,

padding: 10px 20px;

sets vertical and horizontal padding,

and:

padding: 10px 20px 30px 40px;

sets padding individually in the order top, right, bottom, left.

For modern responsive layouts, also consider box-sizing, logical properties such as padding-inline and padding-block, and flexible functions such as clamp().

When used thoughtfully, padding helps turn a technically correct webpage into a comfortable, readable, and visually balanced interface.

Scroll to Top