CSS Padding and Box-Sizing: Complete Guide with Examples

Learn CSS padding and box-sizing with easy examples. Understand padding values, the CSS box model, content-box, border-box, sizing, responsive layouts, and common mistakes.

CSS Padding and box-sizing: A Complete Guide

CSS padding and the box-sizing property are two important concepts for controlling the size, spacing, and layout of elements on a webpage. They are simple to use, but they can sometimes cause confusion when an element becomes larger than expected.

Understanding how padding affects an element’s dimensions and how box-sizing changes the box model will help you create cleaner, more predictable layouts.

What Is CSS Padding?

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

Think of an HTML element as a box. The content is inside the box, padding creates space around that content, the border surrounds the padding, and the margin creates space outside the border.

The basic structure is:

Margin
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚           Border              β”‚
 β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
 β”‚   β”‚       Padding         β”‚   β”‚
 β”‚   β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚   β”‚
 β”‚   β”‚   β”‚    Content    β”‚   β”‚   β”‚
 β”‚   β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚   β”‚
 β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Padding is mainly used to make content more comfortable to read and to create internal spacing within elements.

For example:

.box {
  padding: 20px;
}

This adds 20 pixels of padding to all four sides of the element.

Why Is Padding Important?

Without padding, text and other content can sit directly against an element’s border.

For example:

.card {
  border: 1px solid #ccc;
}

The content may appear too close to the border.

Adding padding improves the appearance:

.card {
  border: 1px solid #ccc;
  padding: 20px;
}

Padding is commonly used in:

  • Cards
  • Buttons
  • Navigation menus
  • Forms
  • Input fields
  • Sections
  • Articles
  • Tables
  • Alerts
  • Headers
  • Footers
  • Sidebars

Good padding can improve both visual design and readability.

CSS Padding Syntax

The padding property is a shorthand property.

selector {
  padding: value;
}

For example:

.box {
  padding: 15px;
}

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

Individual Padding Properties

CSS provides four separate padding properties:

padding-top
padding-right
padding-bottom
padding-left

For example:

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

Each side can have a different value.

Padding With Four Values

When four values are used with the shorthand property, they are applied in this order:

top β†’ right β†’ bottom β†’ left

Example:

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

This means:

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

A useful way to remember the order is TRBL:

Top, Right, Bottom, Left.

Padding With Three Values

When three values are used:

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

The values mean:

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

The left side uses the right-side value.

Padding With Two Values

When two values are used:

.box {
  padding: 10px 20px;
}

The first value applies to the top and bottom.

The second value applies to the left and right.

So:

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

This is very common in web design.

For example:

.button {
  padding: 10px 20px;
}

This creates vertical and horizontal space inside the button.

Padding With One Value

One value applies to all four sides:

.box {
  padding: 20px;
}

It is equivalent to:

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

Padding Units

Padding can use different CSS units.

Pixels

.box {
  padding: 20px;
}

Pixels provide a fixed size.

Percentages

.box {
  padding: 5%;
}

Percentage padding is calculated relative to the containing block’s width in the traditional CSS box model.

Relative Units

You can also use:

.box {
  padding: 1rem;
}

Other possible units include:

em
rem
vw
vh

For responsive interfaces, rem is often a convenient choice because it scales relative to the root font size.

Can Padding Be Negative?

No. CSS padding values cannot be negative.

This is invalid:

.box {
  padding: -10px;
}

If you need to move an element or create space outside it, margin or another layout technique may be more appropriate.

Padding and the CSS Box Model

To understand box-sizing, you first need to understand the CSS box model.

Every element is represented as a rectangular box consisting of:

  1. Content
  2. Padding
  3. Border
  4. Margin

The content area contains the actual text, image, or other child content.

Padding surrounds the content.

Border surrounds the padding.

Margin creates external space around the element.

The Default box-sizing Value

The default value of box-sizing is:

content-box

With content-box, the width and height properties normally apply only to the content area.

Padding and borders are then added outside those dimensions.

For example:

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

The content width is 300px.

There are 20px of padding on both the left and right:

20px + 300px + 20px = 340px

The borders add another 5px on each side:

5px + 340px + 5px = 350px

So the total horizontal size is:

350px

This behavior is one of the most common sources of unexpected element sizes in CSS.

