P2.3: Resilient CSS Box Model
Every element on a webpage is treated as a rectangular box. In this lesson, we will master the box model rules that control spacing, borders, shadows, and visibility.
Every element on a webpage is treated as a rectangular box. In this lesson, we will master the box model rules that control spacing, borders, shadows, and visibility.
Lesson: p2-3-box-model
Every element on a webpage is treated as a rectangular box. In this lesson, we will master the box model rules that control spacing, borders, shadows, and visibility.
Every HTML box is made up of four layers, nesting from the inside out:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ MARGIN โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ BORDER โ โ
โ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ โ
โ โ โ PADDING โ โ โ
โ โ โ โโโโโโโโโโโโโโโโโโโโโ โ โ โ
โ โ โ โ CONTENT AREA โ โ โ โ
โ โ โ โโโโโโโโโโโโโโโโโโโโโ โ โ โ
โ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
width and height)..card {
width: 300px;
padding: 20px;
border: 1px solid #334155;
margin: 15px;
}
Mastering padding and margin scaling is crucial for achieving professional spacing alignments in user interfaces.
Average proportion of element sizing in standard UI layouts.
You can define exact dimensions or set flexible limits to prevent layouts from breaking:
min-width / max-width: Limits how narrow or wide a box can scale.min-height / max-height: Limits how short or tall a box can scale.overflow)If a box has fixed dimensions and its content is too large to fit, the overflow property controls the behavior:
hidden: Crops the overflowing content (renders it invisible).scroll: Adds horizontal and vertical scrollbars, even if the content fits.auto: Adds scrollbars only if the content overflows the box..scroll-box {
height: 200px;
overflow: auto;
}
You can declare padding and margins for each side individually (padding-top, padding-right, padding-bottom, padding-left) or use a shorthand sequence following a clockwise order (Top, Right, Bottom, Left):
/* Clockwise: Top=10px, Right=20px, Bottom=10px, Left=20px */
padding: 10px 20px 10px 20px;
/* Symmetrical: Top/Bottom=10px, Left/Right=20px */
padding: 10px 20px;
/* Uniform: All 4 sides get 15px */
padding: 15px;
When the bottom margin of one element touches the top margin of another, they collapse into a single margin. The browser does not add them together; instead, it renders only the larger of the two margins.
To center a block box inside its parent container, specify a fixed width and set the left and right margins to auto:
.centered-container {
width: 90%;
max-width: 1200px;
margin: 0 auto; /* Top/Bottom=0, Left/Right automatically balanced */
}
Sets width, style, and color in a single declaration:
/* border: width style color; */
border: 3px dotted #4ade80;
solid, dotted, dashed, double, groove, ridge, inset, outset, none.border-radius)Creates rounded corners on any box. Value can be in pixels or percentages (50% on a square box creates a perfect circle).
.rounded-card {
border-radius: 12px;
}
.profile-avatar {
width: 80px;
height: 80px;
border-radius: 50%;
}
box-shadow)Adds a drop shadow around the element box:
/* box-shadow: horizontal-offset vertical-offset blur-radius spread-radius color; */
box-shadow: 0px 4px 20px rgba(0, 0, 0, 0.3);
inset at the start to render the shadow inside the box borders.The display property determines how an element behaves in the document flow:
block: Starts on a new line and stretches to fill the full width of its parent (e.g. <div>, <p>, <h1>).inline: Flows inline with text. Ignores width and height properties (e.g. <span>, <strong>, <a>).inline-block: Flows inline like a span, but respects width, height, margins, and padding.none: Completely hides the element and removes it from the page layout flow.none vs. Visibility hiddendisplay: none: Hides the box completely. Adjacent elements shift to fill the empty space.visibility: hidden: Hides the content of the box, but leaves the physical box in the layout (renders as a blank gap).