Positioning and stacking
Positioning lets you anchor small interface details such as badges and tooltips. It is not a replacement for normal layout.
✓ What you will learn in this topic
- Explain the key idea: Positioning is best for anchored details; use layout systems for the main structure.
- Apply the concept: A small label sits in the demo’s upper corner; the demo remains in normal flow and keeps its layout space.
- Avoid this common mistake:
Concept explanation
static is the default. relative keeps an element in flow and provides an anchor for positioned descendants. absolute removes the element from normal flow and positions it against its nearest positioned ancestor. fixed anchors to the viewport; sticky behaves normally until a scroll threshold is reached.
z-index controls stacking within stacking contexts, not a universal global layer. Prefer a documented layer scale rather than escalating values until one element appears on top.
Syntax
A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.
.demo {
position: relative;
}
.demo::after {
content: "New";
position: absolute;
inset-block-start: 0.5rem;
inset-inline-end: 0.5rem;
}Example 1: a small page element
Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.
<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>.demo {
position: relative;
}
.demo::after {
content: "New";
position: absolute;
inset-block-start: 0.5rem;
inset-inline-end: 0.5rem;
}Expected result: A small label sits in the demoâs upper corner; the demo remains in normal flow and keeps its layout space.
Example 2: a practical interface card
The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.
<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>.card {
position: relative;
}
.card .badge {
position: absolute;
inset-block-start: 1rem;
inset-inline-end: 1rem;
z-index: 1;
}Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.
Common mistake
Absolutely positioning ordinary page content removes it from normal flow and can cause overlap as the page changes size.
Best practice
Use Grid or Flexbox for layout; reserve absolute positioning for intentional overlays anchored to a positioned component.
Practice exercise
Create a navigation row with flex layout and a card with a small badge positioned in its corner without removing the card from flow. Apply it to the example above and inspect the result at a narrow and a wide viewport.
Section summary
- Positioning is best for anchored details; use layout systems for the main structure.
- Use Grid or Flexbox for layout; reserve absolute positioning for intentional overlays anchored to a positioned component.
Knowledge check
What does position: absolute do to an elementâs normal-flow space?
Reveal the explanation
Absolute positioning removes the box from normal flow; anchor it to a suitable positioned ancestor.
Finish the module lessons, then take the server-scored module quiz to check your understanding.
Practical example
css.demo {
position: relative;
}
.demo::after {
content: "New";
position: absolute;
inset-block-start: 0.5rem;
inset-inline-end: 0.5rem;
}
Expected result:
A small label sits in the demo’s upper corner; the demo remains in normal flow and keeps its layout space.
Hands-on practice task
Create a navigation row with flex layout and a card with a small badge positioned in its corner without removing the card from flow.
In this section
Other lessons in Display and Positioning
Broaden your knowledge
Related learning topics
HTML
Learn to structure accessible, meaningful web pages with a complete HTML course from first document to professional application patterns.
JavaScript
Build strong JavaScript foundations with browser examples, focused practice, beginner projects, and module quizzes.