What are the values of the position property?
static(the default),relative(offset from its normal spot, space kept),absolute(out of flow, placed against the nearest positioned ancestor),fixed(out of flow, placed against the viewport) andsticky(in flow until it reaches its offset in its scroll container, then sticks). Anything butstaticcounts as positioned. More in position.How is an absolutely positioned element placed?
Out of the normal flow, against its containing block: the nearest ancestor whose
positionis notstatic, or one with atransform,filterorcontain: paint. With none, it uses the initial containing block. Hence relative parent, absolute child:HTML<style> .card { position: relative; width: 220px; padding: 16px; background: #f3f4f6; border-radius: 8px; } .badge { position: absolute; top: -8px; right: -8px; background: crimson; color: white; border-radius: 999px; padding: 2px 8px; font-size: 12px; } </style> <div class="card"> Notifications <span class="badge">3</span> </div>position: relativealone does not move the parent; it only makes it the reference.Why is my position: sticky not working?
It sticks within its nearest scrolling ancestor, and only while its parent is on screen. Usual causes:
- No
top(or other offset). - An ancestor has
overflow: hidden,autoorscrolland becomes the scroll container. - The parent is no taller than the sticky element.
- In a flex row it stretches to full height; add
align-self: flex-start.
HTML<style> .scroller { height: 160px; overflow: auto; border: 1px solid #9ca3af; } .head { position: sticky; top: 0; background: #2563eb; color: white; padding: 6px; } .scroller p { margin: 0; padding: 10px; border-bottom: 1px solid #e5e7eb; } </style> <div class="scroller"> <div class="head">Sticky header: scroll this box</div> <p>Row 1</p><p>Row 2</p><p>Row 3</p><p>Row 4</p><p>Row 5</p><p>Row 6</p><p>Row 7</p> </div>- No
Why does my z-index: 999 not bring the element to the front?
Because
z-indexonly competes inside one stacking context: if an ancestor makes one with a lowz-index, nothing inside it can rise above that ancestor's siblings.HTML<style> div { color: white; padding: 10px; } .parent { position: relative; z-index: 1; background: #93c5fd; width: 220px; } .child { position: relative; z-index: 999; background: #2563eb; width: 160px; } .sibling { position: relative; z-index: 2; background: #f97316; width: 200px; margin: -30px 0 0 60px; } </style> <div class="parent"><div class="child">child, z-index: 999</div></div> <div class="sibling">sibling, z-index: 2</div>The real contest is parent (1) against sibling (2). Stacking contexts come from
z-indexon a positioned, flex or grid item,position: fixedorsticky,opacitybelow 1,transform,filterandisolation: isolate. More in z-index.How does float work, and what is a clearfix?
floatpushes an element to one side and lets text wrap around it. Floats leave the flow, so a parent holding only floats collapses to zero height; a clearfix makes it contain them.HTML<style> .box { border: 2px solid #2563eb; margin-bottom: 30px; } .img { float: left; width: 80px; height: 80px; background: #f97316; margin-right: 8px; } .contained { display: flow-root; } </style> <div class="box contained"><div class="img"></div>With display: flow-root the box grows to contain the float.</div> <div class="box"><div class="img"></div>Without it the border ignores the float.</div>The old clearfix was a
::afterwithclear: both;display: flow-rootnow does it in one line. More in float.Why is my position: fixed element not fixed to the viewport?
An ancestor with
transform(eventranslateX(0)),filter,backdrop-filter,perspective,contain: paint, orwill-changefor one of those, becomes the containing block for fixed descendants, which then stick to it instead of the viewport.HTML<style> .panel { height: 120px; border: 2px dashed #9ca3af; margin-bottom: 8px; padding: 6px; } .moved { transform: translateX(0); } .toast { position: fixed; bottom: 8px; left: 8px; background: #111827; color: white; padding: 6px 10px; } </style> <div class="panel moved">This box has a transform, so its toast is trapped at the box's bottom left. <div class="toast">trapped</div> </div> <div class="panel">This box has none, so its toast is fixed to the bottom left of the preview window. <div class="toast">viewport</div> </div>Render modals at the end of
body(a React portal), or use<dialog>withshowModal(), which draws in the top layer.What is the difference between position: fixed and position: sticky?
fixedtakes the element out of the flow and pins it to the viewport from the start, so content slides under it and you usually add padding to make room.stickystays in the flow and keeps its space; it scrolls normally until it reaches its offset (top: 0), then sticks, and only while its parent is on screen. Usefixedfor a floating chat button,stickyfor a table header or section heading. More in position.How do you make an absolutely positioned element cover its parent?
Give the parent
position: relativeand the childposition: absolute; inset: 0.insetis shorthand fortop,right,bottomandleft, so all four edges stick to the parent's padding box and the child stretches with no width or height.HTML<style> .card { position: relative; width: 240px; height: 100px; padding: 12px; background: #2563eb; color: white; border-radius: 8px; } .overlay { position: absolute; inset: 0; display: grid; place-items: center; background: rgb(0 0 0 / 0.55); border-radius: inherit; } </style> <div class="card"> Card content <div class="overlay">Loading...</div> </div>With a fixed width and height,
inset: 0plusmargin: autocenters the child instead.Does z-index work on an element with position: static?
No.
z-indexapplies only to positioned elements (anything butstatic) and to flex and grid items, so on a plain block it is ignored. Addposition: relative, which does not move the element.HTML<style> .pair { margin-bottom: 16px; } .box { width: 160px; height: 50px; color: white; padding: 6px; } .a { background: #2563eb; z-index: 10; } .b { background: #f97316; margin: -30px 0 0 40px; position: relative; z-index: 1; } .fixed .a { position: relative; } </style> <div class="pair"><div class="box a">z-index: 10, static</div><div class="box b">z-index: 1</div></div> <div class="pair fixed"><div class="box a">z-index: 10, relative</div><div class="box b">z-index: 1</div></div>In the first pair the orange box covers the blue one despite its lower number.
What is the difference between float and position: absolute?
Both take the element out of the normal flow, but a float still pushes on its neighbors: text and inline content wrap around it. An absolutely positioned element is removed completely: nothing wraps around it, the content behaves as if it were not there, and you place it with offsets against its containing block. Use
floatto wrap text around an image,absolutefor overlays and badges, and neither for page layout, which is a job for Flexbox and Grid.
CSS Position, z-index and Float Interview Questions
The five position values, absolute children, sticky bugs and stacking contexts.
10 questions