CSS Academy · Lesson

Absolute Positioning and Containing Block

Position elements absolutely relative to their nearest positioned ancestor.

Lesson 2 of 413 steps

Absolute Positioning and Containing Block is a free CSS Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CSS Academy learning path, and your progress syncs across the web and the CoddyKit app. The CSS Academy course includes 4 lessons in total.

position: absolute

An absolutely positioned element is removed from normal document flow. Other elements act as if it does not exist. It is positioned relative to its nearest positioned ancestor.

The Containing Block

The containing block for an absolutely positioned element is its nearest ancestor with a position value other than static. If none exists, the element is positioned relative to the initial containing block (the viewport).

Setting up a Containing Block

The most common pattern: add position: relative to a parent to contain absolutely positioned children.

<div class="card">Card content<span class="badge">Badge</span></div>

Offset Properties with absolute

With absolute positioning, top/right/bottom/left are measured from the padding edge of the containing block.

<div class="tooltip-wrap">Hover target<span class="tooltip">I'm below the parent</span></div>

Centering with absolute

The classic centering technique for absolute elements:

<div class="center-wrap"><div class="centered">Perfectly centered</div></div>

Width and Height of absolute Elements

Absolutely positioned elements shrink-wrap to their content by default (unless width/height is set). They no longer stretch to their container's full width like block elements.

Stretching with all Edges

Setting all four edges to 0 stretches an absolutely positioned element to fill its containing block:

<div class="stretch-wrap"><div class="overlay">Overlay covers all edges</div></div>

inset Shorthand

The inset property is shorthand for top, right, bottom, left together:

<div class="cover-wrap"><div class="full-cover"></div></div>

Stacking with z-index

Absolutely positioned elements participate in stacking contexts. Use z-index to control which overlapping absolute elements appear on top.

<div class="stack-wrap"><div class="box-a">Behind</div><div class="tooltip">On top (z-index:100)</div></div>

Common Pitfall: Forgetting the Parent

If you forget to add position: relative to the parent, an absolute child will be positioned relative to the nearest already-positioned ancestor — often the viewport. Always check the containing block.

Absolute Inside Flex/Grid

A flex or grid container is not automatically a positioned element. You still need to add position: relative explicitly to make it a containing block for absolute children.

Quick Check

Where is an absolutely positioned element placed if none of its ancestors have a position other than static?

Recap

Absolutely positioned elements leave the document flow and are positioned relative to their nearest non-static ancestor. Always add position: relative to the parent to control placement. Use inset: 0 to stretch an absolute element to fill its container.

Free to start

Learn CSS with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “Absolute Positioning and Containing Block” lesson free?

Yes — the full text of “Absolute Positioning and Containing Block” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CSS Academy course, upgrade to CoddyKit PRO. The CSS Academy course includes 4 lessons in total.

What will I learn in “Absolute Positioning and Containing Block”?

Position elements absolutely relative to their nearest positioned ancestor. You practise CSS Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start CSS Academy?

No prior experience is required. CSS Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Absolute Positioning and Containing Block” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this CSS Academy lesson?

Yes. Every CSS Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Static and Relative Positioning
  2. Absolute Positioning and Containing Block
  3. Fixed and Sticky Positioning
  4. Z-index and Stacking Context
← Back to CSS Academy