Layout and responsive design
Display and positioning
Goal: Choose block, inline and inline-block and place elements with position · 11 min
display decides how an element takes part in the page flow.
- block (div, p, h1, section): starts on a new line and takes the full width;
width,height, margins and padding all work. - inline (span, a, strong): flows within a line; **
widthandheightare ignored** and vertical margins do not push lines apart. - inline-block: flows like text but accepts width, height and vertical margins.
- none: removes the element completely (no space left). Compare
visibility: hidden, which hides it but keeps its space. - flex and grid: modern layout containers (next lessons).
position takes elements out of or shifts them within the normal flow:
| value | behaviour |
|---|---|
static | default; ignores top/left |
relative | stays in the flow, can be shifted with top/left; it is the anchor for absolute children |
absolute | removed from the flow, placed relative to the nearest positioned ancestor (not static) |
fixed | placed relative to the viewport; stays when scrolling |
sticky | normal flow until a scroll threshold, then sticks (e.g. a header) |
.card { position: relative; }
.card .badge { position: absolute; top: 8px; right: 8px; }
The badge sits 8px from the top-right corner of its card, because the card is the nearest positioned ancestor. Use positioning for small overlays; use Flexbox and Grid for page layouts.
Worked example: A badge on a card
- Make the card the positioning anchor: position: relative.
- Give the badge position: absolute and top/right offsets.
- The badge no longer takes space in the flow and overlaps the corner.
.card { position: relative; }
.badge {
position: absolute;
top: 8px;
right: 8px;
}