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

105 points
1/21/1970
8 hours ago
by seg6

Comments


JoshTriplett

The net result of what this site is doing seems to result in one of two effects when I open the sidebar, randomly:

1) the sidebar covers the content, while the content has blank space to the right, or

2) the content moves off the right edge of the screen, leaving a large blank space to its left.

Both of these are wrong. If I have a sidebar open, the site is now narrower, stop trying to be clever.

My immediate reaction to this is why is the browser giving the site this information, and could we stop.

7 hours ago

seg6

Agreed, both outcomes are bugs and neither is intended. The idea is simply that, content the site author intended to be horizontally centered stays literally centered on my screen when browser UI changes the viewport.

6 hours ago

Borealid

This is a nonsensical goal.

If I drag a browser window to the right, making it cover the right-hand half of my monitor, it serves no useful purpose for "centered" content to migrate to the left edge of the window (the physical midpoint of the screen).

Centered means centered within the virtual viewport, not the physical screen.

5 hours ago

heisenbit

Unless you have e.g. a kiosk with physical buttons below the screen an the labels on the screen.

28 minutes ago

weego

Kiosks by definition don't alow users to modify the ui. they run in kiosk mode

19 minutes ago

JoshTriplett

That results in the first outcome: the content is centered on the screen, which puts it under the sidebar, while there's perfectly good space being ignored off to the right of it.

It's a cute trick, please don't use it, this is one of many reasons why. If I have a sidebar open, it's because that space is not yours to care about.

5 hours ago

hulitu

> If I have a sidebar open, it's because that space is not yours to care about.

Drawing UI is hard. Firefox likes to draw the websites underneat the scrollbar. Google likes to draw system nabigation buttons over the window with all kind of funny interractions. That's what happenes when you train people "on the job".

2 hours ago

LiamPowell

> Firefox likes to draw the websites underneat the scrollbar.

This was a choice that they actively made. It's not hard by default, they just chose to do something stupid because they think it looks pretty.

2 hours ago

kqp

Think about users who don’t maximize the window, for whom centering to the screen, not viewport, would just be broken. It’s right to center to the viewport, and right that you’re able to turn that into centering to the screen by maximizing. The issue, I think, is that you don’t like that the sidebar changes the viewport, and really want a popup menu. I think your browser might duplicate sidebar functionality in configurable toolbar button popups, have you tried that? Failing that, I think the right solution to this problem might be a browser extension.

5 hours ago

hulitu

> Think about users who don’t maximize the window, for whom centering to the screen, not viewport, would just be broken.

This in blasphemy. Never, ever, think about the user. Users are idiots anyway. The apps shall be used for their intended purposes. All windows shall be maximized ( because we say so). /s

2 hours ago

wat10000

The author should not intend that, and if they do, my browser should do its best to put a stop to it.

6 hours ago

shijie

I'm fairly certain you just replied to the author

5 hours ago

alterom

I hope the author gets the point then.

5 hours ago

wat10000

The author referred to themselves in the third person, so I did the same.

5 hours ago

kccqzy

This might be some kind of a weird requirement for a piece of art, but I would literally never expect a website to center a div according to the browser window instead of the viewport. It just looks wrong and feels wrong.

8 hours ago

seba_dos1

Same. Thank heavens the website doesn't know the position of the browser's window in respect to the screen it's on.

7 hours ago

sheept

There's several web APIs for browser window positioning, like screenX/Y[0] (which is what OP uses), which allows for those classic pop-up based browser games from over a decade ago, like Browser Ball.[1]

[0]: demo: https://mdn.github.io/dom-examples/screenleft-screentop/

[1]: https://romanbaiocco.github.io/browser-ball-reloaded/

4 hours ago

seba_dos1

For this to work, the browser itself would have to know where its window is placed on the screen. Not all platforms provide such information.

4 hours ago

gmueckl

