Absolute Positioning
By the end of this lesson you will be able to place an element exactly where a design asks, such as a discount badge on a photo corner, a strip along the bottom of a card, or a "Closed now" layer over a restaurant. You will know how the browser picks the box an absolute element measures from, how to stretch and centre with offsets, and the mistakes that make absolute elements fly to the wrong place.
You should know Relative Positioning first, because relative parents and absolute children work as a pair. We continue with TiffinGo, our imaginary food-delivery app from Pune. This time we build its restaurant cards.
What absolute does
position: absolute takes the element out of the normal flow. Three things follow from that:
- Its siblings behave as if it is not there, so they close up the space it used to take.
- Its parent does not grow to contain it. A parent whose only child is absolute collapses to zero content height.
- It is placed using
top,right,bottomandleft, measured from its containing block.
Two smaller changes surprise beginners. An absolute element is always treated as a block, so an absolute <span> accepts width and height. And when you do not set a width, it shrinks to fit its content instead of filling the line.
Think of a sticky note on a printed notice. The notice's text does not move to make room for the note. You decide where the note goes by measuring from the corners of the notice board, not from the text.
Finding the containing block
The containing block is the rectangle that the offsets are measured from. For an absolute element, the browser walks up the tree, parent by parent, and stops at the first ancestor that is positioned (relative, absolute, fixed or sticky).
Two details matter:
- The offsets are measured from that ancestor's padding edge, which is just inside its border. So
top: 0; left: 0puts the child in the corner inside the border, not on top of it, and the parent's padding does not push it in. - A few other properties also turn an ancestor into a containing block even when it is static, for example
transformandfilter. That matters most for fixed elements, so the next lesson covers it.
If no ancestor is positioned, the browser uses the initial containing block: a rectangle the size of the viewport, sitting at the very top of the document. It scrolls away with the page. That explains a classic bug:
/* No positioned ancestor anywhere */
.chat-help {
position: absolute;
bottom: 0;
right: 0;
}On a long page, this button appears at the bottom of the first screen, not the bottom of the page, and it scrolls up out of view as you scroll down. If you wanted it to stay on screen, you wanted position: fixed.
The inset shorthand
inset sets top, right, bottom and left in one line. It takes one to four values in the same clockwise order as margin:
.layer { inset: 0; } /* all four sides 0 */
.frame { inset: 10px 20px; } /* top and bottom 10px, left and right 20px */
.corner { inset: 8px 8px auto auto; } /* top 8px, right 8px, bottom and left auto */inset has worked in all major browsers since 2021 (Baseline 2021), so it is safe to use. In older code you will see the four properties written out separately; the result is the same.
Building a restaurant card
Step 1: anchor and badge
The photo wrapper becomes the anchor with position: relative. The badge is absolute and sits 0.6rem from the top-left corner.
Step 2: a strip that stretches
The delivery time strip uses left: 0; right: 0; bottom: 0 and no width. When an absolute element has both left and right, it stretches between them. This is different from relative positioning, where one of them is ignored.
Step 3: a button in the other corner
The heart button uses top and right. Here is the whole card:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Restaurant card with absolute parts</title>
<style>
*, *::before, *::after { box-sizing: border-box; }
body { font-family: system-ui, sans-serif; padding: 1.5rem; background: #fff8f0; }
.card { max-width: 20rem; background: white; border-radius: 14px; border: 1px solid #f0d9c4; }
.photo { position: relative; } /* Step 1: the anchor */
.photo img { display: block; width: 100%; height: auto; border-radius: 14px 14px 0 0; }
.badge { /* Step 1 */
position: absolute;
top: 0.6rem;
left: 0.6rem;
padding: 0.2rem 0.55rem;
border-radius: 6px;
background: #1d4ed8;
color: white;
font-size: 0.8rem;
font-weight: 700;
}
.eta { /* Step 2: stretched strip */
position: absolute;
left: 0;
right: 0;
bottom: 0;
padding: 0.35rem 0.75rem;
background: rgb(0 0 0 / 60%);
color: white;
font-size: 0.85rem;
}
.fav { /* Step 3 */
position: absolute;
top: 0.5rem;
right: 0.5rem;
width: 2.2rem;
height: 2.2rem;
border: none;
border-radius: 50%;
background: white;
color: #c1121f;
font-size: 1.1rem;
}
.info { padding: 0.75rem 1rem 1rem; }
.info h3 { margin: 0 0 0.25rem; }
.info p { margin: 0; color: #6b4f3a; }
</style>
</head>
<body>
<article class="card">
<div class="photo">
<img src="https://course.ethicscomputer.in/assets/samples/landscape-400.svg" alt="Garden seating at the restaurant" width="400" height="250">
<span class="badge">40% off up to ₹80</span>
<span class="eta">25-30 min · 2.1 km</span>
<button class="fav" aria-label="Save Sinhagad Bhojanalay">♥</button>
</div>
<div class="info">
<h3>Sinhagad Bhojanalay</h3>
<p>Maharashtrian thali · ₹250 for one</p>
</div>
</article>
</body>
</html>What you should see: a blue badge in the photo's top-left corner, a white round button in the top-right, and a dark strip across the full width of the bottom of the photo. The text below the photo is not pushed down by any of them. Now delete left: 0; from .eta and run again: the strip shrinks to fit its text, because without both sides set it has nothing to stretch between.
Covering and centring
inset: 0 on an absolute element makes it cover its containing block's padding box exactly. That is the standard way to build an overlay.
To centre a box of known size inside its containing block, combine inset: 0 with a width, a height and margin: auto. The browser shares the leftover space equally on all sides.
For a box whose size you do not know, the older trick still works: move the top-left corner to the middle with top: 50%; left: 50%, then pull the box back by half its own size with translate: -50% -50%. The percentages in translate refer to the element's own size, which is why this works.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Overlay and centring</title>
<style>
*, *::before, *::after { box-sizing: border-box; }
body { font-family: system-ui, sans-serif; padding: 1.5rem; background: #fff8f0; display: grid; gap: 1rem; }
.card {
position: relative;
max-width: 20rem;
height: 8rem;
padding: 1rem;
border: 6px solid #e9c46a;
border-radius: 14px;
background: white;
}
.closed { /* covers the padding box, inside the border */
position: absolute;
inset: 0;
border-radius: 8px;
background: rgb(255 255 255 / 75%);
}
.closed-label { /* known size: inset 0 + margin auto */
position: absolute;
inset: 0;
width: 9rem;
height: 2.2rem;
margin: auto;
display: grid;
place-content: center;
border-radius: 999px;
background: #3d405b;
color: white;
font-weight: 600;
}
.loading { /* unknown size: 50% + translate */
position: absolute;
top: 50%;
left: 50%;
translate: -50% -50%;
padding: 0.4rem 0.9rem;
border-radius: 6px;
background: #2a9d8f;
color: white;
}
</style>
</head>
<body>
<div class="card">
<strong>Kesar Sweets, Deccan</strong>
<p>Bakarwadi, amba barfi</p>
<div class="closed"></div>
<span class="closed-label">Opens at 8 am</span>
</div>
<div class="card">
<strong>Udupi Corner, FC Road</strong>
<span class="loading">Loading menu...</span>
</div>
</body>
</html>What you should see: on the first card, a pale layer hides the content but leaves the yellow border visible, because inset: 0 measures from the padding edge. A dark pill sits exactly in the middle. On the second card, the green "Loading menu..." box is centred, even though it has no fixed size. Change its text to something longer and it stays centred.
When not to use absolute
Absolute positioning ignores neighbours, so it breaks easily when content changes. A longer restaurant name or a bigger font size will not push an absolute badge away, and they may overlap. Use it for small parts that sit on top of a component: badges, close buttons, ribbons, overlays, decorative shapes. Lay out the page itself with CSS Flexbox Introduction and CSS Grid Introduction.
To place a tooltip or menu next to a specific button anywhere on the page, modern CSS offers Anchor Positioning, which builds on absolute positioning. When absolute layers overlap, Z-Index & Stacking Context decides which one is on top.
Common mistakes
- No positioned ancestor. The badge lands on the corner of the page. Add
position: relativeto the card. - Wrong ancestor. An inner wrapper is positioned, so the child anchors to it instead of the card. Check which ancestor is nearest.
- Parent collapses. A container with only absolute children has no height. Give it a height or an
aspect-ratio, or keep one child in the flow. - Expecting a width of 100%. An absolute element shrinks to fit. Set
left: 0; right: 0(orinset-inline: 0) to stretch it. - Using absolute for page layout. It looks fine at one screen size and overlaps at the next.
Interview questions
1. What is the containing block of an absolutely positioned element?
It is the padding box of the nearest ancestor whose position is not static. Some other properties, such as transform or filter on an ancestor, also create one. If there is no such ancestor, the initial containing block is used, a viewport-sized rectangle at the top of the document.
2. What happens to the space an absolute element used to take?
It disappears. The element is removed from the normal flow, so siblings move into its place and the parent does not grow to contain it.
3. How do you make an absolute element as wide as its containing block?
Set both left: 0 and right: 0 and leave width as auto; the element stretches between the two edges. inset: 0 stretches it in both directions. width: 100% also works, but it can overflow if the element has padding and uses content-box sizing.
4. How do you centre an absolutely positioned element?
For a known size, use inset: 0, set a width and height, and add margin: auto. For an unknown size, use top: 50%; left: 50% with translate: -50% -50%, since translate percentages refer to the element's own size.
5. Why does an absolute element with bottom: 0 sometimes appear in the middle of a long page?
Without a positioned ancestor, it is placed against the initial containing block, which is only as tall as the viewport and starts at the top of the document. So bottom: 0 means the bottom of the first screen, and it scrolls with the page.
Key points
absoluteremoves an element from the flow; siblings close up and the parent does not grow.- Offsets are measured from the padding edge of the nearest positioned ancestor.
- With no positioned ancestor, the initial containing block (first screen of the page) is used.
insetis the shorthand for all four offsets and is Baseline 2021.- Setting both
leftandrightstretches the element;inset: 0covers the containing block. - Centre with
inset: 0plusmargin: autofor known sizes, or50%plustranslatefor unknown sizes. - Use absolute for small layers on top of a component, not for page layout.
Exercise
Build a TiffinGo dish card with a photo. Add a "Pure veg" badge in the bottom-left corner of the photo, a "₹30 off" tag stretched across the top of the photo, and a round "+" add button that hangs half outside the bottom-right corner of the card. Then add a second card for a dish that is sold out: cover the whole card with a semi-transparent layer and centre a "Sold out" label of known size on it.
Show solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Dish cards</title>
<style>
*, *::before, *::after { box-sizing: border-box; }
body { font-family: system-ui, sans-serif; padding: 1.5rem; background: #fff8f0; display: grid; gap: 2rem; }
.dish { position: relative; max-width: 16rem; background: white; border-radius: 12px; border: 1px solid #f0d9c4; }
.photo { position: relative; }
.photo img { display: block; width: 100%; height: auto; border-radius: 12px 12px 0 0; }
.veg { position: absolute; left: 0.5rem; bottom: 0.5rem; background: #2d6a4f; color: white; padding: 0.15rem 0.5rem; border-radius: 4px; }
.off { position: absolute; top: 0; left: 0; right: 0; background: #e76f51; color: white; text-align: center; padding: 0.2rem; border-radius: 12px 12px 0 0; }
.add { position: absolute; right: -1rem; bottom: -1rem; width: 2.5rem; height: 2.5rem; border-radius: 50%; border: none; background: #1d4ed8; color: white; font-size: 1.4rem; }
.dish p { margin: 0; padding: 0.75rem; }
.sold-layer { position: absolute; inset: 0; border-radius: 12px; background: rgb(255 255 255 / 70%); }
.sold-label { position: absolute; inset: 0; width: 7rem; height: 2rem; margin: auto; display: grid; place-content: center; background: #3d405b; color: white; border-radius: 999px; }
</style>
</head>
<body>
<div class="dish">
<div class="photo">
<img src="https://course.ethicscomputer.in/assets/samples/landscape-400.svg" alt="Plate of sabudana vada" width="400" height="250">
<span class="veg">Pure veg</span>
<span class="off">₹30 off</span>
</div>
<p>Sabudana vada, ₹70</p>
<button class="add" aria-label="Add sabudana vada">+</button>
</div>
<div class="dish">
<div class="photo"><img src="https://course.ethicscomputer.in/assets/samples/landscape-400.svg" alt="Bowl of puran poli" width="400" height="250"></div>
<p>Puran poli, ₹90</p>
<div class="sold-layer"></div>
<span class="sold-label">Sold out</span>
</div>
</body>
</html>The badge and tag are measured from .photo, while the add button and the sold-out layer are measured from .dish, because each one anchors to its nearest positioned ancestor. Negative offsets let the button hang outside the card.
Quick Quiz
1. An absolute element's parent is static, but its grandparent is position: relative. What is it positioned against?
The browser uses the nearest positioned ancestor, skipping static ones.
2. What does left: 0; right: 0; do on an absolute element with width: auto?
For absolute elements, setting both sides stretches the box between them.
3. Where is top: 0; left: 0 measured from inside a positioned card with a 5px border and 20px padding?
Absolute offsets are measured from the containing block's padding edge.
4. Which pair centres a box of unknown size?
The translate percentages use the element's own size, so the box moves back by half its width and height.
Learn CSS with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
