Hacker News new | past | comments | ask | show | jobs | submit login
CSS adds vertical centering in 2024 (build-your-own.org)
136 points by thunderbong 8 months ago | hide | past | favorite | 44 comments



Still surprised it took so long. Feels like the folks working on CSS have some really weird prioritites when it comes to deciding what to work on and what to add.


Vertical centering has been trivial in numerous ways for many years. At least since flexbox. This is mostly a convenience to achieve what flexbox already did, with a simpler (albeit more error prone) imposition on document structure.

With that in mind, which “really weird priorities” would you have deprioritized relative to this convenience?


I don't think it's fair to dismiss something as "convenience". Convenience is very important in development, convenience is the reason we use CSS to begin with.

The idea of centering an element within another is extremely basic. Something someone might want to do literally within the first 30 minutes of touching CSS for the first time and historically that person, who probably expected some evident answer instead found a 5000 vote post in stackoverflow with information on flexbox, margin auto, text align and half a dozen hacks for different situations.

Like I can do a js implementation of array.has() in a couple of minutes. I would still be shocked if it was never implemented by default and it got implemented now.


All of what you say is reasonable, as written. But I also don’t think it’s fair to characterize my comment as dismissing convenience. I think “really weird priorities” is a strong claim, and I think it deserves perspective and warrants questioning.


Indeed, since at least 1997, when HTML 3.2 standardized tables.


I'm also frequently surprised at how long certain CSS features take to implement. Some recent examples for me are the `has:()` pseudo selector [1], the scripting media queries [2], and css nesting [3].

[1] https://caniuse.com/css-has

[2] https://caniuse.com/mdn-css_at-rules_media_scripting

[3] https://caniuse.com/css-nesting


The `scripting` @media query isn't very useful. If you want CSS that's in effect when the browser doesn't have JavaScript, you can put those styles inside <noscript>. More commonly, use JavaScript itself to add or remove a class that is used within the relevant selectors. Only using JavaScript itself is potentially effective when script-blocking extensions are used rather than JavaScript not being available at all.

`:has()` is very clever, powerful, and has been desired for a long time. However, even if someone figured out how to implement it twenty years ago, it probably still couldn't have been used for performance reasons.

CSS nesting may have also been infeasible for performance reasons.


> The `scripting` @media query isn't very useful. If you want CSS that's in effect when the browser doesn't have JavaScript, you can put those styles inside <noscript>.

If your styles are defined within external stylesheets, then having to extract them from their respective stylesheets and place them directly in a noscript DOM node is not very ergonomic (and it will also affect your ability to cache those styles, since they're now a part of your potentially dynamically generated HTML rather than a static stylesheet).

> use JavaScript itself to add or remove a class that is used within the relevant selectors.

Well yeah, If you're ok with adding JS then most native CSS features can be polyfilled. This is a very imperative solution to a problem that the scripting media query handles declaratively. I would much rather have my styles dictated by my stylesheets, and reserve my JS for adding interactivity.

> However, even if someone figured out how to implement it twenty years ago, it probably still couldn't have been used for performance reasons.

Yeah the has selector is similar to a lookahead assertion in a regular expression, and CSS nesting is similar to a lookbehind (both of which can be computationally expensive). I'm not sure browsers needed to wait until the 2020s to deliver these features though. I would have loved to ditch SASS and SCSS years ago.


When the CSS is only to be used when JavaScript runs, I'm okay with using JavaScript to make it active. Again, the media query only does its thing when the browser says JavaScript isn't supported, if the page's JavaScript doesn't run because the .js file was blocked or there was a network issue, you get the "JavaScript styles" without the JavaScript.

Yes, there's probably been enough device performance to handle things like :has() and CSS nesting for a while. Another factor may have been IE; as long as there was a browser that wouldn't add features but still had to be supported, interest in expanding CSS spec in ways that couldn't easily rely on CSS parsers ignoring code they don't understand may have been limited.


> Again, the media query only does its thing when the browser says JavaScript isn't supported

I get how it works, it just feels like a hack designed to work around a missing feature, rather than the ideal way to handle the problem. Phrased another way: if the scripting media query was ubiquitous I would never go back to toggling classes via JS.

> Another factor may have been IE;

Browsers have been shipping features that don't work in IE for the better part of the last decade. If you think CSS nesting would break IE, imagine what IE would do when it encounters a modern JS feature that can't be polyfilled like Proxies.


> if the scripting media query was ubiquitous I would never go back to toggling classes via JS

I am asserting that in the majority of cases where a person's browser is not running a page's JavaScript, it is because of a network failure or intentional blocking, neither of which can be detected by the media query.

Therefore, three years from now when the media query will be ubiquitous, it won't prevent your styles that are meant to be used only with your JavaScript from being used most of the time.


The `@media (scripting: none)` query absolutely can detect if a user has disabled JS on the page via their browser's settings (e.g. via chrome://settings/content/javascript). If you mean to say that it can't detect when a third party extension has disabled scripts, well then I would say that's personally out of scope for me. Extensions can do all sorts of nefarious things, the least of which is disable JS. As far as network reliability is concerned, if the user can't download fonts, media files, stylesheets, scripts, and linked pages, then toggling classes is probably the least of my concerns.


This was already a feature for flex box and grid. They’re just adding this to flow layout. Still a welcome addition but consider that all browsers that add support for this will already have support for flex and grid.


Yes, but imagine if you're new to CSS how unintuitive and annoying it is that you need to figure out what display modes mean and how they behave in order for a property like `align-content` to work as expected? That's a weird thing about css in my opinion.


The article already addresses this. The point is to have the feature exist without having to know about flexbox/grid (or history).


Learning flex box has great ROI for saving your time and sanity wrangling CSS.


Bizarre that a statement of fact is being voted down.


Didn’t downvote (can’t even downvote, wouldn’t anyway), but if I were to guess it’s that the article talks about it fairly extensively.


Yeah … but that's also what makes the title clickbait — that the article itself knows that what is written isn't true. "CSS makes vertical centering easier in 2024" or just "CSS makes vertical centering easier" would have been more honest, I think.


It's kind of funny seeing center-aligned "align-content just works!" aligned-left within the div in Firefox ESR. This is definitely a bleeding edge feature but a welcome one. It'll be some years before I chose to use it.

edit: It's in the top left, not centered horizontally or vertically.


Firefox has supported this since 125. So if you are on ESR 128 it should work fine

https://caniuse.com/mdn-css_properties_align-content_block_c...


That is what the article claims, but... I see it as left-aligned. Firefox 129.0.1 on MacOS.


It should be left aligned, but it should also be vertically centered within the resizable black-bordered box.


I've just looked at it in Firefox 129 and it's vertically centered.


Per the article, _align_ is for vertical distribution, and _justify_ is for horizontal. The example is actually behaving correctly. The naming is confusing though, imo


Ah! Thanks for the correction. Unfortunately it's not centered vertically either. It's in the top left.


Oh lol that’s not great


Well, to center the text in flow layout, you would use text-align:center;


You do realize this is about vertical alignment, right?


Why don’t top and bottom `auto` margins center things?


Because our writing system is unidirectional. Documents have a direction that they flow in (typically vertical), and text within those documents is written along the other axis (typically horizontal) and wraps when it hits the edge. So in a viewport, a document has three fixed edges defined by the viewport dimensions (typically top, left, and right), with the other edge (bottom) defined by the size of the content. So the horizontal axis works in a very different way to the vertical axis.


Then why is 'auto' a valid value for the bottom margin?


Doesn’t seem like that would play nice with margin collapsing, like what if you had two box elements with that style that would otherwise undergo margin collapsing, how should they render? What’s the baseline margin to combine if they should collapse and what’s the baseline margin to separate them if they shouldn’t?


Just let me opt out of margin collapsing. That may be helpful if we’re dealing with paragraphs on a long piece of prose, but that’s decidedly not what I’m dealing with 95% of the time.

Even with all the cool layout props we have today, it’s way too complicated to say „make this thing fill the whole screen minus the navbar at the top, make it scrollable if the content overflows and Center it vertically otherwise“. Add a grid or sticky elements or z index modifications to the mix, and the quirks get loud fast.


You can use flex or grid to opt out of margin collapsing and get your easy center alignments and justifications.

You’re right that it’s super hard to accomplish that but you have to remember it’s also hard to accomplish that in real life if you’re mathing it out. It’s easy to look at something and put something in its center (probably not accurately but convincingly enough) but to automate it is a different case, especially when the DSL for doing so 1) must handle so many other structuring features, as you noted, 2) was originally designed for structuring text documents rather than applications, as you noted, and 3) must maintain backwards compatibility so as not to break the internet.

It’s frustrating, it is. I’m not trying to say “margin collapsing takes precedence over your minute concerns!” I’m just suggesting one reason (of perhaps many reasons) why margin: auto isn’t an effective approach.


Why would that be an issue? Isn't it fundamentally the same as with the horizontal centering via margin: auto for inline-blocks?


Margins are only collapsed in the vertical direction


> It’s amazing that CSS finally has a single property to control vertical align

It's amazing that this happens in 2024, CSS, you have one job!


It seems odd that "style sheets" turned into layout language. ;-)


Always has been.

From SGML, precursor to HTML:

> Style sheets provide the means to specify the rendering of arbitrary elements, including whether an element is rendered as block or inline.

https://www.w3.org/TR/WD-html40-970708/intro/sgmltut.html


That’s the whole point yeah


But what about all the Vertical Centering as a Service providers that is going to put out of business??!


They can pry my flex box from my cold dead fingers


CSS has finally arrived. Only took 20 years. Now remove the syntax bloat with something like:

made-by-humans: enabled




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: