Web Developer Path ยท lesson 12 of 18
Display and position
Control how boxes flow on the page, hide them, and place one precisely on top of another.
What you will learn
display:block,inline,inline-block,noneposition:static,relative,absolute,fixed,sticky- offsets
top,right,bottom,leftand stacking withz-index
Understand the idea
display decides how an element takes part in the page flow. A block element (paragraph, div) starts on a new line and fills the width. An inline element (span, a) flows within a line and ignores width and height. inline-block flows in a line but accepts width and height. display: none removes the element completely, and it takes no space.
position decides where a box goes. static is the default: normal flow. relative keeps the box in the flow but lets you nudge it with top, left and so on, and it also becomes the reference point for children. absolute takes the box out of the flow and places it relative to the nearest ancestor that is positioned (not static), or to the page if there is none. fixed pins a box to the browser window, so it stays put while scrolling, and sticky scrolls normally until it reaches an edge, then sticks.
A classic pattern: give a parent position: relative and a child position: absolute with top and right, which pins the child to the parent's corner, perfect for a badge. When boxes overlap, z-index decides which is on top: the higher number wins.
Example
.card {
position: relative;
padding: 24px;
border: 2px solid slateblue;
}
.badge {
position: absolute;
top: -10px;
right: -10px;
background: crimson;
color: white;
padding: 2px 8px;
}The badge is absolute, so it is placed relative to the card, because the card is relative.
Your turn: code to type and practise
Make .card position: relative, pin .badge to its top-right corner with position: absolute, top and right, and hide .hide-me with display: none.
Stretch challenge
Make a header stay at the top while scrolling with position: sticky; top: 0.
โ The box modelResponsive design and the 12-column grid โ
More in this module
Keep going in Web Developer Path
Adding CSS: three ways and three selectors
Colour the `h1` text, give `.intro` a `background-color`, and give `#special` both a `color` and a `background-color`.
Start โ8Backgrounds and gradients
Give `.hero` a `background-image` that uses `linear-gradient`, set its `background-size`, and give `.card` a `background-color`. Use the long-hand properties.
Start โ9Fonts and text styling
Style `h1` with a `font-family`, a `font-size`, `text-align: center` and `text-transform: uppercase`. Style `p` with a `line-height` and `letter-spacing`, and remove the underline from `a` with `text-decoration: none`.
Start โ10Combinators, pseudo-classes and pseudo-elements
Write four rules with exactly these selectors: `.menu a:hover` (set a `color`), `.menu li:first-child` (set `font-weight`), `h2::before` (set `content`) and `input[type="text"]` (set a `border`).
Start โ11The box model
Style `.card`: give it a `width`, some `padding`, a `border`, a `margin` of `12px auto`, and `box-sizing: border-box`.
Start โPractise the idea
๐ฆ Play Laser Mirrors
Place and turn mirrors to bend a laser beam onto every crystal. 24 puzzles, beam splitters in tier 3.
FAQ
Display and position: common questions
What is the difference between position relative and absolute?
Relative keeps the element in the normal flow and offsets it from where it would have been. Absolute removes it from the flow and places it relative to its nearest positioned ancestor.
How do I hide an element in CSS?
display: none removes it and its space. visibility: hidden hides it but keeps the space.
What does z-index do?
It sets which overlapping positioned element is on top. A higher number goes in front.
I am stuck. What can I do?
Press Hint for a nudge, read the message next to each goal, and compare your code with the lesson text. If you still need it, Show answer reveals a model solution. Try to type it yourself rather than copy it.
Does my work get saved?
Your ticks and XP are saved in this browser, and in your account if you log in. The code you type is not stored on a server.
Start a conversation
Enjoying coding? Meet a teacher.
Tell us the learner's age, country and what they would like to build, and we will suggest a class. A parent or guardian can make the enquiry.
- Tell us the learnerโs age or grade and goals
- Weโll suggest a suitable class or learning path
- Weโll confirm format, timing and fees with a parent or guardian
Play it, don't just read it
Write some code right now
Free games that need no sign-up. Pick one and play for five minutes.
Pick a game
- Predict the OutputRead a short Python program and choose what it prints. Then watch it runโฆPlay
- Bug HunterA Python program prints the wrong answer. Find the one line with theโฆPlay
- Code BuilderThe lines of a Python program are mixed up. Tap them into the rightโฆPlay
- Code JungleWrite real Python to guide Miko the monkey to every banana. 24 levels:โฆPlay