Padding
Padding
padding — Space Inside the Element
.box {
padding: 20px; /* 20px space INSIDE, between content and border */
border: 1px solid navy;
}
Padding is the layer just inside the border — space between the element's content and its edge. Unlike margin (which pushes OTHER elements away), padding creates room WITHIN the element, around its content. If the element has a background colour or border, padding is visibly filled by that background — because padding is inside the box.
padding vs margin — the crucial difference
┌─ MARGIN (outside - pushes neighbours away) ─┐
│ ┌─ BORDER ─────────────────────────────┐ │
│ │ ┌─ PADDING (inside - around content) │ │
│ │ │ CONTENT │ │
│ │ └───────────────────────────────────┘ │
│ └──────────────────────────────────────┘ │
└──────────────────────────────────────────────┘
| padding | margin | |
|---|---|---|
| Where | Inside the border | Outside the border |
| Purpose | Space around content | Space between elements |
| Background | Filled by background | Always transparent |
| Clicks | Part of the element (clickable) | Not part of the element |
Sides and Shorthand — same system as margin
padding-top: 10px;
padding-right: 20px; /* or set each side */
padding: 20px; /* all sides */
padding: 10px 20px; /* top/bottom | left/right */
padding: 10px 20px 30px 40px; /* top right bottom left (clockwise) */
Padding uses the EXACT same system as margin: individual sides (padding-top etc.), or the clockwise shorthand (Top-Right-Bottom-Left, remember "TRouBLe"). One value = all sides; two values = vertical then horizontal; four = each side. Since you learned this for margin, padding is instantly familiar — the same rules, just applied inside instead of outside.
Real-World Uses — where padding shines
- Buttons:
padding: 12px 24pxmakes a button comfortably sized and clickable — text alone is too cramped to click. - Cards and boxes: padding keeps content from touching the box edges, giving that clean, breathable look.
- Table cells: padding on th/td (from the tables chapter) stops data from cramming against the borders.
- Sections: generous padding creates the spacious feel of professional websites.
Padding and Size — the box model reminder
.box {
width: 200px;
padding: 20px; /* adds 20+20 = 40px to the total width! */
}
/* Actual on-screen width = 200 + 40 = 240px (by default) */
box-sizing: border-box (its own chapter, coming next), makes padding fit INSIDE the declared width instead. For now, remember padding adds to size by default — plan your widths accordingly.Exam Corner
Q: Key difference between padding and margin? Padding is inside (around content); margin is outside (between elements).
Q: Is padding filled by the element's background? Yes — padding is inside the box, so the background shows through it.
Q: What does padding: 10px 20px mean? Top & bottom 10px, left & right 20px.
Q: Does padding add to an element's width by default? Yes — unless box-sizing: border-box is set.
padding — Element के अंदर की Space
.box {
padding: 20px; /* content aur border ke beech ANDAR 20px space */
border: 1px solid navy;
}
Padding border के ठीक अंदर वाली layer है — element के content और उसके किनारे के बीच space. Margin (जो DUSRE elements को दूर धकेलता है) के उलट, padding element के ANDAR, उसके content के चारों ओर जगह बनाता है. अगर element का background colour या border हो, padding उस background से दिखाई देते हुए भरता है — क्योंकि padding box के अंदर है.
padding vs margin — अहम फर्क
┌─ MARGIN (bahar - padosiyon ko door dhakelta) ┐
│ ┌─ BORDER ─────────────────────────────┐ │
│ │ ┌─ PADDING (andar - content ke charon or)│
│ │ │ CONTENT │ │
│ │ └───────────────────────────────────┘ │
│ └──────────────────────────────────────┘ │
└──────────────────────────────────────────────┘
| padding | margin | |
|---|---|---|
| कहां | Border के अंदर | Border के बाहर |
| मकसद | Content के चारों ओर space | Elements के बीच space |
| Background | Background से भरा | हमेशा transparent |
| Clicks | Element का हिस्सा (clickable) | Element का हिस्सा नहीं |
Sides और Shorthand — margin जैसा ही system
padding-top: 10px;
padding-right: 20px; /* ya har side set karo */
padding: 20px; /* saare sides */
padding: 10px 20px; /* top/bottom | left/right */
padding: 10px 20px 30px 40px; /* top right bottom left (clockwise) */
Padding margin जैसा EXACT same system use करता है: individual sides (padding-top आदि), या clockwise shorthand (Top-Right-Bottom-Left, "TRouBLe" याद रखिए). एक value = सारे sides; दो values = vertical फिर horizontal; चार = हर side. यह आपने margin के लिए सीखा, इसलिए padding तुरंत परिचित — वही rules, बस अंदर की बजाय... अंदर लगे.
असली इस्तेमाल — जहां padding चमकता है
- Buttons:
padding: 12px 24pxbutton को आरामदायक size और clickable बनाता है — सिर्फ text click करने को बहुत तंग. - Cards और boxes: padding content को box के किनारों से छूने से रोकता है, वही साफ, breathable look देता है.
- Table cells: th/td पर padding (tables chapter से) data को borders से चिपकने से रोकता है.
- Sections: उदार padding professional websites का विशाल feel बनाता है.
Padding और Size — box model reminder
.box {
width: 200px;
padding: 20px; /* total width me 20+20 = 40px jodta hai! */
}
/* Asli screen width = 200 + 40 = 240px (default) */
box-sizing: border-box (अपना chapter, अगला), padding को declared width के ANDAR fit कराता है. अभी याद रखिए padding default रूप से size में जुड़ता है — अपनी widths उसी अनुसार plan कीजिए.Exam Corner
Q: Padding और margin में मुख्य फर्क? Padding अंदर (content के चारों ओर); margin बाहर (elements के बीच).
Q: क्या padding element के background से भरा होता है? हां — padding box के अंदर है, तो background उसमें से दिखता है.
Q: padding: 10px 20px का क्या मतलब? Top और bottom 10px, left और right 20px.
Q: क्या padding default रूप से element की width में जुड़ता है? हां — जब तक box-sizing: border-box set न हो.
💻 Live Code Editor
इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.console.log देखने के लिए F12 दबाइए.