📦 CSS Box Model

Last Updated: November 2025


The CSS Box Model defines how every HTML element is structured and spaced.
Every element is treated as a rectangular box made up of 4 layers:

🗣 Hinglish Tip: Har element ek box hota hai — andar se content, phir padding, border aur sabse bahar margin.


Box Model Layers Overview

LayerDescriptionProperty Example
ContentActual text or image inside the boxwidth: 200px; height: 100px;
PaddingSpace between content and borderpadding: 10px;
BorderOutline around the paddingborder: 2px solid black;
MarginSpace outside the bordermargin: 20px;

🧍 Margin Properties

Margin defines outer space around an element — separates elements.

PropertyDescriptionExample
margin-topSpace abovemargin-top: 10px
margin-rightSpace to the rightmargin-right: 20px
margin-bottomSpace belowmargin-bottom: 15px
margin-leftSpace to the leftmargin-left: 5px

✅ Shorthand

SyntaxMeaning
margin: 10pxAll sides same
margin: 10px 20pxTop/Bottom → 10px, Left/Right → 20px
margin: 10px 15px 20pxTop, Left/Right, Bottom
margin: 5px 10px 15px 20pxTop, Right, Bottom, Left
margin: 10px 20px;

💡 You can also use auto to center a block horizontally:

margin: 0 auto;

🗣 Hinglish Tip: auto se element horizontally center ho jata hai — especially fixed width wale div ke liye.


🧊 Border Properties

Border is the line around padding and content.

PropertyDescriptionExample
border-widthThicknessborder-width: 4px
border-styleLine type (solid, dotted, dashed, double, groove, ridge)border-style: solid
border-colorBorder colorborder-color: red
border-radiusRounded cornersborder-radius: 10px

✅ Shorthand

border: 3px solid green;

✅ Each Side

border-top: 2px solid red;
border-right: 4px dashed blue;
border-bottom: 3px dotted orange;
border-left: 1px solid gray;

🎯 Border Radius (Rounded Corners)

border-radius helps you curve or round the corners of a box.

You can control each corner separately or use shorthand for all.

PropertyCornerExample
border-top-left-radiusTop-left cornerborder-top-left-radius: 20px;
border-top-right-radiusTop-right cornerborder-top-right-radius: 30px;
border-bottom-right-radiusBottom-right cornerborder-bottom-right-radius: 10px;
border-bottom-left-radiusBottom-left cornerborder-bottom-left-radius: 40px;

✅ Shorthand Syntax

SyntaxMeaning
border-radius: 10px;All corners same
border-radius: 10px 20px;Top-left/Bottom-right → 10px, Top-right/Bottom-left → 20px
border-radius: 10px 20px 30px 40px;Top-left, Top-right, Bottom-right, Bottom-left

🧩 Example

<!DOCTYPE html>
<html>
  <head>
    <style>
      .rounded-box {
        width: 200px;
        height: 120px;
        background-color: lightcoral;
        border: 3px solid #333;
        border-top-left-radius: 20px;
        border-top-right-radius: 40px;
        border-bottom-left-radius: 10px;
        border-bottom-right-radius: 30px;
      }
    </style>
  </head>
  <body>
    <div class="rounded-box"></div>
  </body>
</html>

🧱 Padding Properties Padding gives inner space between content and border.

PropertyDescriptionExample
padding-topSpace at toppadding-top: 10px
padding-rightSpace at rightpadding-right: 20px
padding-bottomSpace at bottompadding-bottom: 15px
padding-leftSpace at leftpadding-left: 5px

✅ Shorthand Forms Same rules as margin:


🌈 Box Sizing

By default, total box size = content + padding + border ```css

.box {
  box-sizing: content-box; /* Default */
}

If you set:

box-sizing: border-box;

👉 Then width includes padding + border, making layout easier.

🗣 Hinglish Tip: border-box ka matlab — jo width set kiya wahi final size hoga, extra space add nahi hoga.


Total Element Size Formula

If box-sizing: content-box;

Total Width = width + padding-left + padding-right + border-left + border-right + margin-left + margin-right
Total Height = height + padding-top + padding-bottom + border-top + border-bottom + margin-top + margin-bottom

If box-sizing: border-box; → Padding and border included in width & height.


🧩 Example: Compare Box Sizing

<!DOCTYPE html>
<html>
  <head>
    <style>
      div {
        width: 200px;
        padding: 20px;
        border: 5px solid blue;
        margin: 10px;
      }

      .box1 {
        box-sizing: content-box;
        background-color: lightcoral;
      }
      .box2 {
        box-sizing: border-box;
        background-color: lightgreen;
      }
    </style>
  </head>
  <body>
    <div class="box1">content-box</div>
    <div class="box2">border-box</div>
  </body>
</html>

💡 Notice: Both boxes have same width in CSS, but actual visible width differs.


Box Shadow

Syntax

selector {
  box-shadow: offset-x offset-y blur spread color;
}
  • ✔ offset-x → Left/Right movement
  • ✔ offset-y → Up/Down movement
  • ✔ blur → Shadow ka softness
  • ✔ spread → Shadow kitna bada
  • ✔ color → Shadow ka color

🗣 Hinglish Tip: Zyada blur = soft shadow Zyada spread = big shadow

TypeDescriptionExample
Basic ShadowNormal outer shadow0 4px 10px rgba(0,0,0,0.3)
Dark Strong ShadowDeep shadow for floating effect0 10px 25px rgba(0,0,0,0.5)
Inset ShadowShadow inside the boxinset 0 0 15px rgba(0,0,0,0.4)
Multiple ShadowsTwo or more shadows0 0 10px red, 0 0 20px blue
Soft Card ShadowUI cards, modern design0 8px 20px rgba(0,0,0,0.15)

Example

/* Basic Shadow */
.box {
  box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3);
}

/* Soft Card Shadow */
.card {
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
}

/* Inset Shadow (Inside the Box) */
.inset-box {
  box-shadow: inset 0 0 15px rgba(0, 0, 0, 0.4);
}

/* Hard Dark Shadow */
.dark {
  box-shadow: 10px 10px 30px rgba(0, 0, 0, 0.6);
}

/* Multiple Shadows */
.multi {
  box-shadow: 0 0 10px red, 0 0 20px blue;
}