What Is box-sizing?

The box-sizing property controls how an element’s declared width and height are calculated.

Its main values are:

content-box
border-box

There is also a global value:

inherit

The two important choices are content-box and border-box.

box-sizing: content-box

This is the default behavior.

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

With content-box, the declared width refers to the content area.

Consider:

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

The total width becomes:

300px content
+ 40px padding
+ 10px border
= 350px

This means the actual rendered box is wider than the declared width.

box-sizing: border-box

With border-box, the declared width includes the content, padding, and border.

Example:

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

The total width remains:

300px

The browser calculates the available content width after accounting for padding and borders.

The calculation is effectively:

300px total width
- 40px padding
- 10px border
= 250px content width

This makes element dimensions much easier to control.

content-box vs border-box

The difference can be summarized as follows:

Propertycontent-boxborder-box
Width includes contentYesYes
Width includes paddingNoYes
Width includes borderNoYes
Default valueYesNo
Easier for predictable layoutsSometimesUsually

Why Developers Often Use border-box

Many developers prefer:

* {
  box-sizing: border-box;
}

This makes the declared width and height include padding and borders.

For example:

.card {
  width: 100%;
  padding: 20px;
  border: 1px solid;
}

With border-box, the card can remain within its parent width instead of becoming wider because of padding and borders.

This is especially useful for:

  • Responsive layouts
  • Grid systems
  • Forms
  • Cards
  • Navigation bars
  • Columns
  • Input fields
  • Components with fixed dimensions

The Common Global Box-Sizing Pattern

A common CSS pattern is:

html {
  box-sizing: border-box;
}

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

This approach makes the box-sizing value inherit from the root element.

Another simpler approach is:

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

Both patterns are widely used.

The inheritance-based pattern can be particularly convenient when you want components or third-party elements to inherit the same box-sizing behavior.

Padding and Width: A Practical Example

Consider a container:

.container {
  width: 500px;
  padding: 30px;
}

With the default content-box, the total width is:

500px + 30px + 30px
= 560px

Now add:

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

The total width becomes:

500px

The content area becomes:

500px - 60px
= 440px

This difference becomes particularly important when working with percentage widths.

Padding With width: 100%

A common problem occurs with:

input {
  width: 100%;
  padding: 12px;
}

With content-box, the input’s content width is 100% of its container, and padding is added afterward. This can cause the input to extend beyond its parent.

Using:

input {
  width: 100%;
  padding: 12px;
  box-sizing: border-box;
}

keeps the total width at 100%.

This is one reason border-box is so useful for forms.

Padding and Responsive Design

Padding plays an important role in responsive design.

A fixed padding value can work well:

.card {
  padding: 20px;
}

But on very small screens, large padding may consume too much available space.

You can adjust padding with media queries:

.card {
  padding: 30px;
}

@media (max-width: 600px) {
  .card {
    padding: 15px;
  }
}

You can also use responsive CSS functions such as clamp():

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

This allows the padding to grow and shrink within a defined range.

Padding and calc()

CSS calc() can be used to create calculated padding values.

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

This can be useful when designing fluid layouts.

Padding and Logical Properties

Modern CSS also provides logical padding properties.

Instead of relying only on physical directions such as left and right, you can use:

padding-block
padding-inline

For example:

.box {
  padding-block: 20px;
  padding-inline: 30px;
}

This means:

  • padding-block: spacing in the block direction
  • padding-inline: spacing in the inline direction

You can also use:

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

Logical properties are useful for layouts that need to support different writing directions.

Padding vs Margin

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

Padding creates space inside an element.

Margin creates space outside an element.

For example:

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

Here:

  • padding separates the content from the card’s border.
  • margin separates the card from surrounding elements.

A simple rule to remember is:

Padding is internal space. Margin is external space.

Padding vs Border

Padding and border are also different.

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

Padding creates empty space around the content.

The border creates a visible boundary around the padding.

Padding Does Not Inherit

The padding property is not inherited by default.

For example:

.parent {
  padding: 20px;
}

A child element does not automatically receive 20px padding.

If inheritance is specifically desired, it can be requested:

.child {
  padding: inherit;
}

Padding and Backgrounds

An element’s background normally extends through the padding area.

For example:

