Notes › Web Development
CSS Layout — Box Model, Flexbox and Grid
The box model
Every element is a box: content → padding → border → margin.
.card {
width: 300px;
padding: 16px;
border: 2px solid #ddd;
margin: 12px;
}
By default (content-box) the real width is 300 + 2×16 + 2×2 = 336px. Almost every project sets:
*, *::before, *::after { box-sizing: border-box; } /* width now includes padding and border */
Vertical margins between blocks collapse (the larger one wins) — padding doesn't.
display
| Value | Behaviour |
|---|---|
block |
full width, starts on a new line (div, p, h1) |
inline |
flows in text, ignores width/height (span, a) |
inline-block |
flows in text but accepts width/height |
none |
removed from the layout |
flex / grid |
turns the element into a layout container |
position
| Value | Positioned relative to | In normal flow? |
|---|---|---|
static (default) |
— | yes |
relative |
its normal place | yes (space kept) |
absolute |
nearest positioned ancestor | no |
fixed |
the viewport (stays on scroll) | no |
sticky |
normal until a scroll threshold, then sticks | yes |
z-index only works on positioned elements (and flex/grid items). |
Flexbox — one direction (row OR column)
.nav {
display: flex;
flex-direction: row; /* or column */
justify-content: space-between; /* main axis */
align-items: center; /* cross axis */
gap: 12px;
flex-wrap: wrap;
}
.nav .search { flex: 1; } /* take the remaining space */
| Container property | Values |
|---|---|
justify-content |
flex-start, center, flex-end, space-between, space-around, space-evenly |
align-items |
stretch (default), center, flex-start, flex-end, baseline |
flex-wrap |
nowrap, wrap |
gap |
space between items |
| Item property | Meaning |
|---|---|
flex: 1 |
grow to fill space (flex-grow: 1; flex-shrink: 1; flex-basis: 0) |
flex: none |
keep its size |
align-self |
override align-items for one item |
order |
change visual order |
With
flex-direction: column, the main axis is vertical:justify-contentaligns vertically andalign-itemshorizontally.
Grid — two directions (rows AND columns)
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 3 equal columns */
gap: 16px;
}
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); /* responsive, no media query */
}
.layout {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"side header"
"side main"
"side footer";
}
.layout header { grid-area: header; }
.hero { grid-column: 1 / -1; } /* span all columns */
fr = a fraction of the free space.
Flexbox or Grid?
| Use Flexbox for | Use Grid for |
|---|---|
| a row of buttons, a navbar | page layouts |
| aligning items in one line | card galleries |
| content decides the size | the layout decides the size |
| They work together — a grid of cards, each card using flex inside. |
Centering
.center { display: grid; place-items: center; } /* simplest */
.center2 { display: flex; justify-content: center; align-items: center; }
.abs { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
Responsive design
.grid { display: grid; grid-template-columns: 1fr; }
@media (min-width: 768px) { /* mobile-first: add columns on bigger screens */
.grid { grid-template-columns: 1fr 1fr; }
}
img { max-width: 100%; height: auto; }
h1 { font-size: clamp(1.6rem, 4vw, 2.6rem); }
Add <meta name="viewport" content="width=device-width, initial-scale=1"> in the HTML head.
Units
| Unit | Relative to |
|---|---|
px |
fixed |
% |
the parent |
em |
the element's font size |
rem |
the root (html) font size — best for spacing and fonts |
vw / vh |
1% of the viewport width / height |
Specificity (which rule wins)
inline style > #id > .class, [attr], :hover > element. Equal specificity → the later rule wins. !important overrides all (avoid it).
Interview one-liners
box-sizing: border-boxincludes padding and border in the width.- Flexbox is one-dimensional, Grid two-dimensional.
position: absoluteuses the nearest positioned ancestor.- Mobile-first = base styles for phones,
min-widthmedia queries for bigger screens. - Learn more: CSS documentation, HTML documentation.