CSS Padding Property: Complete Guide with Syntax and Examples

calender-iconPublished: 27 Jun 2025

clock-icon5-min read





INTRODUCTION

Padding is the space between an element’s content area and its border. It adds space inside the element, pushing the content inward.

By default, an element has no padding. This means the content sits very close to the border. So, when you add a border to an element, it's a good idea to also add some padding to give the content some space.

css box-model

CSS Box-Model

CSS padding property

The CSS padding property is used to set the padding on all four sides of an element at once.

The padding property can take one to four values, and each value can be a length value (like 10px, 2em) or a percentage.

Length Values - You can use any length unit like em, rem, px etc for padding. Also while using multi-value syntax you can use different units for different sides.

Percentage - When percent value is used for padding, the percentage is calculated with respect to the width of the parent element's content area. So percentage value may change if width changes.

CSS Padding order

As mentioned earlier padding property can be defined by using one, two, three, or four values. An easy way to remember order of sides is to imagine the sides in clockwise direction starting with top side. You can visualise by seeing the below diagram for reference.

clockwise
  • One value - Applies to all four sides
  • Two values:
    • First value → top and bottom
    • Second value → left and right
  • Three values:
    • First value → top
    • Second value → left and right
    • Third value → bottom
  • Four values: Applies in clockwise order: top, right, bottom, left
    • First value → top
    • Second value → right
    • Third value → bottom
    • Fourth value → left
Syntax

padding: [all-sides];                         /* one value */

padding: [top and bottom] [left and right];   /* two values */

padding: [top] [left and right] [bottom];     /* three values */

padding: [top] [right] [bottom] [left];       /* four values */


Exmaples:


padding: 20px;                /* 20px on top, right, bottom, left */                       

padding: 1em 5%;              /* 1em top & bottom, 5% left & right */

padding: 2rem 3vw 10px;       /* top: 2rem, left & right: 3vw, bottom: 10px */

padding: 10px 2em 5% 1rem;    /* top: 10px, right: 2em, bottom: 5%, left: 1rem */




Negative Padding

Negative padding is not allowed in CSS. The padding property defines the space between the content and the element’s border. It can only add space — not remove it. If you try to use something like (padding: -10px;), It will simply be ignored by the browser and reset to 0 (or fallback value).

Constituent properties

In CSS, the padding shorthand property is made up of four constituent (longhand) properties that control the padding on each individual side of an element. The constituent properties are listed below.

Padding on Inline elements

The CSS padding property can also be applied to inline elements. when you apply padding to top or bottom-side of inline element it will not increase/decrease the line-height of text.

Example: As can be seen in below example the word automobile is given an background-color of grey along with an top and bottom padding of 5px, but it has no effect on line height.


span{
    background-color: grey;
    padding-top: 5px;
    padding-bottom: 5px;

}


An <span>automobile</span> is a self-propelled vehicle designed for transporting people on roads. The <span>automobile</span> has revolutionized modern life by offering flexibility, convenience, and speed in travel. Whether it's a compact <span>automobile</span> for city use or a powerful SUV, the type of <span>automobile</span> someone chooses often reflects their lifestyle. 
Output
An automobile is a self-propelled vehicle designed for transporting people on roads. The automobile has revolutionized modern life by offering flexibility, convenience, and speed in travel. Whether it's a compact automobile for city use or a powerful SUV, the type of automobile someone chooses often reflects their lifestyle.




The CSS padding property when applied to the right or left side of an element it will create an extra space after or before the element.

Example: As can be seen in below example the word automobile is given an background-color of grey along with an left and right padding of 10px, has created an extra space before and after the element.


span{
    background-color: grey;
    padding-left: 10px;
    padding-right: 10px;

}


An <span>automobile</span> is a self-propelled vehicle designed for transporting people on roads. The <span>automobile</span> has revolutionized modern life by offering flexibility, convenience, and speed in travel. Whether it's a compact <span>automobile</span> for city use or a powerful SUV, the type of <span>automobile</span> someone chooses often reflects their lifestyle. 
Output
An automobile is a self-propelled vehicle designed for transporting people on roads. The automobile has revolutionized modern life by offering flexibility, convenience, and speed in travel. Whether it's a compact automobile for city use or a powerful SUV, the type of automobile someone chooses often reflects their lifestyle.

