Lesson 20 of 296
display and position
What you will learn
- The display values and what each does
- Ways to hide elements and the differences between them
- The five position values
- z-index and stacking context
- overflow
On this page
display
display decides how an element's box behaves in the layout.
| Value | Behaviour |
|---|---|
block | New line, full width. Width, height, margin and padding all work. |
inline | Stays in the line of text. Width and height are ignored. Top and bottom margins do not push other lines away. |
inline-block | Stays in the line like inline, but width, height and all margins work. |
flex | Makes the element a Flexbox container (Lesson 21) |
grid | Makes the element a Grid container (Lesson 22) |
none | Removes the element completely |
a.button {
display: inline-block;
padding: 10px 20px;
}Links are inline by default. Making them inline-block lets padding and sizes work fully while they stay in the line.
Ways to hide things
| Method | Visible? | Takes space? | Read by screen readers? | Clickable? |
|---|---|---|---|---|
display: none | No | No | No | No |
visibility: hidden | No | Yes | No | No |
opacity: 0 | No | Yes | Yes | Yes |
.sr-only (below) | No | No (tiny) | Yes | No |
hidden HTML attribute | No | No | No | No |
Hidden for eyes, available for screen readers: a common class, often called sr-only (screen reader only):
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}Use it for labels that are clear visually from icons but still need text for screen readers.
position
position changes how an element is placed. Use it together with top, right, bottom and left.
| Value | Where it is placed | Keeps its space in the layout? |
|---|---|---|
static | Normal place in the page. The default. top and left do nothing. | Yes |
relative | Its normal place, then moved by top/left | Yes, the original space stays |
absolute | Relative to the nearest positioned ancestor | No, removed from the flow |
fixed | Relative to the screen. Stays put when you scroll. | No |
sticky | Normal place, until you scroll to a point, then it sticks | Yes |
absolute: the most important rule
An absolutely positioned element is placed relative to the nearest ancestor whose position is not static. If there is none, it uses the page itself.
That is why you usually set position: relative on the parent:
.card {
position: relative;
}
.card .badge {
position: absolute;
top: 8px;
right: 8px;
}The badge sits in the top-right corner of the card. Without position: relative on .card, it would go to the top-right corner of the page.
fixed and sticky
.site-header {
position: sticky;
top: 0;
}The header scrolls normally until it reaches the top of the screen, then stays there. sticky needs a top (or other side) value to work, and it stops working if a parent has overflow: hidden.
.chat-button {
position: fixed;
bottom: 20px;
right: 20px;
}The button always stays in the bottom-right corner of the screen.
Centring with absolute
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}top and left at 50% put the modal's top-left corner in the centre. The transform then moves it back by half its own width and height, so its centre is in the centre.
z-index: what goes on top
When positioned elements overlap, z-index decides which one is in front. A bigger number is closer to the user.
.dropdown { position: absolute; z-index: 10; }
.modal { position: fixed; z-index: 100; }z-index only works on elements that are positioned (not static), or that are flex or grid items.
Stacking context: why z-index 9999 "does not work"
Some properties create a stacking context: a separate group of layers. Examples: a positioned element with a z-index, opacity less than 1, transform, and filter.
Elements inside a stacking context can only be ordered inside that group. If the parent is behind another element, its children are also behind it, even with z-index: 9999.
Fix: find the parent that creates the stacking context (use dev tools), and change the z-index of that parent, or move the element (for example, put modals at the end of <body>).
Tip: keep a small scale of z-index values in your project, such as 10 for dropdowns, 100 for headers, 1000 for modals, instead of random large numbers.
overflow
overflow decides what happens when content is bigger than its box.
| Value | Effect |
|---|---|
visible | Content spills out (the default) |
hidden | Extra content is cut off |
scroll | Always shows scroll bars |
auto | Shows scroll bars only when needed. Usually the best choice. |
overflow-x and overflow-y control each direction separately.
Common mistakes
- Setting width on an inline element and wondering why nothing changes.
- Forgetting
position: relativeon the parent of an absolute element. position: stickywith notopvalue.- Using huge z-index numbers to beat a stacking context.
- Using
opacity: 0to hide something, so invisible buttons can still be clicked.
How to say it in an interview
"Block elements take the full width, inline elements sit in the text flow and ignore width and height, and inline-block is a mix. For position, relative offsets an element but keeps its space; absolute removes it from the flow and positions it against the nearest positioned ancestor; fixed is relative to the viewport; sticky switches between normal and fixed at a scroll point. z-index only works within a stacking context, which things like transform and opacity create, so a high z-index can still lose."
Key words
| Word | Meaning |
|---|---|
| display | How an element's box behaves in the layout |
| Normal flow | The default way elements are laid out, one after another |
| Positioned element | An element with position other than static |
| z-index | Which overlapping element is in front |
| Stacking context | A group of layers that z-index works inside |
| overflow | What happens to content bigger than its box |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.