🧱 CSS Tutorial: padding

πŸ“Œ Introduction

The padding property in CSS controls the space inside an element β€” between the element’s content and its border. Unlike margin (external spacing), padding expands the clickable area and gives breathing space to text and UI elements. Padding is essential for clean, readable, and user-friendly design. ✨

🎯 Why Use padding?

  • To increase space inside buttons for better usability πŸ”˜
  • To improve readability of text blocks πŸ“„
  • To make elements look balanced and comfortable 🧘
  • To enhance spacing inside cards, containers, and boxes πŸ“¦

✨ Syntax

Basic Syntax

selector {
  padding: <length> | <percentage>;
}

Note

Padding does not accept negative values β€” unlike margin. Percentage padding is based on the width of the element’s container.

🧩 Ways to Set Padding

1. Shorthand Padding

4-Value Syntax

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

2. Three Values

Three Values

/* top | left+right | bottom */
.box {
  padding: 10px 20px 30px;
}

3. Two Values

Two Values

/* top+bottom | left+right */
.box {
  padding: 10px 20px;
}

4. One Value

Single Value

.box {
  padding: 20px;
}

5. Individual Padding Properties

Individual Sides

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

🧩 Special Padding Concepts

1. Padding Affects Element Size (Box Model!)

By default, padding increases the total size of an element. But with box-sizing: border-box;, padding stays inside the declared width/height.

Border Box

* {
  box-sizing: border-box;
}

Note

Use border-box for predictable layouts β€” highly recommended. βœ”οΈ

2. Percentage Padding (Pro Tip πŸ’‘)

Percentage-based padding is based on the container’s width, not height. This enables responsive square or rectangle containers.

Responsive Square

.square {
  width: 100%;
  padding-top: 100%; /* makes a perfect square */
}

πŸ”₯ Practical Examples

1. Button Padding for Better UX

Button Example

button {
  padding: 12px 20px;
  font-size: 16px;
}

2. Spacing Inside a Card

Card Example

.card {
  padding: 25px;
  background: #f8f8f8;
  border-radius: 8px;
}

3. Responsive Section Padding

Responsive Padding

section {
  padding: 5% 10%;
}

4. Padding for Inputs

Input Padding

input {
  padding: 10px 12px;
  border-radius: 5px;
}

πŸ“Š Padding vs Margin vs Border

PropertyWhere It AppliesPurpose
paddingInside the elementAdd space between content & border
borderBetween padding & marginCreates visual boundary
marginOutside the elementCreates spacing between elements

πŸ–ΌοΈ Visual Demo

>>β€œPadding is like breathing room β€” it makes your UI feel open, comfortable, and readable.” ✨

πŸŽ‰ Conclusion

The padding property is essential for clean and comfortable UI design. It defines internal spacing, enhances user experience, and ensures visual balance. Master padding along with margin and border, and you'll have full control of the CSS box model. πŸš€