The Box Model
বক্স মডেল
1. Every Element Is a Box
Whatever an HTML element looks like — a heading, a paragraph, an image, a button — the browser always renders it as a rectangular box. That box has four layers: the content itself, padding around the content, a border around the padding, and margin outside the border separating it from other boxes. This is the single most important mental model in all of CSS layout.
Understanding the box model explains almost every "why is there unexpected space here" question you'll ever have while styling a page.
2. The Four Layers, One at a Time
Content is the actual text or image. Padding pushes the border away from the content — it's inside the box's background. Border is a visible (or invisible) line around the padding. Margin is transparent space outside the border, pushing other elements away. Padding and border are included in the element's background color; margin is not.
<div class="box">I am a box.</div>
.box {
background: #e6f4e8;
padding: 20px;
border: 4px solid #39b549;
margin: 30px;
}
3. box-sizing: border-box — The Fix Everyone Uses
By default (box-sizing: content-box), width only sets the content area — padding
and border are added on top, making the box bigger than you specified. Setting
box-sizing: border-box makes width include padding and border, so the box stays
exactly the size you set. Nearly every modern stylesheet starts with this reset.
box-sizing: content-box), width শুধু content এলাকা নির্ধারণ করে — padding ও border এর উপরে যোগ হয়, ফলে বাক্সটি আপনার নির্ধারিত মাপের চেয়ে বড় হয়ে যায়। box-sizing: border-box সেট করলে width-এর মধ্যেই padding ও border অন্তর্ভুক্ত থাকে, ফলে বাক্সটি ঠিক আপনার নির্ধারিত মাপেই থাকে। প্রায় প্রতিটি আধুনিক স্টাইলশিট এই রিসেট দিয়েই শুরু হয়।
* { box-sizing: border-box; } — it removes an entire category of layout-math surprises.
প্রায় সার্বজনীন একটি শুরুর রিসেট হলো:
* { box-sizing: border-box; } — এটি লেআউট গণনার অনেক অপ্রত্যাশিত সমস্যা দূর করে দেয়।
content-box (default)
width= content only- Final box size = width + padding + border
- Easy to accidentally overflow a layout
✅ border-box (recommended)
width= content + padding + border- Final box size = exactly the width you set
- Predictable, industry-standard default
4. Margin Collapsing — The Surprising Part
When two vertical margins meet — the bottom margin of one block and the top margin of the next — they don't add together. Instead they collapse into a single margin equal to the larger of the two. This only happens with vertical margins between block-level siblings, never with padding, and never with horizontal margins.
ধরে নেওয়া যে 20px bottom margin এবং 30px top margin মিলে 50px ফাঁকা জায়গা তৈরি করবে — বাস্তবে এটি 30px তৈরি করে, কারণ বড় margin-টাই জেতে।
5. Inline vs Block vs Inline-Block
Every element has a default display value that controls how its box behaves. Block
elements (like <div>, <p>) take the full available width and stack
vertically. Inline elements (like <span>, <a>) only
take as much width as their content and flow within a line of text — width/height/vertical margin don't
apply to them. Inline-block flows like inline but accepts width, height, and full margin
like block.
display মান থাকে, যা নির্ধারণ করে সেটির বাক্স কেমন আচরণ করবে। Block এলিমেন্ট (যেমন <div>, <p>) পুরো উপলব্ধ প্রস্থ নেয় এবং উল্লম্বভাবে সাজে। Inline এলিমেন্ট (যেমন <span>, <a>) শুধু কনটেন্ট যতটুকু প্রস্থ লাগে ততটুকুই নেয় এবং টেক্সটের লাইনের ভেতরেই প্রবাহিত হয় — width/height/vertical margin এদের ক্ষেত্রে কাজ করে না। Inline-block inline-এর মতো প্রবাহিত হয় কিন্তু block-এর মতো width, height ও পূর্ণ margin গ্রহণ করে।
| display | Takes full width? | width/height apply? | বাংলায় |
|---|---|---|---|
block | Yes | Yes | পুরো প্রস্থ নেয়, width/height কাজ করে। |
inline | No — content-sized | No | শুধু কনটেন্টের মাপে থাকে, width/height কাজ করে না। |
inline-block | No — content-sized | Yes | Inline-এর মতো বসে কিন্তু width/height মানে। |
6. Inspecting Boxes in DevTools
Every modern browser's DevTools (right-click → Inspect) shows a live box model diagram for the selected element, with exact pixel values for content, padding, border, and margin. This is the fastest way to debug "why is this element positioned/sized wrong" — faster than guessing from the CSS alone.
Right-click any element → Inspect to open DevTools on it.
Shows the final box model diagram with live pixel numbers.
Edit padding/margin values directly and see the page update instantly.
7. Glossary (শব্দকোষ)
| Term | Meaning | বাংলায় |
|---|---|---|
| Content box | The area holding the actual text/image. | প্রকৃত টেক্সট/ছবি ধারণকারী এলাকা। |
| Padding | Space between content and border, inside the background. | content ও border-এর মধ্যেকার জায়গা, ব্যাকগ্রাউন্ডের ভেতরে। |
| Border | A visible line drawn around the padding. | padding-এর চারপাশে আঁকা একটি দৃশ্যমান লাইন। |
| Margin | Transparent space outside the border, separating boxes. | border-এর বাইরে স্বচ্ছ জায়গা, যা বাক্সগুলোকে আলাদা রাখে। |
| box-sizing | Controls whether width includes padding/border. | width-এর মধ্যে padding/border অন্তর্ভুক্ত হবে কিনা তা নিয়ন্ত্রণ করে। |
| Margin collapsing | Adjacent vertical margins merge into the larger value. | পাশাপাশি vertical margin মিলে বড় মানে পরিণত হয়। |
8. Practice Problems
Every problem has a Show Answer button with real, runnable HTML+CSS. Try it yourself first, then check.
-
Create a <div> with 10px padding, a 2px solid black border, and 20px margin. Describe how big the total box is if the content is 100px wide (content-box default).10px padding, 2px solid কালো border, এবং 20px margin সহ একটি <div> বানান। content 100px চওড়া হলে (content-box ডিফল্টে) মোট বাক্সের আকার কত হবে তা লিখুন।
✨ Show Answer (উত্তর দেখুন)
ans1.html<div class="box">100px content</div>style.css.box { width: 100px; padding: 10px; border: 2px solid black; margin: 20px; }Answer: The visible border-to-border box is 124px wide (100 content + 10+10 padding + 2+2 border). Margin (20px each side) is outside that and doesn't count toward the box's own size — it only pushes neighboring elements away.
দৃশ্যমান border-থেকে-border বাক্সের প্রস্থ হবে 124px (100 content + 10+10 padding + 2+2 border)। Margin (প্রতি পাশে 20px) এর বাইরে থাকে এবং বাক্সের নিজের আকারের মধ্যে গণনা হয় না — এটি শুধু পাশের এলিমেন্টকে দূরে ঠেলে দেয়।
-
Add box-sizing: border-box to the previous box and set width to exactly 100px. What is the new content width?আগের বাক্সে box-sizing: border-box যোগ করুন এবং width ঠিক 100px সেট করুন। এখন নতুন content width কত হবে?
✨ Show Answer (উত্তর দেখুন)
ans2.html<div class="box">border-box demo</div>style.css.box { box-sizing: border-box; width: 100px; padding: 10px; border: 2px solid black; margin: 20px; }Answer: The content area shrinks to 76px (100 total − 10−10 padding − 2−2 border), so the outer box (border-to-border) stays exactly 100px wide.
Content এলাকা 76px-এ সংকুচিত হয় (100 মোট − 10−10 padding − 2−2 border), ফলে বাইরের বাক্স (border-থেকে-border) ঠিক 100px প্রস্থ থাকে।
-
Two stacked <p> elements have margin: 20px 0; on both. How many pixels of gap appear between them, and why?দুটি স্ট্যাক করা <p> এলিমেন্টের উভয়েই margin: 20px 0; আছে। তাদের মাঝে কত পিক্সেল ফাঁকা জায়গা দেখা যাবে, এবং কেন?
✨ Show Answer (উত্তর দেখুন)
Answer: 20px, not 40px. The bottom margin of the first paragraph and the top margin of the second collapse into a single margin equal to the larger of the two — since both are 20px, the result is 20px.
40px নয়, 20px। প্রথম paragraph-এর bottom margin এবং দ্বিতীয়টির top margin একত্রে collapse করে দুটির মধ্যে বড়টির সমান একটি margin-এ পরিণত হয় — যেহেতু দুটোই 20px, ফলাফল 20px।
-
Write CSS turning a <span> into an inline-block element sized 80px by 80px with a background color.এমন CSS লিখুন যা একটি <span>-কে 80px বাই 80px আকারের একটি ব্যাকগ্রাউন্ড-কালারযুক্ত inline-block এলিমেন্টে পরিণত করে।
✨ Show Answer (উত্তর দেখুন)
ans4.htmlText before <span class="swatch"></span> text after.style.css.swatch { display: inline-block; width: 80px; height: 80px; background: #39b549; vertical-align: middle; } -
In one paragraph, explain why setting width and height on a plain <span> (display: inline) has no visible effect.একটি অনুচ্ছেদে ব্যাখ্যা করুন, একটি সাধারণ <span>-এ (display: inline) width ও height সেট করলে কেন কোনো দৃশ্যমান প্রভাব পড়ে না।
✨ Show Answer (উত্তর দেখুন)
Answer: Inline elements are designed to flow within a line of text alongside other inline content, so the box model treats their size as determined purely by their content. The browser intentionally ignores width, height, and vertical margin on inline boxes to keep text flow predictable; to make sizing work, the element needs display: inline-block or display: block instead.
Inline এলিমেন্ট অন্যান্য inline কনটেন্টের পাশাপাশি টেক্সটের লাইনে প্রবাহিত হওয়ার জন্য ডিজাইন করা, তাই বক্স মডেল তাদের আকার শুধু কনটেন্ট দ্বারা নির্ধারিত ধরে নেয়। টেক্সট ফ্লো predictable রাখতে ব্রাউজার ইচ্ছাকৃতভাবে inline বাক্সে width, height ও vertical margin উপেক্ষা করে; আকার কাজ করাতে হলে display: inline-block বা display: block ব্যবহার করতে হবে।
Summary — Module 12
Every element renders as a box built from content, padding,
border, and margin. box-sizing: border-box makes width
include padding and border for predictable sizing. Vertical margins between blocks can
collapse into the larger value. display — block, inline, or inline-block —
decides whether width/height apply and whether the box takes the full line.
box-sizing: border-box width-এর মধ্যে padding ও border অন্তর্ভুক্ত করে predictable আকার দেয়। ব্লকগুলোর মধ্যে vertical margin collapse করে বড় মানে পরিণত হতে পারে। display — block, inline, বা inline-block — নির্ধারণ করে width/height কাজ করবে কিনা এবং বাক্সটি পুরো লাইন নেবে কিনা।