335 points · by feross · 5y ago · 160 comments
CSS always feels like less of a languages and more like a bunch of hacks crammed into the browser. And the inscrutibility leads to copy-pasta code: search for a sample, paste it, it works now, mostly... But when it breaks, newbie dev will struggle to untangle. (Ruefully, speaking from experience).
I know, many folks love css, but I find it a big mental discontinuity from html and js. It's solving a different problem, sure, but I look forward to some future version of styling which might offer a more "readable" experience.
Disclaimer: No affiliation, just a grateful customer hoping these ideas catch on.
First example on every-layout and already I have a fresh perspective. For example, under stack [0]:
"margin is really a property of the relationship between two proximate elements. "
and then a solution for styling the relationship directly.
When you start adding features to give web developers pixel-perfect control of the viewport then you impede other features like allowing the user to choose their own font sizes while still having everything render in a sensible way.
1. A page is tet + images. Complex layouts are very late invention for HTML
2. The whole layout can be done in a single pass.
This was crucial for the 90-s era browsers because you wanted to be fast on rather underpowered machines. I think double-pass was permitted/mentioned in standards only around 2000/2001 with relation to tables (I forget what it was exactly). Centering an element vertically may require several passes (do the rest of the layout, layout the component, layout the component centrally, possibly reflow the rest of the document, etc.), and it was a big no-no
Layout is a hard thing that looks easy until you get in to the details. This is no different to talking to a junior C developer about transferring data and them asking why there isn't just a single simple and robust way to send structured data from one server to another. We have JSON and Unix sockets so why do Protobufs need to be a thing. Because their lack of experience doesn't give them the depth of knowledge to understand why it's actually hard, that's why.
what?
https://en.wikipedia.org/wiki/There%27s_more_than_one_way_to...
This is actually the misconception that leads to confusion with (and ultimately, poorly written) CSS.
It comes from a misunderstanding of what creating a web page actually is.
As Alan Kay once remarked, “A change of perspective is worth 80 IQ points.”
This is no less true with CSS.
Most developers view CSS an annoying 'styling' language, that's 'not really a programming language'. Which separates it in your mind from the engineering frame of view that tends to excite you as a developer.
This makes CSS tedious to use.
In truth, HTML and CSS are parts of a holistic language of layout construction. They work in unison, not as individual components. Very much an engineering toolset. Just that the problems to be solved are more visual in nature than you're probably used to.
Instead of seeing CSS as an annoying way to 'style' pages, see it instead as a visual programming language for constructing visual guides (UI's) for your user.
You don't 'style' pages, you 'construct' and 'architect' them. It sounds weird to use these words, but they have a massive effect in changing your perception of what you're doing. And that has a massive effect on your impression and willingness to learn how to do it well.
I think that understanding this is one of the key differentiators between developers who are good with CSS and those that struggle.
Tools like TailwindCSS (that lead to a massive increase in both productivity with CSS as well layout maintanability) are based on this fundamental premise.
I expand on this more in a recent post called "How To Get Better At CSS", which you can read here: https://planflow.dev/blog/how-to-get-better-at-css
Sidenote: I'm also working on a book that aims to help you gain mastery over CSS, showing you how to truly understand it, by learning how to debug its most common issues as well as re-framing how you see it in the way I've described above. You can check that out here: https://gumroad.com/l/Debbg/z823cp8.
> You don't 'style' pages, you 'construct' and 'architect' them. It sounds weird to use these words, but they have a massive effect in changing your perception of what you're doing. And that has a massive effect on your impression and willingness to learn how to do it well.
I honestly don't understand what this means (I read the corresponding blog post but am still confused, unfortunately). Could you expand, possibly?
A lot of the problem developers have with CSS, has to do with the way they see it.
Developers don't typically like to 'design' things in the artsy sort of way. But they do like to engineer them. CSS can be viewed from both of these perspectives very accurately.
The latter however, generates more enthusiasm for the developer. And enthusiasm is key to everything else.
Alan Kay is still alive.
> I think that understanding this is one of the key differentiators between developers who are good with CSS and those that struggle.
Yeah, no.
HTML is a single-root hierarchy whose only primary job is to display text and maybe some images. That's it and it pretty much sucks even at this job.
CSS is and will remain a bunch of hacks trying to force the web page into something it's not. Saying that CSS and HTML are meant to be used together to construct and architect pages is some serious re-writing of history and evolution of CSS and some impressive mental gymnastics.
For a great immediate overview why that is, it's enough to look at this page: https://csstriggers.com When changing text-shadow causes your entire page to reflow and repaint, there's no "architecting" or "constructing". It's piling hacks on top of each other in the hopes it somehow works.
For most of its life since inception in 1996 CSS was nothing but simple styling of HTML pages. There were a few feeble attempts like `display:float` and `position:absolute` to pretend it's not, but the enduring resilience of the Holy Grail kept obliterating that idea. This changed around 2008 with the meteoritic rise of mobile phones and mobile apps (thanks, Apple and Google). It still took w3c 5 years to come up with a solution that could barely place boxes in a relative position to each other without increasing developer suicide rates. I'm talking about flexbox, of course.
Side note: Terrainformatica's Sciter had already had a solution to this for several years, and it was simpler, faster, and required significantly less cognitive load: [1] Not to mention Cassowary which was proposed all the way back in 1999 (can't find the link now).
I think it took MS and then w3c a combined of 10 years to get Grid off the ground.
And all that to be able to finally say: hey, we can barely position an element vertically. Of course, they still can't, as the OP link demonstrates: you need a brand new layout module + a brand new alignment directive to achieve something most GUI libraries in the world have had since Palo Alto's Xerox in 1979. And yeah, it still has cons that has to be remedied by "Gentle Flex".
And, of course, all of this is as brittle as the tail of Prince Rupert's drop. You even glance at it sideways, and it breaks in countless unimaginable ways. Attempt a slightly different layout? Just lift the layout with the markup and attempt to place it in a different position on the page? Adjust sizes in the most minuscule way? Place these things side-by-side? Oh, great constructing and architecting work guys, everything is broken.
And, of course, it cannot be in any way, shape or form properly refactored because it's a flat system where everything is global, styling a single tree tailored to display text and images.
[1] https://terrainformatica.com/w3/flex-layout/flex-vs-flexbox.... and https://terrainformatica.com/2018/12/11/10-years-of-flexboxi...
From last link:
--- start quote ---
As a result 12 (sic!) new flexbox-only-properties were added to already huge CSS property namespace (200 properties and counting). All that just to make FlexBox anyhow useful.
But the biggest problem of the solution is that the flexbox breaks existing CSS box model – dimensions of elements are now defined not just by width/height properties but bunch of others like align-items and align-self.
Go figure out why “align” property shall change dimensions and why explicit height:100px declaration shall be ignored
--- end quote ---
Mobile phones started off as simply ways to call people. Are they not much more than that now? Would approaching them with only their original perspective be as helpful today?
No. The perspective has completely changed. Now, the more accurate one would be to approach them as full-blown computers.
> HTML is a single-root hierarchy whose only primary job is to display text and maybe some images. That's it and it pretty much sucks even at this job.
While HTML and CSS started in the ways that you've described. That's not what they are for now.
> Saying that CSS and HTML are meant to be used together to construct and architect pages is some serious re-writing of history and evolution of CSS and some impressive mental gymnastics.
I'm not rewriting history. I'm re-framing the CURRENT perspective of these technologies, in a way that is much more helpful. What use does framing HTML and CSS from the perspective of their origins have?
How does this help you create with it today?
With modern considerations that are more in the realm of architectural thinking than they were before?
The last centering example (left with translate) was fun to explain to junior devs too.
All those dirty hacks you needed to do to make stuff work make me so thankful for today's wonderful CSS, even when it frustrates me.
¹ a real-life lesson in monopolism: IE6 was around for over 14 years!
Only wish "gap" property would be adopted easier to make working collapsing flex rows way easier.
Even the basic things like :last-child selector is good quality-of-life improvement.
Is this why the web is less successful than it might be?
People underestimate the complexity of layout. Take vertical centring, for example. The immediate assumption is that it’s easy; you just find the mid-way point between the top and bottom. But the top and bottom of what? The web is a document-based system, with text flowing from top to bottom. The centre in this context is not the centre of the window, but the centre of the document.
Okay, so let’s find the mid-way point between the top and the bottom of the document. The top is easy, it’s zero. So where’s the bottom? Well to find that, you need to know how tall the document is. How do we do that? Well, the height of the document is defined by the height of all of its contents. So let’s render its contents and find the height. But wait – the thing we are trying to render in the centre is part of the contents, so we’ve got ourselves into an infinite loop.
There are various solutions to this – hard-code the height of the document (to what?); take the thing being centred out of consideration when rendering the rest of the document (and now it can’t affect / be affected by the rest of the contents), etc. But which solution is best depends upon the context – and this is how you end up with complexity. It’s easy to say “just put it in the centre”, but it’s not as easy to figure out what that means exactly or which solution is best for doing so.
> Is this why the web is less successful than it might be?
The web is one of the most successful technologies in the history of human civilisation. It seems quite odd to talk about it as if it were a flop because you have to think for a moment before centring something.
I don't know what it is about the web, maybe because it's so accessible, that makes people assume its GUI abstraction should be easy. Or that because web GUI is hard then the web's abstractions are basically a failed experiment. Or, HN's favorite, that we're somehow trying to build things with these completely anodyne GUI tools that they're somehow not meant for.
But it's hard because GUIs are always hard. And the things that make HTML/CSS hard are the threads that run through all other GUI abstractions and make them hard.
Notice how Android and iOS developers are never piling into HN circlejerks raving about how Google and Apple have really solved GUIs.
Here are six different ways to center a square in a box in UIKit with constraints: https://stackoverflow.com/a/26181982/511200 I've been an iOS developer for over five years and I still consult this regularly when checking constraint spaghetti to make sure it's not missing any bits of the magic incantations.
And HTML nor CSS weren't developed with layout in mind. The original idea was to mark up (the M in HTML) meaning and semantics and leave the rendering and display to the client entirely.
If you consider that systems that were designed entirely around layout (Like QT layout https://doc.qt.io/qt-5/layout.html), are complex and hard, you can imagine what happens when you try to do layout with a system that was never intended to handle this, must be backwards compatible, is a standard (not a lib or framework) and can be freely interpreted by renderers by definition.
This was always the central mistake in the design of CSS. It was solving a problem people just didn't really have. I mean, yes, it did both simplify and improve the situation for this sort of document flow kind of thing but we were all using tables for a reason:
Because what people needed was a mechanism to describe structured layout. Inside that, document flow is important, but we spent 20+ years just getting back to the structural power of tables and it's fucking ridiculous.
And it did it all in a way that required the exact constraint solving complexity you're lamenting. But it didn't really have to be that way.
CSS was a disaster and has almost certainly wasted incredible amounts of time and money that could have been better spent if a better solution had come along in time.
Already CSS 2.0 (which is indeed 20 years old) had the power of tables through the "display:table" property. Flexbox and grid gives you additional power.
I wholeheartedly disagree. When you see the title "centering in CSS" you know exactly what it means. It's the same old problem everyone has been solving repeatedly for 20 years. It's not about edge cases. It's not about exceptions to rules self imposed by your specific needs. It's just centering inside a box. And the author of the article knows this, and solves it again for us for the nth time with updated techniques.
The issue isn't how complicated a layout is or needs to be. If you were to look at how horizontal center is implemented under the hood, it's going to be complicated, yet that complexity is abstracted away.
It boils down to the simple fact that intuitively when we've had <center> forever, why couldn't there have been a <vcenter> from the beginning? Not even broken, it never existed. It's as if we're being told, "horizontal centering we'll handle, but vertical centering is for you to figure out". Err, why? And figure out based on the tags and attributes provided. Like, they implemented the puzzle but not the solution.
Similarly, why can't "vertical-align: center" just work more often? The fact that a div needs to be a table-cell or a grid, or having the option of using <td> that still works should all be redundant. There is nothing ambiguous about wanting to vertically center.
We're always reverse engineering from a need. Complex needs justify complex code. Simple needs should be simple declarations.
Because HTML is incrementally rendered. The content of <center> can be rendered line by line as data arrives, but a <vcenter> would need the full content to be loaded before it could be rendered, since you need to know the dimensions of the content before you can center it vertically in a container.
Vertical centering cannot be supported in the ordinary top-down incremental flow rendering. It requires a more complex layout model like table, flex or grid.
So if you want to support a layout that adapts to the size of a viewport you'd do what? Go through DOM with JS on each size change and rewrite DOM from <vcenter> to <somethingelse> and back?
With CSS as is, it's fairly easy to achieve.
.child-element-selector {
centered-inside: .parent-element-selector;
}
And let the browser figure out what I mean, taking into account border widths and parent element padding, for example.What? Where did the loop come into it? The height of the document is defined by the height of its contents. The thing we're putting in the document is part of the contents. So what? You can render it and measure its height without needing to lay out the document. There is no self-reference or circular reference anywhere in the "problem" you've described. The height of the element is an input into the centering problem, but it's not also an output of the centering problem. The positioning of the element is an output of the centering problem, but it's not an input into it.
I also believe that's why some traditional UI toolkits have started adopting constraint solvers.
CSS can be frustrating and it was certainly for many years missing basic stuff, punting that to developers to solve by hand - but I agree with grandparent that these problems are not as trivial as they appear.
So let’s make it hard even for simple cases. Meanwhile,
el.centerX = other.centerX
el.centerY = other.centerY
And it just works without “loops” that simpleton methods like container stacking do have. Did you know that linear algebra has methods to quickly solve that Excel circular reference? What should we do, maybe stop using Excel for layouts?>The web is one of the most successful technologies in the history of human civilisation.
Amazon is the most succesful e-store in the history. Microsoft is the most succesful desktop in the history. Etc, etc, etc. Being most succesful is a result of many traits, and it doesn’t mean it is maximally succesful, technologically and programmable-y superior or even correct. It was just succesful enough to not break under its weight and split into two or more streams of development. (In smartphones it actually did, and we all know how faster and cleaner phone apps look and feel vs web apps).
I've worked with web developers that still don't understand the usefulness of collapsing margins. Developers that think padding is an alternative to a margin and not understanding they are different things altogether. If your solution to spacing something is to wrap it in a new element and add padding then you're thinking backwards. You just add margin to the element you want to space.
I firmly believe the idea that CSS (today at least) is complicated is just people not trying to learn what modern CSS is like. Learn your basics like the box model, block elements, inline elements, etc. Then learn about flex and grid. That right there is like 90% of the knowledge you need to make anything in CSS today.
The way I learned was being given Sketch or Photoshop documents with all the sizes and margins/paddings and told "now make that a website". I really wish there was a learning tool that just had website/website element mockups with sizes and your job is to replicate it as close as possible in HTML/CSS.
I do web development only occasionally, so I'm not familiar with current best practices and rely on trying to mine the docs for what I'm trying to do and googling for it. I've always very strongly had the impression that while CSS was good at styling, it was hopeless at layout.
That changed with the introduction of flex and grid. Suddenly I'm able to use the layout I want without any real problems. CSS is now usable for both styling and layout.
> That changed with the introduction of flex and grid.
Half true. Flex and grid are a significant improvement, but "hopeless" was just about always inaccurate. The vast majority of issues people had with layouts pre-flex were because they didn't understand the box model and how block/inline/inline-block/float interacted with each other.
...Unfortunately, not understanding the box model is still a problem, but flex/grid can get people further without needing to understand it.
> I firmly believe the idea that CSS (today at least) is
> complicated is just people not trying to learn what modern
> CSS is like.
You are absolutely correct.Our crosslinked thoughtworld for intermingling intellects in practice is mostly an entertainment and shopping platform.
Really they probably should have split off when it was young ... Replacing HTML with something for the original lofty goals and leave the html/css/js triumvirate for the mainstream use we all see now.
Then there becomes no pretext that there's any intellectual, educational, or documentarian use for the web because there'd be something else, let's call it the Yeb for those purposes. It'd get two way linking, ontological markup, a WAIS like querying system (where the document author explicitly sets the search up separately from the document), etc.
But that never happened and the web has way too much inertia to successfully fork it at such a fundamental level without a monumental multi-year effort so here we are with one set of standards trying to serve all masters at once by layering stuff on top of each other, building a bigger russian nesting doll to stuff our existing web into (could you imagine if say jstor or citeseer required a "yeb browser" because it served content that was fundamentally incompatible with the web standards. Could have taken off in 1995 but not now)
The only engineering results those conditions ever produce is complexity since it inherently, by design, lacks the focus to remove it.
"If both 'margin-left' and 'margin-right' are 'auto', their used values are equal. This horizontally centers the element with respect to the edges of the containing block."
i.e. horizontal centering is easy because that's a natural thing to want to do in a document, but vertical centering seems to make less sense and is thus correspondingly more difficult.
That its tools lagged behind what people were building with them is just a reminder that a platform's caretakers have little to do with the creators on the platform.
This goes for everything. I built a forum from scratch that miraculously exploded into a community and culture and drama. 15 years later I look at it and realize it happened in spite of me, not because of me. It doesn't even feel like any of it's mine. I'm a groundskeeper.
I might have built the scaffolding, but who am I to say what it's for once thousands of people have made that decision for themselves?
"No, no, no. The web is for boring documents of <p> and <img>! What are you people building?! The most complicated application I envisioned when I invented HTTP was Encyclopedia Britannica online, and the limits of my imagination must be yours!"
It’s many little things, workarounds that pile up given a sufficiently complex/bespoke page. And each of them feel a little bit wrong. Especially when I have to „fix“ things via JS, which fortunately almost never happens anymore.
It’s because text wraps horizontally and flows from top to bottom. The width is constrained by the window but the height is defined by the contents of the document. This means that if you are trying to vertically centre something in the document, you need to know the size of the document before you can render its contents, but you need to render its contents to know the size of the document.
There’s various ways around this but they all have different trade-offs, hence multiple solutions to the problem of vertical centring in CSS.
I'm struggling to understand this question. If the web were more successful than it already is right now... what would that look like?
So if you want layout to be simple, use PDF. Of course most users hate to read PDF's online, because they have to zoom and scroll around and so on. So the real question if you want the pain on the producer side or push it to the consumer side.
Producing layout which render reasonably well on completely different devices and screen dimensions is inherently non-trivial.
No. The success of the web has nothing to do with CSS. By all standards the web has over-achieved and will continue to do so for the foreseeable future of humanity. CSS is an order of magnitude better these days. Browsers are playing nicer together except Safari. Chrome uses more resources than a triple A game title but sets the standard.
Imagine if CSS had that and nesting and variables from the start :)
Flex is easy to remember though.
.parent {
display: grid;
place-items: center
}
and it's supported everywhere CSS grid is! My biggest TIL of the year.More 1 liner tricks from Una Kravets here: https://twitter.com/swyx/status/1279674027070504961?s=20
(the vertical centering has always been the not-so-straightforward part. horizontal centering has always been easy. the current CSS techniques are just as easy as the HTML-only techniques you describe.)
According to section 10.6.4 "Absolutely positioned, non-replaced elements", "height: auto" is the height based on the content except when top and bottom are both auto and it suddenly changes to the the height of the containing/parent element. This breaks vertical alignment based on content.
Also in the same section, if height is auto, then a top or bottom of auto is calculated, unless both are auto and then suddenly top is set to 0 instead of spreading evenly. For that you need to use margins, but they only work if top, height and bottom are all non-auto.
As a result millions of developer hours have been spent on frustrations, fixups and workarounds.
For inline I'm not sure I ever found a solution I liked. I just wanted something like
This brought to by <avatar> Name
With the avatar the same size and centered similar to emoji faces. I'm sure it's easy but I couldn't find a solution that wasn't a hack of specific sizes (ie, not flexiable/responsive)
I have had start and stop bouts of learning CSS several times before and I couldn’t understand it logically like other languages or frameworks for reasons I can’t clearly explain. I avoid frontend programming mainly because I don’t know CSS.
Don't worry, I'm not gonna scold HN, I actually don't think it's that bad of a practice as no browser will ever stop supporting it. The experts can look down upon it all they want.
It's not a bad practice to use center because it's not HTML5-compliant (though that's not a bad reason to avoid it). It's bad practice because putting purely visual info in your semantic markup is a bad idea - mixing responsibilities between different layers creates pain points for later work.
In this sense, it's like a GOTO. It'll work well enough for one person on a small project, and you might thumb your nose at the experts who tell you it's a bad idea - but it's a source of technical debt, and the better approach is to just avoid it from the start.
It's bad practice to constantly change everything every few years, then expect everyone to upgrade to the latest and greatest technology or get left behind.
It's good practice to use code that runs just fine on a 20 year old system.
Centering HTML elements through CSS has existed since 1997 as well, specifically because CSS aimed to make those HTML extensions, that Navigator had introduced, obsolete.
Standards bodies sometimes have to make the tough decision of breaking with the past to safeguard the future. Software longevity is a worthwhile cause, but protecting a 23-year-deprecated, non-standard HTML tag, whose use is bad engineering, is not the kind of hill I would die on.
From the example, I don't like the way he used flex on grids to maintain children aspect ratios, better to use grid's container optiones like justify-items.
.autobot > * {
margin: auto;
}
Browsers match CSS selectors right-to-left. Because there is a * at the end, this rule has to be matched against every element.More info:
https://stackoverflow.com/questions/5797014/why-do-browsers-...
https://www.stevesouders.com/blog/2009/06/18/simplifying-css...
Measuring DOM elements and moving them around in JS is quite slow. Transforms are fast, but understanding where things are after they've been transformed is hard. JS layout is also somewhat prone to problems with things like browser zoom level, OS display scaling (although I've not seen that be an issue in a couple of years), differences between browsers and browser versions, and so on. Layouts with CSS, so long as you're being reasonably conservative, do work quite well in 2020.
CSS transforms are really just an visual effect, not affecting the layout. That's why they are super fast (typically HW accelerated by GPU), but not suitable for defining the layout.
Managing the DOM in JS may be order of magnitude slower but is still fast enough, actually surprisingly fast. It's common to implement 60 FPS animations e.g. in React. Of course the performance heavily depends whether the manipulation causes a layout reflow etc, but generally speaking, DOM is pretty fast nowadays, and most sites/apps are not _that_ performance critical (such as games). Yes, it's not uncommon either that a complex JS app becomes slow, but that's mostly because of inefficient programming / generally bloated app, not because the layout is done by JS instead of pure CSS.
Browsers already have "alternative" APIs to make totally non-DOM interfaces, such as canvas (2D) and WebGL (3d). Nothing prevents to come up with alternative layout/UI libraries written on top of them. I'm sure such a libraries exist. Why it's really rare to utilize non-DOM UIs is probably due to many reasons, i.e. compatibility issues with exotic/older user-agents, devs being more familiar with DOM based solutions, etc.
After all, I think DOM works pretty well even for app-style UIs. Actually, many "native" technologies don't offer as easy responsive layouts, user-settable font-size, etc, which comes almost for free with DOM. There's lof of beauty in the DOM, despite its document-oriented origins.
(Oh, I just typed out some mind flow of mine, maybe a bit off-topic.)
I believe Figma renders like this.
It is not an inherent property of js-controlled positioning. Browsers don’t have direct measuring methods and require stupid workarounds, that’s it. Measuring text layout (most complex thing) in wfh/hfw in js is as fast as in css, because it is just el.getSizeForW(w) and then c++ does the thing as usual. There is no black magic in calculating layouts, it is arithmetic.
>JS layout is also somewhat prone to problems with things like browser zoom level, OS display scaling (although I've not seen that be an issue in a couple of years),
It’s not a property of js either. If a browser provides a stupid internal api that requires manual accounting to zooms and dpi, then css implementation is also prone to that.
>differences between browsers and browser versions, and so on. Layouts with CSS, so long as you're being reasonably conservative, do work quite well in 2020.
The problem is that apps are not conservative, apps want innovation instead of a text layout that spends 20 years to learn a proper vertical alignment.
Oh? There are the old-school e.offset{Top,Right,Bottom,Left} etc methods for measuring the DOM stuff, and there's Visual Viewport API (experimental) for more advanced use cases:
https://developer.mozilla.org/en-US/docs/Web/API/Visual_View...
I basically only use flexbox now, but even flexbox does some really stupid things I don't get. Like if you want one element to go against the parent's rules of say, aligning left, you set the left margin of the object you want going right to "auto" which will push it and every object after it right. But this can create some annoying problems.
https://medium.com/swlh/dynamically-position-and-center-an-h...
Flex and grid was the moment I cared about doing things right. Even now I use grid a lot because I find it works as well in 3d as 2d and I get CSS disciples trying to correct me.
I lean more towards a flexbox guide [1] or game [2]
#5 is one that I like. In the past, I’ve had to specify concrete sizes for elements, then set margins of minus half that value. This is better.
I would like to be able to do the same type of thing for background images, but this technique could give me the same effect.
- Buttons and stuff that should stay the same with font increase: padding with ems
- Everything else: flexbox
Flexbox has made everything so easy.
seems to work every time.
"Pop and Plop"?
"Gentle Flex"?
"Autobot"?
kind of like a cheat sheet. because the problem i face is that there are just too many ways to center things and wish there was a most recommended method. i come across many articles explaining how to in raw CSS, but not too many using frameworks.
i typically jump from one css frameworks to the next (bs4, material, tailwind) and writing custom css is usually frowned upon if there are already a way to do so built-in by the framework. but it's hard to keep up with these changes so thought it'd be nice if there were some guides or articles for them.
But, often the problems are also non-trivial. The web platform is really diversed, there are user-agents everywhere from mobile phone to smart tv and everything between, with extremely varying input methods (touch, mouse, etc), resolutions, pixel densities, performance, etc. Some of the devices are never updated, running a decade old browser. I don't think any other technology faces similar challenges. Actually it's amazing that things work as good as they do.
The HTML center tag only horizontally centers the contents which you can do with the simple CSS: `text-align:center` [1]
The examples in this post center horizontally AND vertically.
[1]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ce...
The fact that there are "JS land" and "CSS land" is one of the problems. There should be better integration between the two, eliminating the need for "hacks" such as replacing stylesheets by JS styling (popular with React, for example). Writing variable-based dynamic CSS is too hard. SASS and other variants don't solve the problem because they are compile-time, not runtime technologies.