CSS

CSS (Cascading Style Sheets) is the language that brings web pages to life with colors, layouts, and animations. It works alongside HTML to create visually stunning and responsive designs. Let's explore the power of CSS.

CSS Box Model

31 March 2025 | Category:

The CSS Box Model defines how elements are displayed and sized on a webpage. It consists of four layers:

  1. Content – The actual text or image inside the element.
  2. Padding – Space between content and border.
  3. Border – Surrounds the padding and content.
  4. Margin – Space between this element and other elements.

1️⃣ Understanding the CSS Box Model

Every HTML element is treated as a box in CSS. The total size of an element is calculated as:

Total Width  = Width + Padding (left + right) + Border (left + right) + Margin (left + right)
Total Height = Height + Padding (top + bottom) + Border (top + bottom) + Margin (top + bottom)

Visual Representation

+--------------------------+  ← Margin
|                          |
|  +--------------------+  |  ← Border
|  |                    |  |
|  |   +------------+   |  |  ← Padding
|  |   |  Content   |   |  |
|  |   +------------+   |  |
|  |                    |  |
|  +--------------------+  |
|                          |
+--------------------------+

2️⃣ CSS Box Model Properties

PropertyDescription
width / heightDefines the content width & height
paddingCreates space inside the border
borderDefines the outline of the box
marginCreates space outside the box

Example:

.box {
    width: 200px;
    height: 100px;
    padding: 10px;
    border: 5px solid black;
    margin: 20px;
}

📌 Total width = 200px + (10px * 2) + (5px * 2) + (20px * 2) = 270px
📌 Total height = 100px + (10px * 2) + (5px * 2) + (20px * 2) = 170px


3️⃣ box-sizing Property

By default, width and height only apply to the content, not padding or border. To make sure the total size includes padding and border, use:

.box {
    box-sizing: border-box;
}

Comparison:

PropertyEffect
box-sizing: content-box;Default behavior – padding & border are added outside width & height
box-sizing: border-box;Width & height include padding & border (recommended)

Best practice: Always use box-sizing: border-box; for consistent sizing.


4️⃣ Padding in Detail

padding defines the space inside the border.

.box {
    padding: 20px; /* Applies to all sides */
}

📌 You can set different values for each side:

.box {
    padding: 10px 15px 20px 25px; /* top right bottom left */
}

or

.box {
    padding-top: 10px;
    padding-right: 15px;
    padding-bottom: 20px;
    padding-left: 25px;
}

5️⃣ Border in Detail

The border property defines the outline of the element.

Example:

.box {
    border: 5px solid red;
}
Border TypeExample
solidborder: 2px solid black;
dashedborder: 2px dashed red;
dottedborder: 2px dotted blue;
doubleborder: 4px double green;
noneborder: none;

6️⃣ Margin in Detail

margin defines external spacing between elements.

.box {
    margin: 20px; /* Applies to all sides */
}

📌 You can also set individual sides:

.box {
    margin: 10px 15px 20px 25px; /* top right bottom left */
}

or

.box {
    margin-top: 10px;
    margin-right: 15px;
    margin-bottom: 20px;
    margin-left: 25px;
}

🔹 Auto Margin (Centering Elements)

.box {
    width: 300px;
    margin: 0 auto;
}

📌 This centers the element horizontally.


7️⃣ Collapsing Margins

When two vertical margins meet, the larger one takes effect instead of adding up.

.box1 {
    margin-bottom: 50px;
}
.box2 {
    margin-top: 30px;
}

📌 Instead of 50px + 30px = 80px, the margin will be 50px (the larger one).


8️⃣ Overflow Handling

If content exceeds the box size, use overflow:

PropertyEffect
visibleContent overflows (default)
hiddenExtra content is hidden
scrollAdds scrollbars
autoAdds scrollbars only when needed

Example:

.box {
    width: 100px;
    height: 50px;
    overflow: hidden;
}

9️⃣ Summary Cheat Sheet

PropertyEffect
width & heightDefines element’s size
paddingSpace inside the element
borderOutline of the box
marginSpace outside the element
box-sizing: border-box;Ensures padding & border are included in width
margin: auto;Centers the element horizontally
overflow: hidden;Hides overflowing content

🎯 Conclusion

✅ CSS Box Model defines how elements are sized and spaced
box-sizing: border-box; simplifies layout calculations
margin, padding, and border affect spacing and size
✅ Understanding these avoids layout issues