Book a free demo

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, none
  • position: static, relative, absolute, fixed, sticky
  • offsets top, right, bottom, left and stacking with z-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

A badge pinned to a card corner
.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 โ†’

CSS

More in this module

Keep going in Web Developer Path

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.

Play now

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

For a childโ€™s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

Weโ€™ll use your details only to respond to this request.

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.

All free games โ†’
Chat on WhatsApp
Esc

Try