CSS

CSS (Cascading Style Sheets) is the language that brings web pages to life with colors, layouts, and animations. It works alongside HTML to create visually stunning and responsive designs. Let's explore the power of CSS.

CSS Padding

31 March 2025 | Category:

CSS padding controls the space inside an element, between the content and the border. This guide covers all padding properties, including shorthand, percentage-based padding, and advanced techniques.


1️⃣ What is CSS Padding?

Padding adds space inside an element, increasing the distance between its content and the border.

Example:

div {
    padding: 20px;
}

📌 This applies 20px padding on all sides (top, right, bottom, left).

Shorthand Syntax:

padding: [top] [right] [bottom] [left];

2️⃣ CSS Padding for Each Side

You can set padding for specific sides using:

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

Example:

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

🔹 Shorthand Alternative:

padding: 10px 20px 30px 40px; /* Top, Right, Bottom, Left */

3️⃣ CSS Padding Shorthand

The padding property can take 1, 2, 3, or 4 values:

SyntaxEffect
padding: 10px;All sides = 10px
padding: 10px 20px;Top & Bottom = 10px, Left & Right = 20px
padding: 10px 20px 30px;Top = 10px, Left & Right = 20px, Bottom = 30px
padding: 10px 20px 30px 40px;Top = 10px, Right = 20px, Bottom = 30px, Left = 40px

4️⃣ Percentage-Based Padding

Padding can be defined in percentages, based on the element’s width.

Example:

div {
    width: 200px;
    padding: 10%; /* 10% of 200px = 20px padding */
}

📌 Even vertical padding (padding-top and padding-bottom) is based on width, not height.


5️⃣ Padding with inherit, initial, unset

PropertyEffect
padding: inherit;Inherits padding from the parent element
padding: initial;Resets to default (0)
padding: unset;Acts as inherit if parent has padding, otherwise initial

Example:

div {
    padding: inherit;
}

6️⃣ CSS Padding and box-sizing

By default, padding adds to the total size of an element.

Example:

div {
    width: 200px;
    padding: 20px;
}

📌 Total width = 200px + 20px (left) + 20px (right) = 240px

✅ To prevent this, use box-sizing: border-box;, which keeps the width constant.

div {
    width: 200px;
    padding: 20px;
    box-sizing: border-box; /* Keeps width fixed */
}

7️⃣ Negative Padding? ❌

CSS does not support negative padding (unlike margins).

📌 If you need an element to shrink, use margin: -value; instead.


8️⃣ CSS Padding in Flexbox

Padding works normally inside Flexbox layouts, but can affect spacing differently.

Adding space inside flex items:

.flex-item {
    padding: 20px;
}

Ensuring padding does not increase width:

.flex-item {
    padding: 20px;
    box-sizing: border-box;
}

9️⃣ CSS Padding vs Margin

PropertyEffect
paddingSpace inside an element
marginSpace outside an element

Example:

div {
    padding: 20px; /* Space inside */
    margin: 10px;  /* Space outside */
}

🛑 Important: Padding increases element size, while margin only pushes elements apart.


🔟 CSS Padding Cheat Sheet

PropertyEffect
padding: 20px;Same padding on all sides
padding: 10px 20px;Top & Bottom = 10px, Left & Right = 20px
padding: inherit;Takes padding from parent
padding: initial;Resets padding to default (0)
padding: 10%;10% of the element’s width

🎯 Conclusion

✅ Padding adds space inside an element
padding: auto; does not exist
Use box-sizing: border-box; to prevent size increase
Percentage padding is based on width
No negative padding in CSS