The Tutorial SIte

Menu
  • Bootstrap 4
  • CSS
  • HTML
Home
CSS
CSS Padding – A Guide
CSS

CSS Padding – A Guide

CSS padding is used to create space inside an element, between its content and border. It helps improve readability, layout, and spacing within a webpage. Proper padding enhances design aesthetics and user experience.

What You Will Learn

  1. What is CSS Padding?
  2. Padding vs. Margin
  3. Basic Syntax and Usage
  4. Setting Individual Padding Values (Top, Right, Bottom, Left)
  5. Using the Shorthand padding Property
  6. Padding Units (px, %, em, rem, vw, vh)
  7. Padding and Box Model Behavior
  8. Padding with box-sizing
  9. Using padding for Responsive Design
  10. Best Practices and Common Mistakes

1. What is CSS Padding?

Padding is the inner space between an element’s content and its border. It does not affect the position of surrounding elements but increases the size of the element itself.

Example: Padding Illustration

[ Margin ]  --- Space outside the element
[ Border ]  --- Edge of the element
[ Padding ] --- Space inside the border
[ Content ] --- Text, images, or other elements

2. Padding vs. Margin

Property Description
Padding Creates inner space between the content and the element’s border.
Margin Creates outer space between the element and surrounding elements.

Example: Difference Between Padding and Margin

.box {
    margin: 20px; /* Space outside */
    padding: 20px; /* Space inside */
    border: 2px solid black;
}

3. Basic Syntax and Usage

The padding property allows you to define spacing inside an element.

Basic Syntax

selector {
    padding: value;
}

Example: Applying Padding

.box {
    padding: 20px;
    background-color: lightgray;
}
<div class="box">This content has padding.</div>

Effect:

  • The text moves inward because of padding.
  • The background extends to the padding area.

4. Setting Individual Padding Values

Padding can be individually set for each side:

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

Example: Different Padding for Each Side

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

5. Using the Shorthand padding Property

Instead of setting padding for each side separately, you can use shorthand.

Value Count Order Applied
padding: 10px; All sides have 10px padding.
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.

Example: Using Padding Shorthand

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

6. Padding Units (px, %, em, rem, vw, vh)

Padding can be defined using different units.

Unit Description
px Fixed size (e.g., padding: 20px;).
% Relative to the element’s width (e.g., padding: 5%;).
em Relative to the font size of the element (e.g., padding: 1.5em;).
rem Relative to the root font size (e.g., padding: 2rem;).
vw Relative to viewport width.
vh Relative to viewport height.

Example: Using Different Units

.box1 { padding: 20px; }  /* Fixed pixels */
.box2 { padding: 5%; }  /* Relative to width */
.box3 { padding: 1.5em; } /* Scales with font size */
.box4 { padding: 2rem; } /* Scales with root font size */

7. Padding and Box Model Behavior

By default, padding increases the element’s total size because it is added to the width and height.

Example: Default Box Model (Padding Increases Size)

.box {
    width: 200px;
    height: 100px;
    padding: 20px;
    border: 2px solid black;
}

Total Width Calculation:

Width = 200px + Left Padding (20px) + Right Padding (20px) + Border (2px * 2)
Width = 244px

The actual element is larger than expected.

8. Padding with box-sizing

To prevent padding from increasing the total size, use box-sizing: border-box;.

Example: Using border-box to Fix Width

.box {
    width: 200px;
    height: 100px;
    padding: 20px;
    border: 2px solid black;
    box-sizing: border-box;
}

With box-sizing: border-box;, padding is included in the width and height.

9. Using padding for Responsive Design

Padding with percentages (%) or relative units (em, rem) helps maintain a responsive layout.

Example: Responsive Padding

.responsive-box {
    width: 80%;
    padding: 5%;
    background-color: lightblue;
}
  • The padding scales with the element’s size.
  • Works well for fluid and flexible layouts.

10. Best Practices and Common Mistakes

Best Practices

Use box-sizing: border-box; to prevent unexpected size increases.
Use % for flexible padding in responsive layouts.
Keep padding consistent across elements for a clean layout.
Use em or rem when scaling padding with font sizes.

Common Mistakes and How to Fix Them

Mistake Solution
Forgetting box-sizing: border-box; Use it to prevent unexpected element size increase.
Overusing fixed padding (px) Use % or em for flexible layouts.
Mixing margin and padding unnecessarily Use padding for inside spacing, margin for outside spacing.
Setting excessive padding Use CSS Grid or Flexbox instead of large paddings for layout adjustments.

Conclusion

  • Padding adds space inside an element.
  • Use padding: auto; does not work, unlike margins.
  • Use shorthand properties for better efficiency.
  • Use box-sizing: border-box; to prevent size issues.
  • Use relative units (%, em, rem) for responsive layouts.
Share
Telegram
Linkedin
Pocket
Pinterest
Tweet
Email
Prev Article
Next Article

Related Articles

CSS Tutorial Collection

CSS Tutorial Collection

CSS Display – A Guide
The display property in CSS controls how elements are rendered …

CSS Display – A Guide

Categories

  • Bootstrap 4
  • CSS
  • HTML

Latest

  • HTML Classes : A Tutorial
  • HTML Tables : A Tutorial
  • HTML iframe : A Tutorial
  • HTML div : A Detailed Tutorial
  • Bootstrap 4 Tutorial Collection
  • CSS Tutorial Collection
  • HTML Tutorial Collection
  • HTML head : A Detailed Tutorial
  • Popular
  • Recent
  • Tags

The Tutorial SIte

Copyright © 2025 The Tutorial SIte
The Tutorial Site

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh