Show HN: Struggle with CSS Flexbox? This Playground Is for You
yoavsbg.github.io
yoavsbg.github.io
Once you understand this, all of flexbox becomes easy.
For example, justify-content: center and flex-direction: row centers items the same as align-items: center and flex-direction: column
If you choose "row", I see 1 row and 5 columns. So if I want more columns, I need to choose "row".
If you choose "column", I see 5 rows and 1 column. So if I want more rows, I need to choose "column".
When the content is wrapped, the setting is more intuitive, but most of the time, you don't want to use the wrap feature.
Just my $.02
wtf is an fr anyways? is a question I've never been able to retain the answer. there are many times where i've attempted to use grid layout, and then ultimately just switched back to neanderthal mode and used a table.
If you want the second column to stretch, then the `fr` part is assigned to the second column. Mixing `fr` and `auto` doesn't really make sense. You can do it like this. https://codepen.io/tomtheisen/pen/emOeqPy
grid-template-columns: 10em 1fr;I also have issue of letting go and fully accepting the way of the flex. Instead of feeling comfortable just throwing elements inside the grid element, by brain needs each row to have a wrapper element like a tr and nested td instead of just tossing td elements in tbody (sticking to the table mentality). It took a minute for me to get that to click in my head for whatever reason
Now that you've explained the rest of your requirements, I can see that `fr` is not suitable for the purpose. In the meantime you've gotten a better answer for the updated requirements, in particular min-content and max-content.
First:
> I want a simple 2 column row with the first field being narrow and the second field stretching to fill the width
Second:
> i want the first column to be just wide enough, and the second column to fill the space.
actually, that was the point of my comment a few parent generations up from linked: by hanging the train under the rail, you get perfect banking for basically free. Fair, entry and exit of turns has to be handled smoothly, but you get passive pendulum-based perfect bank angle at any speed from 0 to the maximum allowed for that radius. And while it's certainly not cheap to be forced to build the track as "technically a bridge", the lack of serious span makes the cost not actually that bad relative to train track in locations where the curve radius and speeds tend to actually be used (valleys narrow enough to need quite tight radii, as well as near existing infrastructure: if you're already bridging, it's easy to design the pillar point foundations to not conflict with existing usage).
Oh, and about the coffee you mentioned: just like bus and subway, (open) drinks and food are not allowed anyways. In any case, the only substantial forces would be increased gravity, and whatever traction limited acceleration along the direction of travel (this would be with deliberate jerk minimization, though, as people would otherwise loose their footing, where the currently active limits for subways come from).
Whereas the "ASCII diagrams" of grid-template-areas, especially, I think is one of the most intuitive things in recent CSS. The majority of Grid CSS styles are generally centralized to your top-most containers outside `grid-area: some-area-name;` container classes (which may also have an align or justify) and the rare `display: contents;` (if you count that as Grid CSS) or `display: subgrid;`. Responsive layouts in CSS Grid is just changing the templates in your central containers based on your breakpoints.
I've gotten about to the point that even for 1D layouts I'm about to "forbid" junior developers from using flexbox to avoid its seemingly inevitable decline (in so many large apps I've seen) to inline-style soup via (usually ad hoc) Tailwind-like DSLs, because CSS Grid is easy enough and clean enough to use everywhere.
This is particular important on an SPA where I want the boxes to fill the screen but never overflow it. I manage eventually. I need to put the totally intuitive (sarcasm) min-width: 0, here and there, and a few other things here and there and eventually I get it.
I feel like I should be able to make a few css classes to cover all of this but I have yet to figure it out for every case.
- "flex: 1 1 0px" for fill available space
- "flex: 0 0 auto" for sizes to content
And then arguably every flexbox item ought to have min-width (and/or min-height) set to 0 because flexbox has a "min content sized" automatic minimum size built-in, which is rarely what you want. But if the content isn't overflowing or can be compressed in some way then you can get away without this.
justify-content: center -> justify-center
align-items: center -> items-center
Just… why?
I still use it from time to time.
It's amazing how simple and intuitive the <table>-based layout is, that we still can't even get close to its usability.
justify-content: center; with flex-direction: row; makes the element horizontally centered. But justify-content: center with flex-direction: column; makes the element vertically centered.
This is another good one for learning css grid https://cssgridgarden.com/
A useful tool for generating Bootstrap code is https://build.layoutit.com/ and for CSS grid https://grid.layoutit.com/. Unfortunately the former is limited to Bootstrap 4, not 5.
It goes over more advanced gotchas and tips in detail such as flex-basis, auto margins, min-width, etc.
It was useful to refresh my memory here and there with it.
Another one that didn't help me as well: https://flexboxfroggy.com/
The problem is that too many property names are just non-intuitive or plain wrong. They might as well have given them one or two letter symbolic names.
Absolute takes the element out of the flow while relative maintains it in the flow.
Sure you can change that. But that covers the flex default behavior.
const plugin = require('tailwindcss/plugin')
const aliasFlexboxAlignment = plugin(function({ addUtilities }) {
addUtilities({
'.cross-axis-baseline': { 'align-items': 'baseline' },
'.cross-axis-center': { 'align-items': 'center' },
'.cross-axis-end': { 'align-items': 'flex-end' },
'.cross-axis-start': { 'align-items': 'flex-start' },
'.cross-axis-stretch': { 'align-items': 'stretch' },
'.main-axis-around': { 'justify-content': 'space-around' },
'.main-axis-between': { 'justify-content': 'space-between' },
'.main-axis-center': { 'justify-content': 'center' },
'.main-axis-end': { 'justify-content': 'flex-end' },
'.main-axis-evenly': { 'justify-content': 'space-evenly' },
'.main-axis-normal': { 'justify-content': 'normal' },
'.main-axis-start': { 'justify-content': 'flex-start' },
})
})I also like Tailwind because it’s so self-documenting. Even if Tailwind’s development were to stop, tomorrow, and all of the style sheets were lost globally, I would know what everything is meant to be.
I think it’s much easier to understand what
<input class=”input input--email input--valid”>
is supposed to be compared to <input class=”bg-white focus:outline-none focus:shadow-outline border border-gray-300 rounded-lg py-2 px-4 block w-full appearance-none leading-normal”>Secondly though, and why this opinion is frankly stupid, is that I can just put this in my CSS code before it’s built through Vite:
input { @apply bg-white focus:outline-none focus:shadow-outline border border-gray-300 rounded-lg py-2 px-4 block w-full appearance-none leading-normal; }
And then lo and behold, I’ll do you one better:
<input type=“text”> </input> is now fully styled, with Tailwind only.
Strange you would be worried about accidental overrides with class selectors but then go ahead to target styles with tag selectors. Is that not objectively worse?
Btw, targeting input[type=”text”] means now you cannot apply those styles to a regular <span> or <div> using a class, which is sometimes useful, especially in very large projects.
I am still not at all convinced Tailwind is even slightly useful. It feels like shoehorning HTML4 <color>, <font> etc. into HTML5 classes.
All in all, your arguments come off as ad hoc rationalizations for a personal preference, not as substantial examples of Tailwind’s superiority over more traditional CSS approaches.
It's also not zero based which I always forget.
• this name works with Grid and Flexbox
• this convention matches justify-items and justify-self, which makes sense
to your point I still confuse it with align-content sometimes.
I never struggled with Flexbox or grid (most of it) but I see people being so confused. Just understand the box model, and use Firefox Dev tools. That helped me a lot when learning Flexbox model. It lets you visualize your containers quite well. I think even Chrome Dev tools do that now.
As everyone else mentioned, flexboxfroggy and cssgridgarden were very helpful to practice.
That being said, I still open the CSS-tricks cheatsheet everytime for syntax and I can never get if is row or column in the first try.
--
ChatGPT has been very helpful when it comes to debugging CSS issues (not always correct but mostly in the right direction), still, I always want to get a deeper understanding of how CSS works. I have read books and tutorials, and for sure it just comes down to a set of rules, but correctly applying them is hard.
If anyone has a guide or spec how overflow rules apply in CSS, please share.
e.g. I recently made a component representing an attached file, where the flex items in its layout were:
[icon] [filename] [button]
To get long filenames to not force their container to the full length of the string, so they could be truncated by text-overflow: ellipsis, I needed to put min-width: 0; on the flex item containing the filename.*,*:before,*:after { min-width: 0}
I haven't had a single problem with it. I honestly feel this part of the spec was a mistake, given all the confusion I see. Online discussions suggest 'auto' is a counterintuitive default.
This is exactly what LLMs are good for -- applications where you can immediately test and iterate on their output.
I treat it as if I'm speaking with the world's foremost expert on CSS and most of the time it's spot on.
However, web front-end feels very chaotic by comparison. I’m still no expert, but my big gotchas are always div sizes ie min-max-fr height/width.
With WPF you can size Grids as auto or “*” and things size dynamically - I’ve grown to appreciate the WPF Grid and data binding.
It also doesn’t help that I find trial-and-error more helpful than css/html docs. The docs often feel tautological to me for some reason and it feels like there’s too many ways to achieve the same thing. I definitely recognize this personal experience - YMMV
Same with their grid guide.
Everything in FB is 2 dimensional.