The Box Model

বক্স মডেল

Read: ~24 min Medium 5 practice problems Live preview runner

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.

HTML এলিমেন্ট দেখতে যেমনই হোক না কেন — একটি heading, একটি paragraph, একটি ছবি, একটি বাটন — ব্রাউজার সবসময় সেটিকে একটি আয়তক্ষেত্রাকার বাক্স (box) হিসেবে রেন্ডার করে। সেই বাক্সের চারটি স্তর থাকে — content নিজেই, content-এর চারপাশে padding, padding-এর চারপাশে একটি border, এবং border-এর বাইরে margin যা একে অন্য বাক্স থেকে আলাদা রাখে। CSS লেআউটের সবচেয়ে গুরুত্বপূর্ণ মানসিক মডেল এটিই।

Understanding the box model explains almost every "why is there unexpected space here" question you'll ever have while styling a page.

বক্স মডেল বুঝলে পেজ স্টাইল করার সময় প্রায় প্রতিটি "এখানে হঠাৎ এত জায়গা কেন?" প্রশ্নের উত্তর নিজে থেকেই পাওয়া যায়।
margin border padding content Figure 12.1 — content, padding, border, margin এর নেস্টেড স্তর।

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.

Content হলো প্রকৃত টেক্সট বা ছবি। Padding border-কে content থেকে দূরে সরায় — এটি বাক্সের ব্যাকগ্রাউন্ডের ভেতরেই থাকে। Border হলো padding-এর চারপাশে দৃশ্যমান (বা অদৃশ্য) একটি লাইন। Margin হলো border-এর বাইরের স্বচ্ছ জায়গা, যা অন্য এলিমেন্টকে দূরে ঠেলে দেয়। Padding ও border এলিমেন্টের ব্যাকগ্রাউন্ড কালারের ভেতরে পড়ে; margin পড়ে না।
index.html
<div class="box">I am a box.</div>
style.css
.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 অন্তর্ভুক্ত থাকে, ফলে বাক্সটি ঠিক আপনার নির্ধারিত মাপেই থাকে। প্রায় প্রতিটি আধুনিক স্টাইলশিট এই রিসেট দিয়েই শুরু হয়।
Key insight A near-universal starting reset is: * { 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.

যখন দুটি vertical margin মিলিত হয় — একটি ব্লকের নিচের margin এবং পরের ব্লকের উপরের margin — তখন সেগুলো যোগ হয় না। বরং তারা collapse করে একটি margin-এ পরিণত হয়, যা দুটির মধ্যে বড়টির সমান। এটি শুধু block-level sibling-দের মধ্যে vertical margin-এই ঘটে, padding-এ কখনো নয়, এবং horizontal margin-এও নয়।
Common Mistake — সাধারণ ভুল Expecting a 20px bottom margin plus a 30px top margin to create 50px of gap — it actually creates 30px, because the larger margin wins.

ধরে নেওয়া যে 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 গ্রহণ করে।
displayTakes full width?width/height apply?বাংলায়
blockYesYesপুরো প্রস্থ নেয়, width/height কাজ করে।
inlineNo — content-sizedNoশুধু কনটেন্টের মাপে থাকে, width/height কাজ করে না।
inline-blockNo — content-sizedYesInline-এর মতো বসে কিন্তু 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.

প্রতিটি আধুনিক ব্রাউজারের DevTools (right-click → Inspect) নির্বাচিত এলিমেন্টের জন্য একটি লাইভ বক্স মডেল ডায়াগ্রাম দেখায়, content, padding, border ও margin-এর সঠিক পিক্সেল মান সহ। "কেন এই এলিমেন্টের অবস্থান/আকার ভুল?" ডিবাগ করার এটিই দ্রুততম উপায় — শুধু CSS দেখে অনুমান করার চেয়ে।
Inspect Element
Right-click any element → Inspect to open DevTools on it.
Computed Panel
Shows the final box model diagram with live pixel numbers.
Live Editing
Edit padding/margin values directly and see the page update instantly.

7. Glossary (শব্দকোষ)

TermMeaningবাংলায়
Content boxThe area holding the actual text/image.প্রকৃত টেক্সট/ছবি ধারণকারী এলাকা।
PaddingSpace between content and border, inside the background.content ও border-এর মধ্যেকার জায়গা, ব্যাকগ্রাউন্ডের ভেতরে।
BorderA visible line drawn around the padding.padding-এর চারপাশে আঁকা একটি দৃশ্যমান লাইন।
MarginTransparent space outside the border, separating boxes.border-এর বাইরে স্বচ্ছ জায়গা, যা বাক্সগুলোকে আলাদা রাখে।
box-sizingControls whether width includes padding/border.width-এর মধ্যে padding/border অন্তর্ভুক্ত হবে কিনা তা নিয়ন্ত্রণ করে।
Margin collapsingAdjacent 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.

প্রতিটি প্রশ্নের সাথে Show Answer বাটন রয়েছে, যেখানে চালু-করার-যোগ্য HTML+CSS দেওয়া আছে। প্রথমে নিজে চেষ্টা করুন, তারপর মিলিয়ে নিন।
  1. 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) এর বাইরে থাকে এবং বাক্সের নিজের আকারের মধ্যে গণনা হয় না — এটি শুধু পাশের এলিমেন্টকে দূরে ঠেলে দেয়।

  2. 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 প্রস্থ থাকে।

  3. 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।

  4. 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.html
    Text before <span class="swatch"></span> text after.
    style.css
    .swatch {
      display: inline-block;
      width: 80px;
      height: 80px;
      background: #39b549;
      vertical-align: middle;
    }
  5. 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.

প্রতিটি এলিমেন্ট content, padding, border, ও margin দিয়ে তৈরি একটি বাক্স হিসেবে রেন্ডার হয়। box-sizing: border-box width-এর মধ্যে padding ও border অন্তর্ভুক্ত করে predictable আকার দেয়। ব্লকগুলোর মধ্যে vertical margin collapse করে বড় মানে পরিণত হতে পারে। display — block, inline, বা inline-block — নির্ধারণ করে width/height কাজ করবে কিনা এবং বাক্সটি পুরো লাইন নেবে কিনা।

Next Module → Colors, Units & Typography — রঙ, ইউনিট ও টাইপোগ্রাফি দিয়ে টেক্সটকে প্রাণবন্ত করা।