Display
Every element has a default display type.
blockstarts on a new line and fills the width (div,p,h1).inlineflows with text and ignores width and height (span,a).inline-blockflows inline but accepts width and height.nonehides the element and removes it from the layout.
.badge { display: inline-block; padding: 4px 10px; }
.hidden { display: none; }Position
| Value | Behaviour |
|---|---|
static | Default. Normal flow |
relative | Normal flow, but can be shifted with top/left |
absolute | Removed from flow and placed relative to the nearest positioned parent |
fixed | Fixed relative to the screen, stays when scrolling |
sticky | Acts normal until a scroll point, then sticks |
<style>
.parent { position: relative; height: 140px; background: #eff6ff; }
.tag { position: absolute; top: 10px; right: 10px; background: #2563eb; color: white; padding: 4px 10px; border-radius: 8px; }
.sticky { position: sticky; top: 0; background: #fde68a; padding: 8px; }
</style>
<div class="sticky">I stick to the top when you scroll</div>
<div class="parent"><span class="tag">New</span> Parent box</div>
<div style="height:300px"></div>z-index
z-index controls which positioned element appears on top. A higher number sits above a lower one.
.modal { position: fixed; z-index: 1000; }Tip: Use a small set of z-index values (for example 10, 100, 1000) so your stacking stays predictable.
Practice
Build a sticky navigation bar and a card with a badge in the top-right corner.
Practice tasks
Reading is not enough. Try these without copying code, then compare with the lesson.
- Compare block, inline and inline-block with three elements.
- Position a badge on a card using relative and absolute.
- Use z-index to stack two overlapping boxes.
Common mistakes
- Using absolute positioning for full-page layout.
- Applying z-index to an element that is not positioned.
Quick check
Relative to what is an absolutely positioned element placed?
Its nearest positioned ancestor.
What does display: none do?
It removes the element from the layout.