SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
CSS
1. CSS Tutorial for Beginners — Learn CSS from Scratch 2. CSS Syntax Explained — Selectors, Properties & Values 3. CSS Selectors Tutorial — Class, ID, Attribute & More 4. How to Add CSS to HTML — Inline, Internal, External 5. CSS Colors Tutorial — HEX, RGB, RGBA, HSL Explained 6. CSS Backgrounds Tutorial — Images, Size & Position 7. CSS Borders Tutorial — Style, Width, Radius & Shadow 8. CSS Margin Explained — Shorthand, Auto & Collapse 9. CSS Padding Explained — Shorthand & Margin Difference 10. CSS Height and Width — px, %, em, rem, vw, vh Units 11. CSS Box Model Explained — Content, Padding, Border 12. CSS Text Styling Tutorial — Color, Align & Spacing 13. CSS Fonts Tutorial — font-family, Sizes & Google Fonts 14. CSS Link Styling — Hover, Visited, Active & Buttons 15. CSS Lists Styling — Markers, Counters & Shorthand 16. CSS Tables Styling — Zebra Stripes & Hover Rows 17. CSS Display Property — Block, Inline, None Explained 18. CSS Position Explained — Relative, Absolute, Fixed 19. CSS Overflow Explained — Hidden, Scroll, Auto & More 20. CSS Float and Clear — Layouts & Clearfix Trick 21. CSS Pseudo-classes — Hover, Focus, nth-child & More 22. CSS Pseudo-elements — ::before, ::after & ::first-line 23. CSS Form Styling — Inputs, Labels & Buttons Tutorial 24. CSS Navigation Bar Tutorial — Dropdown Menu Included 25. CSS Website Layout Tutorial — Header, Sidebar, Footer 26. CSS Flexbox Tutorial — Complete Guide with Examples 27. CSS Grid Tutorial — Columns, Rows & Layouts Guide 28. CSS Responsive Design — Media Queries Tutorial 29. CSS Transitions & Animations — No JavaScript Needed 30. CSS Variables Tutorial — :root, var() & Theming 31. CSS Transforms Explained — Rotate, Scale, Translate 32. CSS Filters Tutorial — Blur, Grayscale, Sepia & More 33. CSS Text Effects — Shadows, Wrapping & Overflow 34. CSS Keyframes Tutorial — Bouncing Ball Animation 35. CSS Grid Template Areas — Layout in Words, Not Numbers
Final Test — Certificate ₹10
Try this code live
Lesson 9 of 35

CSS Padding Explained — Shorthand & Margin Difference

4 min read · Free · No login needed

Padding is the space inside an element — between its content and its border. Think of a photo frame: the picture is the content, the frame is the border, and the white mount board between them is the padding. Padding makes content breathe; without it, text touches the border and looks cramped.

Diagram — see how it fits together
Diagram: CSS Padding

Padding is part of the element itself, so the element's background colour fills the padding area too. That is the easiest way to tell padding apart from margin: margin is outside the border and stays transparent, padding is inside the border and shows the background.

The four sides

Each side has its own property: padding-top, padding-right, padding-bottom and padding-left. Use them when only one side needs space.

h1 {
  padding-top: 20px;
  padding-bottom: 10px;
}

The padding shorthand

One property, padding, can set all four sides at once. The number of values changes the meaning:

  • padding: 20px; — all four sides get 20px.
  • padding: 10px 20px; — top and bottom 10px, left and right 20px.
  • padding: 10px 20px 30px; — top 10px, left and right 20px, bottom 30px.
  • padding: 10px 20px 30px 40px; — top, right, bottom, left — clockwise from the top.

The four-value order is easy to remember: it goes clockwise, starting at the top — like the hands of a clock.

Syntax — padding in action

.card {
  border: 2px solid orange;
  background: lightyellow;
  padding: 20px 30px;
}
button {
  padding: 10px 24px;
}
Note: Padding values can be in px, %, em or rem. A percentage is measured against the width of the element's parent — even for top and bottom padding, which surprises most beginners.
Tip: Buttons feel right when their left and right padding is about twice their top and bottom padding — for example padding: 10px 24px;. The button looks wide and easy to tap, not tall and cramped.

Padding vs margin

Use padding when the space should carry the element's background — a card, a button, a coloured banner. Use margin when you want empty space between two elements, like the gap between two cards. If you are not sure, ask: "Should this space be coloured?" If yes, padding. If no, margin.

Common mistakes

  • Using padding to push two elements apart — that is margin's job. Padding pushes the content away from its own border.
  • Forgetting that padding adds to the width. A box of width: 200px with padding: 20px is 240px wide on the page (with the default box-sizing: content-box).
  • Putting top and bottom padding on an inline element like <span> — it paints, but it does not push the lines above and below apart.
  • Writing the four-value shorthand in the wrong order — it is top, right, bottom, left, clockwise from the top.

Remember

  • Padding is the space inside the border; the background colour fills it.
  • Shorthand values go clockwise from the top: top, right, bottom, left.
  • Two values mean vertical first, then horizontal: padding: 10px 20px;.
  • Padding adds to the element's total size unless box-sizing: border-box is set.