CSS Padding color

The padding property only creates space inside an element — between the content and the border. It doesn’t have its own color or any property to color it.

The padding area is affected by the element’s background-color. When you apply padding to an element and set a background-color, that background will fill both the content and the padding area.

CSS doesn't support targeting just the padding area directly, but you can use workarounds: You can use an inner wrapper to color just the padding area as shown in the below code.


<div class="outer">
  <div class="inner">Text</div>
</div>



.outer {
  padding: 20px;
  background-color: lightblue; /* Padding area */
 }

.inner {
  background-color: white; /* Content area */
 }

Frequently Asked Questions (FAQ)

What is the difference between margin and padding in CSS?
Feature Margin Padding
What it does Creates space outside the element Creates space inside the element
Affects The space between elements The space between content and border
Visual effect Pushes the element away from others Pushes the content away from the element’s border
Can collapse Vertical margins can collapse (e.g., between two elements) Padding does not collapse
Can be negative? Yes, margins can be negative No, padding cannot be negative

Why does CSS not support negative padding?
CSS does not support negative padding because padding defines the space between the content and the element’s border, and this space must always be positive or zero for layout integrity and visual consistency.

Padding increases the internal area of an element, pushing the content inward. If negative padding were allowed, it would mean pulling the content outside its own container, which Breaks the box model logic. It Could cause layout and rendering issues which Would be difficult for browsers to handle.

How do I prevent the padding property from changing width or height in CSS?
When you add padding in CSS, it normally increases the size of the element because padding is added inside the element’s box by default. To prevent padding from changing the element’s width or height, use:


box-sizing: border-box;

By default, CSS uses box-sizing: content-box, which means: width and height only include the content. Padding and border are added outside of that, increasing the final size.

Using box-sizing: border-box; changes the box model so that: width and height include the content, padding, and border. The total size stays fixed, no matter how much padding or border you add.

Can We Define max-padding or min-padding in CSS?
No, CSS does not currently support max-padding or min-padding properties directly — unlike min-width, max-width, min-height, and max-height.

CSS treats padding as a single-value property, not a size constraint like width or height. The CSS box model doesn’t allow setting limits on padding values directly.

What does padding:10px, 15px, 12px, 20px; mean?
It sets the padding for all four sides of the element using the four-value shorthand in clockwise order:

It is same as
padding-top: 10px;
padding-right: 15px;
padding-bottom: 12px;
padding-left: 20px;

What units can be used for padding in CSS?
Here's a list of all the units that can be used for padding in CSS:

Absolute Length Units - px, pt, pc, in, cm, mm, Q

Relative Length Units - em, rem, ex, ch, lh, rlh

Viewport Units - vw, vh, vmin, vmax

Percentage Unit - %

What is the default padding in CSS?
By default, the padding of most HTML elements is: 0 (zero) No padding is applied by default to elements like div, span, section, etc. Some elements appear to have space (like body, ul, h1), but that's usually margin, not padding.

Can I Add Background Color Only for Padding in CSS?
Not directly. In CSS, the background-color applies to the content + padding area — but not just the padding alone. You can use some Workarounds to Target Only the Padding Area

example:

<div class="outer">
  <div class="inner">Text</div>
</div>

.outer {
  padding: 20px;
  background-color: lightblue;
}

.inner {
  background-color: white; /* Covers the content area */
}

Does CSS Padding Collapse Like Margin?
No, padding does not collapse in CSS. In CSS, margin can collapse — especially vertical margins between elements — but padding never collapses.

Can You Use padding: auto in CSS?
No, padding: auto does not work as you might expect. The auto value is valid for some CSS properties like margin, width, left, or right, but not for padding. When you set padding: auto, most browsers will treat it as 0 — it has no effect.