we finally learned to center a div, then browsers added sidebars

Lobsters Hottest Tools

Summary

A blog post about a CSS/JS technique and a small tool that properly centers a div while accounting for browser sidebars and devtools, correcting the position using pointer events.

<p><a href="https://lobste.rs/s/ikkudb/we_finally_learned_center_div_then">Comments</a></p>
Original Article
View Cached Full Text

Cached at: 08/04/26, 11:51 PM

# we finally learned to center a div, then browsers added sidebars Source: [http://seg6.space/posts/center-div/](http://seg6.space/posts/center-div/) Centering a div used to require this little ritual: ``` .thing { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } ``` These days, it is almost disappointingly easy: ``` body { display: grid; min-height: 100dvh; place-items: center; } ``` I used that for the`\.site`div you’re reading\. It looked centered until I opened it in a browser with the sidebar visible\. The`\.site`div was still perfectly centered, just inside the wrong rectangle\. I figured the fix would be simple enough: JavaScript knows the width of both the webview and the browser window\. ``` window.innerWidth // the webview window.outerWidth // the whole browser window const browserChrome = window.outerWidth - window.innerWidth; ``` With the sidebar on the left, I could move`\.site`back by half of that difference: ``` const shift = -browserChrome / 2; ``` ``` .site { translate: var(--window-center-shift, 0px); } ``` That worked, right up until I opened DevTools\. Mine is docked on the right, so the width difference now included browser UI on both sides\. It gave me the total, but no way to tell how that total was split\. What finally gave me the missing coordinate was the pointer\. A trusted pointer event knows where it is on the screen and where it is inside the webview, which is enough to locate the webview inside the window: ``` const viewportLeft = event.screenX - event.clientX * scale; const viewportRight = viewportLeft + innerWidth * scale; const left = viewportLeft - window.screenX; const right = window.screenX + outerWidth - viewportRight; const shift = (right - left) / (2 * scale); ``` Firefox exposes the same viewport position directly\. Chromium does not, so this site starts with the left\-sidebar estimate and corrects it as soon as the pointer enters the page\. ## center, actually I wanted to try the same fix on pages I do not control, so I made[**center, actually**](https://github.com/seg6/center-div)\. It tries to find the centered element itself; if it guesses wrong, I can pick one\. The[demo](https://seg6.space/center-div/)is the simplest place to see the difference\.

Similar Articles

Fixing full-bleed CSS

Lobsters Hottest

A front-end developer discusses the imperfections of the classic CSS full-bleed utility class and presents modern fixes using CSS containment and container units to avoid scrollbar-related clipping issues.

CSS: Unavoidable Bad Parts

Lobsters Hottest

A personal blog post by a non-web developer discusses the unavoidable bad parts of CSS, including layout challenges, browser defaults, and wrapper overuse, while highlighting a learnable subset for simple tasks.

Getting Started with Anchor Positioning

Lobsters Hottest

A comprehensive guide to the CSS Anchor Positioning API, explaining how to anchor elements to other elements with `position-anchor` and `position-area` properties, covering common use cases like tooltips and dropdown menus.

On the <dl>

Simon Willison's Blog

A blog post by Ben Meyer shares new-to-me facts about the HTML <dl> element, including that multiple <dd> can follow a <dt>, grouping within <div>, ARIA labeling, and that they've been called 'description lists' since 2008.

How I'm using CSS View Transitions on this blog

Lobsters Hottest

The author details how to use CSS-only cross-document view transitions on a static blog to animate page navigation without JavaScript routing, including setting up layout columns and animations.