CODE PATH
PRO ACCOUNT

Master Code
On The Go

Learn. Practice. Build.

Quest 1 โ€ข Lesson 8

๐Ÿ“ฆ CSS Box Model

Every element in CSS is a rectangular box. The Box Model consists of four layers: content, padding, border, and margin.

"Understanding the box model is essential for creating layouts. It determines how much space an element takes up and how it interacts with neighbouring elements."

๐Ÿงฉ The Four Layers

๐ŸŸฆ Content ๐ŸŸจ Padding ๐ŸŸฅ Border ๐ŸŸฉ Margin
CONTENT
Padding (yellow) ยท Border (red) ยท Margin (green)
box-model.css
.box {
  /* content area */
  width: 200px;
  height: 100px;
  /* padding inside the border */
  padding: 20px;
  /* border around padding */
  border: 5px solid #ef4444;
  /* margin outside the border */
  margin: 30px;
}

๐Ÿ“ `box-sizing: border-box` vs `content-box`

By default, box-sizing: content-box means width/height apply only to content. Padding and border are added on top.

With box-sizing: border-box, width/height include padding and border โ€“ much easier for layouts.

content-box (default)

200px wide + padding + border = 228px

border-box

200px wide includes padding + border

โœจ Challenge: Build a Card with Box Model

Create a card (div) with:

โค๏ธ Support Free Education

This course is 100% free. If it helps you, consider buying me a coffee.

โ˜• Buy Me a Coffee

โžก๏ธ Ready for more?

Next lesson: Flexbox & Grid โ€“ build modern layouts.

Continue to Lesson 1.9 โ†’

(Coming soon โ€“ check back or buy Pro Pack for instant access)