Skip to main content
CodeOath

CSS and layout · Chapter 2

Positioning, stacking contexts and units

About this chapter

Badges, sticky headers and floating buttons all need positioning, and positioning is where layouts go wrong in ways that are hard to see. This chapter covers the five values of position, why a fixed button can stop being fixed inside a transformed card, how z-index only works inside a stacking context, and how pixels, rem, em, percentages and viewport units behave when the surrounding sizes change.

Who it is for: Front-end developers who can build a page with flexbox and grid and want to understand why an element lands in the wrong corner or hides behind another.

You will be able to

  • Choose among the five position values and say what each does to normal flow.
  • Explain why a fixed element can move with a card that has a transform.
  • Explain stacking contexts and why a very large z-index can still lose.
  • Choose between px, rem, em, percentages and viewport units for a given job.
  • Diagnose a badge, a sticky header or a floating button that misbehaves.

The key idea: Many positioning bugs are not about the element itself but about the box it is measured against. Knowing which ancestor an element is positioned from, and which stacking context it paints in, explains most of the surprises.

In this chapter

  1. Badges, a sticky header and a button that stays put
  2. The five values of position
  3. A transformed ancestor changes what fixed means
  4. z-index and stacking contexts
  5. Units: pixels, rems, ems, percentages and viewport units
  6. Mistakes that recur

Locked: needs a plan

You can read chapter 1 free. Chapter 2 is the next one.

What you can do now, free: re-read chapter 1 and try its problems.

What a plan gives you: all chapters of the track you pick and their checks. Professional, one role is ₹329 a month or ₹2,299 a year. It renews until you cancel, and you can cancel any time. Paid plans open at launch.

Why is this locked?

Chapter 1 of every track is free, so you can see how the track reads. The rest of a track and its checks come with a plan, or open as you pass each chapter's check.

The price is for one period, a month or a year. A plan renews until you cancel.

Back to the track