While providing this information to websites without user permission is probably a longstanding mistake of web browsers, windowing systems withholding proper on-screen positioning from native applications is a bug in that system - a bad one.

3 hours ago

happymellon

Why? The window and its position shouldn't matter to the app.

My tiling window manager doesn't care what your snowflake app thinks it wants, and I don't either.

2 hours ago

krick

Actually, they do, sort of. At least, if you use GLSL shaders. I remember some demo project using it in a way that made it appear that the website "sees" another window with the same website, which blew my mind when I saw it first. Cannot recall the name or anything else to locate it.

7 hours ago

m0nhawk

My guess is that you’re remembering this demo: https://xcancel.com/_nonfigurativ_/status/172732259457002734...

6 hours ago

krick

Yep, that's right.

6 hours ago

seba_dos1

The ability from this demo is Chromium-specific, needs extra permissions and has nothing to do with GLSL: https://developer.mozilla.org/en-US/docs/Web/API/Window_Mana...

5 hours ago

fluoridation

Wait until they realize that on mobile they can use accelerometer data to keep the element in the same position in space across translations of the device.

7 hours ago

Izkata

Translations? Not far enough, got to include rotations: https://developer.mozilla.org/en-US/docs/Web/API/Device_orie...

(And yes, CSS can rotate around all three axes, so you could make a div that resists rotating your device in addition to resisting translation...)

4 hours ago

jagged-chisel

Wait, I have to hold my phone how in order to see the div? ಠ_ಠ

7 hours ago

blamarvt

I assume you have to be facing the server...

7 hours ago

alterom

I mean, this could have applications, if you need to be facing a certain direction to read the content.

While the Silicon Valley is merely a figurative Mecca for all things tech, Apple could require people to be facing Cupertino if you want to watch a WWDC stream :D

5 hours ago

Izkata

Aww, looks like the magnetometer web API was abandoned: https://developer.mozilla.org/en-US/docs/Web/API/Magnetomete...

4 hours ago

dpkirchner

It's easy, you just need to hold your phone such that your eyes are pointed directly at the ads as they scroll up and down the screen. Thank you for consuming content.

6 hours ago

giancarlostoro

The same way Steve Jobs said to hold the iPhone.

7 hours ago

p1mrx

So the content falls down at 10 m/s^2 unless you're falling alongside it.

5 hours ago

happymellon

And running to counter earth rotation...

2 hours ago

fluoridation

But the problem is, you can never quite catch up to it without a rocket.

4 hours ago

valleyer

No, you can also take your clothes off and throw them upward with sufficient force.

3 hours ago

LocalH

