ELSEIF
Your brief EB
316 stories from 72 feeds 58 clusters Refreshed 5 minutes ago next pull 00:20

WEB Signal 622 2 feeds carried it

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

Illustration only Photo by Ronald Crow on Unsplash

A developer documented a JavaScript technique to center page elements relative to the full browser window rather than just the viewport, compensating for sidebars and browser chrome that make CSS-centered content appear visually off-center.

WHY IT MATTERS

CSS centering operates within the viewport, so any browser UI on one side shifts the apparent center toward that side, making technically correct layouts look wrong. This technique gives developers a way to correct for that offset, though it requires JavaScript and a pointer event on Chromium browsers before the adjustment can take effect.

Written by elseif from the cluster below · every claim links back to a source

The three things worth knowing

01

CSS grid makes viewport centering trivial, but browser sidebars and DevTools make viewport-centered content appear visually off-center within the full window.

02

A JavaScript approach using innerWidth and outerWidth fails when browser chrome exists on both sides because it cannot determine how the total is split between left and right.

03

Pointer events provide the missing coordinate by comparing screen position to client position, enabling calculation of the viewport's offset within the browser window.

THE CLUSTER

Same story, 2 feeds.

ORDERED BY FIRST SEEN
Lobsters we finally learned to center a div, then browsers added sidebars Open ↗
Hacker News We finally learned to center a div, then browsers added sidebars Open ↗