Example explained

  • .menu { ... padding: 20px; } — the whole menu card gets 20px of space inside its border on all four sides, so the heading and the items never touch the orange border.
  • .item { ... padding: 10px 15px; } — each menu item gets 10px of space top and bottom and 15px left and right, so the text sits comfortably inside its white box.
  • margin-bottom: 10px; on .item — this is margin, not padding: it is the empty gap between two menu items, outside their borders.
  • .price { ... padding: 4px 10px; } — the offer line is a small strip, so it needs only a little padding: 4px top and bottom, 10px left and right.
  • background: #fffbeb; on .menu — the pale yellow fills the padding too, which is how you can see that padding is inside the element.

Example

Example
<!DOCTYPE html>
<html>
<head>
<title>Padding - Chai Shop Menu</title>
<style>
.menu { width: 300px; border: 2px solid #b45309; background: #fffbeb; padding: 20px; }
.menu h2 { margin: 0 0 10px; }
.item { background: #fff; border: 1px solid #e5e7eb; padding: 10px 15px; margin-bottom: 10px; }
.item p { margin: 0; }
.price { padding: 4px 10px; background: #16a34a; color: #fff; }
</style>
</head>
<body>

<div class="menu">
<h2>Chai and Snacks Menu</h2>
<div class="item"><p>Masala Chai - Rs. 20</p></div>
<div class="item"><p>Samosa - Rs. 15</p></div>
<p class="price">Today's offer: Chai + Samosa = Rs. 30</p>
</div>

</body>
</html>

Output

Output — how it looks in the browser
Output of CSS Padding Explained — Shorthand & Margin Difference

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Frequently asked questions

The questions learners ask most about this lesson — read the question, then open it to see the answer.

1. What is padding?

The space inside an element, between its content and its border. The background colour fills it.

2. What is the difference between padding and margin?

Padding is the space inside the border; margin is the space outside it. Use padding to give content room, margin to push elements apart.

3. Does padding add to an element's size?

Yes, unless box-sizing: border-box is set. A 200px box with 20px padding is 240px wide by default.

Why learn this yourself — and not just ask AI?

AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.

Get certified — just ₹10

Finish all 35 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

AI & ML with Azure AI, AWS AI & Generative AI
₹10,000
View
Summer Training in Python Programming 2026
₹3,000
View
AI Power Skills in 2 Days | Learn AI Text, Image, Video and Voice Tools
View
Browse all courses
CSS — Lessons
1. CSS Tutorial for Beginners — Learn CSS from Scratch 2. CSS Syntax Explained — Selectors, Properties & Values 3. CSS Selectors Tutorial — Class, ID, Attribute & More 4. How to Add CSS to HTML — Inline, Internal, External 5. CSS Colors Tutorial — HEX, RGB, RGBA, HSL Explained 6. CSS Backgrounds Tutorial — Images, Size & Position 7. CSS Borders Tutorial — Style, Width, Radius & Shadow 8. CSS Margin Explained — Shorthand, Auto & Collapse 9. CSS Padding Explained — Shorthand & Margin Difference 10. CSS Height and Width — px, %, em, rem, vw, vh Units 11. CSS Box Model Explained — Content, Padding, Border 12. CSS Text Styling Tutorial — Color, Align & Spacing 13. CSS Fonts Tutorial — font-family, Sizes & Google Fonts 14. CSS Link Styling — Hover, Visited, Active & Buttons 15. CSS Lists Styling — Markers, Counters & Shorthand 16. CSS Tables Styling — Zebra Stripes & Hover Rows 17. CSS Display Property — Block, Inline, None Explained 18. CSS Position Explained — Relative, Absolute, Fixed 19. CSS Overflow Explained — Hidden, Scroll, Auto & More 20. CSS Float and Clear — Layouts & Clearfix Trick 21. CSS Pseudo-classes — Hover, Focus, nth-child & More 22. CSS Pseudo-elements — ::before, ::after & ::first-line 23. CSS Form Styling — Inputs, Labels & Buttons Tutorial 24. CSS Navigation Bar Tutorial — Dropdown Menu Included 25. CSS Website Layout Tutorial — Header, Sidebar, Footer 26. CSS Flexbox Tutorial — Complete Guide with Examples 27. CSS Grid Tutorial — Columns, Rows & Layouts Guide 28. CSS Responsive Design — Media Queries Tutorial 29. CSS Transitions & Animations — No JavaScript Needed 30. CSS Variables Tutorial — :root, var() & Theming 31. CSS Transforms Explained — Rotate, Scale, Translate 32. CSS Filters Tutorial — Blur, Grayscale, Sepia & More 33. CSS Text Effects — Shadows, Wrapping & Overflow 34. CSS Keyframes Tutorial — Bouncing Ball Animation 35. CSS Grid Template Areas — Layout in Words, Not Numbers
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Please log in to download. You will stay on this page after logging in!

New here? Register now →