.box {
  background: lightgray;
  padding: 30px;
}

The background appears behind the content and padding.

The exact background coverage can also be controlled with background-clip.

For example:

.box {
  background-clip: content-box;
}

This can make the background painting area behave differently from the default.

Padding With Images

Padding is often used around images:

img {
  padding: 10px;
}

If a border is added:

img {
  padding: 10px;
  border: 1px solid #ccc;
}

the padding creates space between the image content and border.

However, remember that padding changes the element’s dimensions under content-box.

Padding and box-sizing With Images

You can make image dimensions easier to control:

img {
  box-sizing: border-box;
  max-width: 100%;
  height: auto;
}

If padding is also added, border-box ensures that the declared width includes the padding and border.

Padding and Form Controls

Form controls are a common place where padding matters.

For example:

input,
textarea,
select {
  box-sizing: border-box;
  padding: 10px 12px;
  width: 100%;
}

This makes the controls easier to align and prevents padding from unexpectedly increasing their total width.

Buttons also benefit from padding:

button {
  padding: 10px 18px;
}

Padding makes the clickable area larger and can improve usability.

Padding and Buttons

Instead of forcing a button to have a fixed height, padding is often a better approach:

button {
  padding: 10px 20px;
}

This allows the button’s height to naturally adapt to its text and font size.

For example:

button {
  padding: 0.7rem 1.2rem;
}

This creates vertical and horizontal internal space without unnecessarily fixing the button’s dimensions.

Padding and Text

Padding can improve text readability by preventing text from touching an element’s edges.

For example:

.article {
  padding: 24px;
}

This is particularly useful for:

  • Articles
  • Blog posts
  • Cards
  • Notifications
  • Quotes
  • Sidebars

Padding and Flexbox

Padding can be used on a flex container:

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

It creates space between the container’s content area and its edges.

Padding is different from gap.

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

Here:

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

Padding and Grid

The same concept applies to CSS Grid:

.grid {
  display: grid;
  gap: 20px;
  padding: 20px;
}

The padding creates an inner boundary around the grid content, while gap controls spacing between grid items.

Padding and box-sizing in a Card Layout

Consider:

.card {
  width: 300px;
  padding: 20px;
  border: 1px solid #ccc;
}

With content-box, the total width is larger than 300px.

Using:

.card {
  box-sizing: border-box;
  width: 300px;
  padding: 20px;
  border: 1px solid #ccc;
}

keeps the complete card at 300px wide.

This is usually easier when designing a collection of cards that need consistent dimensions.

Padding and height

The same principle applies vertically.

With:

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

and content-box, the padding is added to the declared height.

The total height becomes:

200px + 40px = 240px

With:

.box {
  box-sizing: border-box;
  height: 200px;
  padding: 20px;
}

the total height remains 200px.

Padding and Overflow

Large padding can affect available content space.

For example:

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

Depending on box-sizing, the content area may become very small or the overall element may become much larger than expected.

If content cannot fit inside the available box, the overflow property may become relevant:

.box {
  overflow: auto;
}

However, overflow should not be used simply to hide poor sizing decisions. It is better to understand the box model first.

Padding With box-sizing: border-box and Zero Width

A useful feature of border-box is that padding and borders are included in the declared dimensions.

For example:

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

The box’s total width is still 0px, although its content area cannot provide negative space. This illustrates why border-box changes how available space is distributed between content, padding, and borders.

Common Mistakes With Padding

Mistake 1: Forgetting the Box Model

A developer may write:

.box {
  width: 100%;
  padding: 20px;
}

and wonder why it appears wider than its parent.

The solution is often:

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

Mistake 2: Confusing Padding With Margin

If the goal is to create space between two separate elements, margin or gap may be appropriate.

If the goal is to create space between content and the element’s edge, padding is usually appropriate.

Mistake 3: Using Fixed Heights Unnecessarily

This can create problems:

.card {
  height: 100px;
  padding: 30px;
}

Content may not fit as expected.

Often, allowing the height to remain auto is more flexible:

.card {
  padding: 30px;
}

Mistake 4: Ignoring Responsive Screens

A padding value that looks good on a large desktop screen may be excessive on a mobile device.

Use responsive values when necessary.

Mistake 5: Applying Padding to the Wrong Element

Sometimes developers add padding to a child when the spacing should belong to the parent.

Understanding the visual relationship between content, container, and surrounding elements helps determine where padding should be applied.

A Practical Modern CSS Example

Here is a simple card using both padding and border-box:

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

.card {
  width: 100%;
  max-width: 400px;
  padding: 24px;
  border: 1px solid #ddd;
  border-radius: 12px;
  background: white;
}

.card-title {
  margin: 0 0 10px;
}

.card-text {
  margin: 0;
}

The global border-box rule makes the sizing behavior more predictable, while padding provides comfortable internal spacing.

Best Practices for CSS Padding

Use padding intentionally rather than adding it everywhere.

Some useful practices include:

  1. Use padding to create internal spacing.
  2. Use margin or gap for spacing between separate elements.
  3. Consider border-box for predictable component dimensions.
  4. Use responsive padding where appropriate.
  5. Prefer rem or fluid values when they suit the design system.
  6. Avoid unnecessary fixed heights.
  7. Use logical properties for internationalized layouts.
  8. Keep spacing consistent across related components.
  9. Check padding on small screens.
  10. Test form controls carefully when using width: 100%.

Padding Accessibility Considerations

Padding can also improve accessibility.

A button with sufficient padding provides a larger clickable area:

.button {
  padding: 12px 20px;
}

This can be more comfortable for users than a very small button.

However, padding alone does not guarantee accessible controls. Text size, contrast, keyboard access, focus indicators, and overall layout should also be considered.

Padding and the outline Property

An outline is not part of the normal box model in the same way as a border.

For example:

button:focus {
  outline: 2px solid;
  outline-offset: 3px;
}

Adding an outline does not work like adding padding or border to the element’s dimensions.

This is useful for creating visible keyboard-focus indicators without changing the layout.

Padding and box-sizing in Real Projects

A practical approach for many projects is to establish predictable box sizing early:

html {
  box-sizing: border-box;
}

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

Then components can be written naturally:

.container {
  width: 100%;
  padding: 20px;
}

.input {
  width: 100%;
  padding: 12px;
  border: 1px solid;
}

The dimensions are easier to reason about because padding and borders are included in declared widths.

Quick Reference

All sides

padding: 20px;

Top and bottom, left and right

padding: 10px 20px;

Top, left/right, bottom

padding: 10px 20px 30px;

Top, right, bottom, left

padding: 10px 20px 30px 40px;

Individual sides

padding-top: 10px;
padding-right: 20px;
padding-bottom: 30px;
padding-left: 40px;

Border-box

box-sizing: border-box;

Common global pattern

html {
  box-sizing: border-box;
}

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

Frequently Asked Questions

What is CSS padding?

CSS padding is the internal space between an element’s content and its border.

What is the default value of box-sizing?

The default value is content-box.

What does box-sizing: border-box do?

It makes the declared width and height include the element’s content, padding, and border.

Does padding increase an element’s size?

With content-box, padding normally increases the element’s total rendered size beyond its declared content width or height. With border-box, padding is included within the declared dimensions.

Can padding be negative?

No. CSS does not allow negative padding values.

Is padding inside or outside an element?

Padding is inside the element’s border.

What is the difference between padding and margin?

Padding creates internal space. Margin creates external space around an element.

Does padding inherit?

No. Padding is not inherited by default.

Why does width: 100% sometimes cause overflow?

With content-box, padding and borders can be added to the 100% content width. box-sizing: border-box can prevent this by including them within the declared width.

Should I use border-box?

For many modern layouts, border-box is a convenient choice because it makes dimensions easier to predict, especially for responsive components and form controls.

Final Thoughts

CSS padding is much more than a simple spacing property. It controls the relationship between an element’s content and its edges, affects the visual comfort of a design, and plays an important role in responsive layouts.

The box-sizing property completes the picture. With the default content-box, padding and borders are added outside declared dimensions. With border-box, they are included within the declared width and height.

Once you understand this difference, many common CSS sizing problems become much easier to diagnose.

A strong foundation is to remember three things:

Padding is inside the border.

Margin is outside the border.

box-sizing: border-box includes padding and borders within the declared dimensions.

Understanding these principles will make CSS layouts more predictable, responsive, and easier to maintain.

Scroll to Top