[{"data":1,"prerenderedAt":6},["ShallowReactive",2],{"post-content-css-position-sticky-not-working":3},{"content":4,"lastModified":5},"\u003Cp>My docs sidebar stopped sticking the same week I fixed a horizontal scrollbar.\u003C\u002Fp>\n\n\u003Cp>The scrollbar existed only on phones. One wide code sample pushed the page sideways by a few pixels, so I did what everyone does: put \u003Ccode>overflow-x: hidden\u003C\u002Fcode> on the layout wrapper and got on with my life. Two weeks later a reader emailed to say the table of contents on my docs page just scrolled away with everything else.\u003C\u002Fp>\n\n\u003Cp>Nothing else in that stylesheet had changed. That is the annoying part about sticky. No error, no warning. A box that fails to stick simply behaves like \u003Ccode>position: relative\u003C\u002Fcode> and the console stays quiet.\u003C\u002Fp>\n\n\u003Cimg src=\"https:\u002F\u002Fimages.pexels.com\u002Fphotos\u002F6991352\u002Fpexels-photo-6991352.jpeg?auto=compress&cs=tinysrgb&w=1600\" alt=\"Four colored sticky notes pressed flat against a white wall\" loading=\"lazy\" \u002F>\n\n\u003Ch2>What sticky is actually anchored to\u003C\u002Fh2>\n\n\u003Cp>A sticky element sits in normal flow until its nearest scrolling ancestor crosses the inset you gave it, then it pins. Two boxes decide what you see: the scrollport it measures against, and the containing block it is not allowed to leave.\u003C\u002Fp>\n\n\u003Cp>Every \"sticky does not work\" case I have hit lived in one of those two boxes. The property has shipped in every browser I care about for the better part of a decade. So I stopped reading the rule and started walking up the DOM tree.\u003C\u002Fp>\n\n\u003Ch2>Failure one: an ancestor became the scrollport\u003C\u002Fh2>\n\n\u003Cp>Here is the part almost nobody mentions. Overflow is a two-axis thing, and one axis drags the other with it. The computed value notes on \u003Ca href=\"https:\u002F\u002Fdeveloper.mozilla.org\u002Fen-US\u002Fdocs\u002FWeb\u002FCSS\u002Foverflow\" rel=\"noopener noreferrer\" target=\"_blank\">MDN's overflow page\u003C\u002Fa> spell it out: if one axis is set to something other than \u003Ccode>visible\u003C\u002Fcode> or \u003Ccode>clip\u003C\u002Fcode>, the \u003Ccode>visible\u003C\u002Fcode> value on the other axis computes to \u003Ccode>auto\u003C\u002Fcode>.\u003C\u002Fp>\n\n\u003Cp>So \u003Ccode>overflow-x: hidden\u003C\u002Fcode> is not a horizontal-only decision. It turns the wrapper into a scroll container on both axes. The sticky child now pins to that wrapper instead of the window, and the wrapper never scrolls. Your header scrolls away and looks broken.\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-css\">\u002F* clips the overflow and quietly kills every sticky child *\u002F\n.page { overflow-x: hidden; }\n\n\u002F* clips identically, creates no scroll container *\u002F\n.page { overflow-x: clip; }\n\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>Each variant went into headless Chrome with the window scrolled 400px, reading \u003Ccode>getBoundingClientRect().top\u003C\u002Fcode>. Pinned is \u003Ccode>0\u003C\u002Fcode>, anything negative means it left.\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-text\">case                                top after a 400px scroll\nno wrapper, plain sticky header                              0\nwrapper with overflow-x: hidden                           -250\nwrapper with overflow-x: clip                                0\nbody with overflow-x: hidden                                 0\nhtml + body with overflow-x: hidden                       -250\n\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>That last row is the one that cost me an afternoon. I had assumed putting the rule on both \u003Ccode>html\u003C\u002Fcode> and \u003Ccode>body\u003C\u002Fcode> was the tidy version. It is the broken version. With only \u003Ccode>body\u003C\u002Fcode> set, browsers hand that overflow to the viewport and the body box stays out of the way. Set it on \u003Ccode>html\u003C\u002Fcode> too and the body keeps its own scroll container, becomes the scrollport, and sticky dies again.\u003C\u002Fp>\n\n\u003Cp>Which is why my rule now is: clip the wrapper, leave the body alone.\u003C\u002Fp>\n\n\u003Ch2>Failure two: there is nowhere to travel\u003C\u002Fh2>\n\n\u003Cp>Sticky movement is bounded by the containing block. If the parent is exactly as tall as the sticky child, the offset has nothing to travel through and the box just scrolls away like any other element. The classic way this happens is a sidebar inside flex or grid.\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-css\">.layout { display: flex; }\n.sidebar { position: sticky; top: 0; width: 220px; }\n\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>\u003Ccode>align-items\u003C\u002Fcode> defaults to \u003Ccode>stretch\u003C\u002Fcode>, so the sidebar stretches to the height of the row. Now it is as tall as its own parent and there is nothing to slide through. Measured: a 2000px sidebar inside a 2000px layout, at 400px of scroll the box sat at \u003Ccode>-250\u003C\u002Fcode>. Grid with the default stretch did the same thing.\u003C\u002Fp>\n\n\u003Cp>One line fixes it, and it is a line I now write by reflex on every sidebar.\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-css\">.sidebar { align-self: start; }\n\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>Same scroll, same element, top is \u003Ccode>0\u003C\u002Fcode>. The height also collapses back to its content, which is what you wanted visually anyway.\u003C\u002Fp>\n\n\u003Ch2>The check that takes ten seconds\u003C\u002Fh2>\n\n\u003Cp>Paste this in the console, change the selector, and it names every ancestor that could be holding your element hostage.\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-javascript\">const el = document.querySelector('.toc');\nfor (let n = el.parentElement; n && n !== document.documentElement; n = n.parentElement) {\n  const s = getComputedStyle(n);\n  if (\u002Fauto|hidden|scroll\u002F.test(s.overflowX + s.overflowY)) {\n    const realScroll = n.scrollHeight > n.clientHeight || n.scrollWidth > n.clientWidth;\n    console.log(n, s.overflowX, s.overflowY, realScroll ? 'actually scrolls' : 'dead scrollport');\n  }\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>The dead scrollport line is the one to look for. My wrapper never overflowed vertically and never scrolled a pixel, and it still broke sticky. Staring at the page will never find it.\u003C\u002Fp>\n\n\u003Ch2>Two smaller things\u003C\u002Fh2>\n\n\u003Cp>A sticky element with no inset does nothing at all. It needs one of \u003Ccode>top\u003C\u002Fcode>, \u003Ccode>right\u003C\u002Fcode>, \u003Ccode>bottom\u003C\u002Fcode>, \u003Ccode>left\u003C\u002Fcode> set on the axis you care about, otherwise the box is relatively positioned with extra words. Measured \u003Ccode>-250\u003C\u002Fcode>, identical to having no declaration. I have shipped that bug while convinced the CSS was fine, because the declaration was there and the value was not.\u003C\u002Fp>\n\n\u003Cp>Sticky table headers work now, by the way. Every few months I find a post claiming \u003Ccode>border-collapse: collapse\u003C\u002Fcode> breaks sticky \u003Ccode>th\u003C\u002Fcode>. I tested it in Chrome 157 and the header pinned at \u003Ccode>0\u003C\u002Fcode> like any other box. If the divider under your header looks wrong once it pins, stop fighting the border and draw the line with an inset \u003Ccode>box-shadow\u003C\u002Fcode> instead.\u003C\u002Fp>\n\n\u003Ch2>The follow-on bug nobody warns you about\u003C\u002Fh2>\n\n\u003Cp>Once the header sticks, in-page links break. Click a heading in your table of contents and the browser scrolls it to the top of the scrollport, which is now underneath the header you just pinned.\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-css\">html { scroll-padding-top: 72px; } \u002F* header height plus a little air *\u002F\n\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>One declaration, applies to every anchor and to keyboard focus, no JavaScript. Use \u003Ccode>scroll-margin-top\u003C\u002Fcode> on the targets instead if only some sections need the offset.\u003C\u002Fp>\n\n\u003Cp>The property is fine. Every one of these failures looks the same on screen, which is why the fix is always a search and rarely a syntax question. Walk the ancestors before you touch the rule.\u003C\u002Fp>\n\n\u003Cp>I keep that console walker in \u003Ca href=\"\u002Fsnippetark\u002F\">Snippet Ark\u003C\u002Fa> so I am not retyping it on someone else's machine at 11pm. If you have been fighting stacking order too, the \u003Ca href=\"\u002Fposts\u002Fz-index-not-working-stacking-context\u002F\">z-index write-up\u003C\u002Fa> covers the other half.\u003C\u002Fp>\n","2026-11-13",1794910420005]