Stop giving them ideas! :(

3 hours ago

1bpp

position: hyperfixed;

6 hours ago

silon42

It's also an extra information leak.

33 minutes ago

kazinator

Televisions used to have "sidebars", like a speaker on one side only, with some knobs co-located there also. Yet a test pattern would center in the picture tube, not the TV box.

5 hours ago

hulitu

Thank god there was no Google then. They would have put the knobs on the screen.

2 hours ago

seg6

The point of centering an element horizontally is to make sure that the content you’re viewing is directly in front of you. Browsers with a sidebar shift the content to the right, assuming the sidebar is on the left, which I’ve come to dislike! Others have expressed the same preference, hence this little snippet on my website.

7 hours ago

stevage

> The point of centering an element horizontally is to make sure that the content you’re viewing is directly in front of you.

That's an awfully big assumption. For starters, you're assuming that the browser window itself is centered on the screen, and that the device is centered in front of the user.

I don't think this is the point of centering an element horizontally at all, and the result you have created here is not at all what I as a user want.

7 hours ago

danillonunes

I don't usually use browsers sidebars, but when I do I mentally compensate for it. The viewport is in fact the part of the web page I expect to be the full width, and what things should be centered on.

The same way the vertical centering counts the viewport height only, not the url bar and the tab bar.

5 hours ago

scubbo

I salute your dedication to understanding and fixing a problem that bothers you - but I must add my voice to those who think that this is not a good design for general use. The viewport is the region within which content should be positioned, and thus within which they should be centred. By your logic, left-aligned content in a browser with a left sidebar should be deliberately covered.

7 hours ago

wizzwizz4

As a rule, you should not add functionality to a website to compensate for browser misfeatures. People who want the browser to work differently can get a different browser: that's why we call them "user agents". Making the web all complicated like this mainly serves to make it harder for people to use alternative browsers, or for browsers to change their misfeatures without breaking compatibility.

7 hours ago

seg6

Fair enough, that criticism resonates. I've decided to keep this behaviour only in the extension and have reverted it from the blog. Thanks!

6 hours ago

retrochameleon

Thank goodness

4 hours ago

LocalH

Stop worrying about what's happening outside the viewport. Your website has no right to care or even know what's happening outside the viewport.

Anyone got a Tampermonkey script or tiny extension to re-override this to make such sites actually center in the viewport, like I prefer?

3 hours ago

nkrisc

Joke’s on you: my browser window isn’t centered.

7 hours ago

sebastiennight

Ah but see, exploiting a Chromium API has let us know you've got this second screen on the side, so we're centering the div on your overall work space.

PS: if you switch from the touchpad to a wireless mouse, we'll move the div accordingly

4 hours ago

wat10000

This all sounds like a really elaborate way of not taking advantage of the fact that modern windowing operating systems don't require windows to be full screen, and allow you to position the sides of your browser window wherever you want them to be.

6 hours ago

stkdump

Funnily I can't reproduce the effect at all. I am using Firefox and with sidebar the content of the page centers on the remaining viewport width, like any other site does. I have also tested with Edge, same result. With Edge's auto collapse (i.e. non sticky vertical tabs), I get the described effect, but again this is like any other site. I was actually expecting quite bad results in Edge because it has some sidebars on the right and I thought this page compensates in the wrong direction then. Don't have Chrome installed to see if that does anything differently.

Note I like the default behavior of browsers to center on the viewport as opposed to the window treating sidebars as overlays over the page. Whenever pages do that with their own gadgets I hate it, because they often don't move their content out of the way and it makes it unreadable unless scrolled out of the range of these gadgets, which often isn't possible at all. And even when it is, it effectively reduces the total usable screen to a fraction.

Whenever I'm bothered enough (happens mostly on mobile), I use a kill sticky bookmarklet to fix that.

2 hours ago

krick

Screenshots would be helpful, because (you may be surprised to learn that) it is not entirely obvious which mode of behavior is supposed to be "wrong".

Anyway, I wouldn't say it currently behaves nicely (like something I would like to reuse anywhere whatsoever). Tested on Firefox opening Bookmarks on the left. First off, there is this ugly flicker, as browser first renders the "wrong" version, then your JS "fixes" it. Second, there is some concurrency problem, as by opening and closing Bookmarks repeatedly I somehow managed to render both versions: centered to viewport and covered by sidebar.

And, yeah, as others have said, I also think that allowing sidebar to cover your webpage content is absolutely bogus and don't foresee myself wanting this behavior, ever. But that's up to you.

7 hours ago

seg6

I’ve added GIFs showing the intended behavior. The brief movement doesn’t bother me, opening or closing a sidebar already resizes the webview and causes pages to reflow in the browsers I use. Ending in the wrong state after repeatedly toggling it is a bug. And yeah, if you prefer viewport centering, there’s no reason you’d want to use the extension.

6 hours ago

jdiff

Not trying to argue you out of your preference, but trying to understand it: Why would you want to be able to accidentally hide parts of the page by having a sidebar open, or extended too far? I don't understand why the page should pretend to take up the whole window and ignore that it does truly live in just a viewport. I get why aesthetically it might be pleasing, but isn't that pretty short-lived when faced with serious practical drawbacks?

4 hours ago

fluoridation

I had no idea what this was talking about at first because I had JS blocked, so nothing special seemed to happen when I opened the bookmarks panel. This looks absolutely goofy. I don't know why you'd want the content potentially getting covered by unrelated UI elements.

7 hours ago

hulitu

> I don't know why you'd want the content potentially getting covered by unrelated UI elements.

Well, if Google and Firefox does it, it must be good. Ah, yes, I forgot Microsoft with its helpfull transient "We just introduced new bugs. < remind me later > windows.

This is industry standard. I wish I was being sarcastic.

2 hours ago

busymom0

> This looks absolutely goofy. I don't know why you'd want the content potentially getting covered by unrelated UI elements.

Here in Canada, the mobile website for anything related to Indian immigration (Indian passport, visa etc) is done via this absolutely goofy website with multiple levels of UI elements and popups covering the content. Have a look on mobile:

https://www.blsindia-canada.com/ottawa-jurisdiction/index.ph...

7 hours ago

crtasm

Meanwhile this website has learned how to break mouse scrolling.

7 hours ago

inventor7777

What do you mean? On macOS Safari I see a scroll bar that I can drag with my mouse if I wish.

6 hours ago

crtasm

Scrolling the page with mousewheel/multitouch doesn't work unless the pointer is inside the white area

...appears it's been fixed since my original comment. There's still no scrollbar in the expected place on the side of my browser window though.

5 hours ago

drewbeck

You’ve effectively created a slide over sidebar. It’s an interesting hack to get there but IMO “should” properly be the domain of the browser.

5 hours ago

alterom

See, I'm keeping my siidebar open most of the time, so I WANT it to cover the content that would otherwise be visible - said no one ever.

But yay, it's centered relative to the window now! God forbid the user tiles their windows, because it breaks the aesthetic the website author intended.

5 hours ago

saghm

I'm confused about why this website is using less than half of my window for content. I understand wanting some margins, but a third of my window on each side seems pretty egregious. At least it's centered though!

4 hours ago

woodrowbarlow

wrapping text at 55-75 characters per line is often recommended in digital typography by UX. research implies that an optimum length exists to maximize reading speed, and although it depends on many factors 55-75 is often parroted as a universal rule.

3 hours ago

hulitu

> wrapping text at 55-75 characters per line

What happened to 80 ? Ok, 75 is "close enough", but 55 ?

2 hours ago

cluckindan

80 is a terminal standard, 55-75 is UX.

an hour ago

Brajeshwar

One of my favorite interview questions was asking how many ways a developer can center a DIV or any block element in a container/browser with pre-defined height, or responsive heights. I have special soft corner heart for people who said TABLES.

2 hours ago

boomlinde

The demo page doesn't work as described in Firefox when I open my bookmarks or history sidebar. It displays a correction offset, but Firefox valiantly defeats this evil hack and makes sure that the box is still at the center of the viewport as the viewport shrinks. It doesn't work in Chromium either when I open devtools as a sidebar. Correction resets to "move pointer" when I do.

I could appreciate the hack from an engineering perspective if it actually worked, but from a user perspective, this is just another example of overambitious anti-designers going out of their way to replace whatever simple, sane and predictable defaults a browser provides out of the box with something incredibly dumb. Browsing has become a rather paranoid activity of constantly second guessing what bespoke bullshit the website authors might have attached to operations that otherwise have previously established sane behaviors provided by the browser by default.

an hour ago

bmitc

And then people added unhidable chat bot icons and even windows that hide entire parts of the interface with no recourse.

31 minutes ago

ezwoodland

Websites should be thought of as VMS and shouldn't be trying to reverse engineer host state.

5 hours ago

kazinator

What's the substantial difference betweeen "browser added sidebar" and "the browser has allowed height and width of the window to be resized since the dawn of the web"?

5 hours ago

skgough

I think the scrollbar-gutter property helps with this. https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/P...

7 hours ago

jszymborski

margin:0 auto is the (probably incorrect) incantation I have always reached for

8 hours ago

sodapopcan

That's fine but it only centres horizontally. Usually when people talk about "the div centring problem" they mean x and y, dead centre (as this article is).

8 hours ago

saghm

The article only seems to very briefly mention the vertical axis, with pretty much all of it focusing on interactions with the sidebar (which only affects the horizontal width).

4 hours ago

proc0

Z-index has been invented.

2 hours ago

songhonglei1985

hah the pointer trick is clever. tbh ive been living with everything looking 5px off for years and just... accepted it. does this work with vertical tabs in firefox?

4 hours ago

danielvaughn

Am I the only person that loathes sidebars in browsers? Maybe it's because I've been using a browser since 1996, but my brain is deeply set in the idea that the space below the chrome 110% belongs to user space.

I've tried a few browsers with vertical tabs, and my brain keeps seeing them as part of the page itself. It's a very distracting and uncomfortable experience.

5 hours ago

Dylan16807

Do you always have browsers maximized? If you're used to other windows next to the browser then I'd be surprised if you still find sidebars distracting.

3 hours ago

josefrichter

Centering a div? Impossible.

8 hours ago

qingcharles

GPT-6 will solve this.

4 hours ago

gerdesj

I'm 55 years old. My eyes are not what they used to be and neither are my glasses. I also own a copy of "Usage and Abusage" but I'll pass on that. Click on story link. My laptop renders a page - OK its black text on a white background (thank you but I doubt its a deliberate boon) ...

... why on earth is the offering a "page" ie emulating a printed page? In portrait!

I'll accept that most people are not fixated about grammar, punctuation or basic decency: that's fine.

However a "page" in portrait that looks awful on a landscape screen is unpleasant. You can easily request my viewing parameters and adjust accordingly. It's even worse when you are dispensing advice about www page layouts.

6 hours ago

sherry-sherry

Longer lines of text are harder to read. There's a reason newspapers, books, articles, etc are all vertical and often have a similar amount of words per line. It's easier to follow, scan, and find your place.

Some more on this below:

Optimal Line Length in Reading — A Literature Review (https://journals.uc.edu/index.php/vl/article/view/5765)

Practical Typography — Line Length (https://practicaltypography.com/line-length.html)

5 hours ago

seg6

I’ll accept that not everyone likes a portrait page on a landscape screen, that’s a matter of taste. But if you’re going to invoke Usage and Abusage, grammar, punctuation, and "basic decency", it would help to identify an actual error. And perhaps write "it’s" correctly while doing so. I’m also unclear what moral failing you think an A4 aspect ratio represents.

6 hours ago

SpyCoder77

There needs to be images on this

7 hours ago

LocalH

Nah, I'd rather not. I want sites to center within the viewport, not the browser window. I keep my tabs in a vertical sidebar, and I treat the viewport as "the browser". I want things that should be centered to be centered within that viewport.

3 hours ago

dyauspitr

Jokes on you, I never learned and now LLMs just know how to do it instead.

6 hours ago

seg6

A bit of context, since this is much less serious than it may have sounded:

I use a single browser instance in a tiled window manager, occupying a fixed window directly in front of me, usually with a sidebar open. When a site intentionally centers a fixed-width container, the sidebar leaves that container visibly off the dead center of my screen. I happen to dislike that. This is a personal aesthetic preference for my particular setup, not a claim that viewport centering is generally wrong.

The intended behavior does not remove content or replace responsive layout. The sidebar still narrows the webview and the page reflows normally, the correction only repositions the container that was already meant to be centered, so its center remains in the same physical place. If content ends up covered, offscreen, or stuck in the wrong state, that’s a bug.

I don’t particularly mind the brief movement while toggling a sidebar, since the browsers I use already visibly resize and reflow pages while doing so.

I’m considering reverting the behavior on the blog and keeping it extension only. I’ve also added GIFs to the post showing the intended effect.

6 hours ago