Skip to content(if available)orjump to list(if available)

Show HN: Struggle with CSS Flexbox? This Playground Is for You

Show HN: Struggle with CSS Flexbox? This Playground Is for You

116 comments

·January 5, 2025

Experiment with different flex properties to understand how they affect layout. Adjust the controls below to see changes in real-time and copy the generated CSS code.

karaterobot

In my opinion, flexbox is simple and makes a lot of sense. The problem is that its properties and values were named by a committee, so they aren't intuitive to anyone who wasn't involved in the long proceedings that led up to the spec being written. You try to remember which one is justify-content, which one is align-items, and so forth, and just give up and try everything until something seems to work.

bobbylarrybobby

I have to visit https://css-tricks.com/snippets/css/a-guide-to-flexbox/ literally every time. There's a similar one for grid that's just as good.

nox101

My issue with flexbox is that I can't seem to memorize all the settings I need to get boxes within boxes and content to either fill a box or the box to surround the content.

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.

nicoburns

It's mostly:

- "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.

paradox460

One trick is if you open the chrome css inspector, you can click an icon next to the display: flex rule and see iconographic representation of the various flex box properties, and click them to set them. Every page essentially becomes a playground

yojo

It is impressive that Tailwind succeeded in making this even more confusing.

justify-content: center -> justify-center

align-items: center -> items-center

Just… why?

umvi

There's a reason "naming things" is one of the great hard problems of programming. Especially when you have to name hundreds of things while developing i.e. a framework. Unless it's a solo pedantic developer creating the entire thing, inconsistencies will crop up, some names won't age well, some names don't quite capture the thing they are naming, etc.

cynicalpeace

justify-content should be called main-axis. align-items should be called cross-axis.

Once you understand this, all of flexbox becomes easy.

itake

I have issues with "row" vs "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.

null

[deleted]

phist_mcgee

I've deeply internalised that with flexbox, when using rows or columns that I need to visualise a row that holds multiple columns. And for a column, it's got multiple rows inside it. I literally have to visualise this in my head to remember the correct property.

bobbylarrybobby

Is this something to understand or memorize?

cynicalpeace

Yes- justify-content: center will center items along the flex direction. For example, if your flex-direction is "row" then it will center your items in the middle of the row horizontally. align-items: center will center your items in the middle of the row vertically.

For example, justify-content: center and flex-direction: row centers items the same as align-items: center and flex-direction: column

null

[deleted]

65

One thing I don't like about flexbox is that align-items and justify-content switch what they do depending on the flex-direction.

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.

dieselgate

There are myriad “how to flexbox” but never seen good “how to grid” in my experience - I despise grid and have never intuitively gotten the hang of it and 95% of the time flexbox will work better and is easier anyway

Just my $.02

frob

I've gone back to https://cssgridgarden.com/ multiple times. It's https://flexboxfroggy.com/ but for grids.

WorldMaker

I've reached the other opinion. I learned flexbox better than most, but it never felt "intuitive" and it was hard to reason about complex flexbox layouts, especially responsive ones with multiple variations. Flexbox in general seems so hard to reason about that there are a million "helper" libraries for it, each with their own mini-DSLs on top of the flexbox DSL. Every junior developer's usage of flexbox starts to look like Tailwind, covered in these DSLs for DSLs, increasingly spread out across the elements of the DOM.

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.

bobthepanda

I use https://css-tricks.com/snippets/css/complete-guide-grid/ as a reference since it has pictures.

namibj

Sorry for wrong thread; 38c3 kept me too busy to catch the reply window for https://news.ycombinator.com/item?id=42490859 :

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).

microflash

I have found Grid by Example[1] by Rachel Andrew pretty useful to learn and use Grid.

[1]: https://gridbyexample.com

JamesSwift

Flex is for 1d layout, grid is for 2d layout. Anything by Jen Simmons is a great resource for learning grid.

pjmlp

Grid is originally based on WPF XAML grid system, as it was the former IE team that contributed the original design, and I still can't grasp it fully, even though I spent 4 years doing WPF development.

dylan604

1fr

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.

mixmastamyk

Fraction. As in 1/n.

cyberax

Heh. I made a tool to translate <table>-based layouts to CSS just to try and memorize all the settings. I failed (to memorize them).

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.

codedrivendev

I know others have already mentioned it, but i've recommended https://flexboxfroggy.com/ to others before and they quickly picked it up.

This is another good one for learning css grid https://cssgridgarden.com/

yoav_sbg

I like the gamification of the cssgridgarden, it is similar to https://www.codemonkey.com/ which is mainly for kids!

kmoser

One of my flexbox faves which is very similar to OP: https://flexbox.tech/.

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.

KolmogorovComp

I tried flexboxfroggy, and while the concept is nice and well executed, the lack of syntax completion really is putting me off.

dieselgate

This is hilarious hope it is satire

thetalhatahir

Awesome, looks nice!

smarkov

I highly recommend Josh's guide: https://www.joshwcomeau.com/css/interactive-guide-to-flexbox...

It goes over more advanced gotchas and tips in detail such as flex-basis, auto margins, min-width, etc.

jeeyoungk

This is one of the best thing I read; I thought I was pretty good at Flexbox (and its tailwindcss building blocks), but this scratched my itch on some theoretical foundations.

likium

Nice playground! I built an interactive guide a few years ago with a similar playground at the end: https://lik.ai/guides/an-interactive-guide-to-flexbox/

It was useful to refresh my memory here and there with it.

yoav_sbg

Thanks. your playground is very informative. Like!

notRobot

Such a fun playground! Thanks for this :)

p0w3n3d

2025: two years and three tutorials later I still cannot use flex properly.

Another one that didn't help me as well: https://flexboxfroggy.com/

aimazon

After years of working with Flexbox, I have realized Flexbox is like plugging in a USB-A: justify and align are always the wrong way around on first try.

oneeyedpigeon

But, like USB-A, are they also frequently the wrong way round the second time too?

lenkite

Man, why is it so hard to keep in memory ? Can keep a whole bunch of stuff in memory - including CSS float layout for years, but flexbox hits that strange place where if one doesn't use it for a couple of months, one tends to forget 80% of it and needs to re-read from scratch and spend time on the playground again.

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.

yoav_sbg

I also know flexboxfroggy but as I like to "play" and less read/write I didn't find this website useful. Hope you will find it useful. If something is missing, please let me know :)

vishnuharidas

Why did they name those props `justify-content` and `align-items` instead of `main-axis-arrangement` and `cross-axis-alignment` which makes more sense?

freetonik

There is probably some explanation, but in general, I find many CSS properties confusing and unintuitive: `color`, `text-align`, `position: absolute` vs `position: fixed` (absolute is still technically relative!), etc.

cluckindan

Everything is laid out on the page in a flow, according to DOM order. Think how a typewriter produces text on a page: top-down, left-to-right. This is the flow.

Absolute takes the element out of the flow while relative maintains it in the flow.

sureIy

What isn't clear (even to many experts) is that certain properties change the layout model entirely and that text has its own "pushing" box that contributes to sizing.

cantSpellSober

For discussion I'd argue

• 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.

rererereferred

Similarly, I find confusing that the `grid-template` shorthand uses the y axis first: y1 / x1 / y2 / x2

It's also not zero based which I always forget.

insin

Either Google has shifted _so much_ focus to getting an LLM to tell you about very real Encanto 2 spoilers that its search capabilities have atrophied, or my surprise at there not already being a Tailwind plugin for this is justified:

    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' },
      })
    })

cluckindan

Tailwind sure is a blight upon frontend maintainability.

9dev

Is it, though? How much easier to refactor it in the future can it be, other than being able to search-replace Tailwind classes when the need arises to?

gjsman-1000

I strongly disagree; any large project using CSS tended to have arcane names for everything and turned into a Tailwind of its own, but worse.

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.

null

[deleted]

sod

I didn't confuse it since I use the mnemonic "formatting text to “justify” is horizontal in my language".

Sure you can change that. But that covers the flex default behavior.

vishnuharidas

But when the `flex-direction` is `column`, `justify-content` becomes synonymous for "vertical alignment" - that's what creates the confusion here.

rererereferred

I guess it makesmsense if flex-direction vertical is like switching to a vertical language.

vidyesh

This is great for quick visual cheatsheet but I think one just has use it everywhere in a project to make it stick. The syntax is fairly self-explainatory for me. (except row and column)

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.

emmanueloga_

Chrome/Edge has a nice feature that I don't think Firefox includes. It allows you to quickly toggle between flex modes [1].

--

1: https://imgur.com/a/17iy1eN

Kwpolska

This seems extremely limited to just four container properties, completely ignoring the child item properties, which are quite important in flexbox as well.

n144q

I wonder if there is a set of interesting CSS challenges, with well written solutions and explanations, to help practice CSS layouts. And maybe debugging challenges -- why doesn't this code snippet give me the layout that I want?

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.

buremba

Same here, but with Claude. I often just ask it to rewrite the CSS to fix issues & make it look the way I want, and it does it for me much faster than going through the docs.

darekkay

When learning Flexbox, I've created a cheat sheet to look up the properties. Others might find it useful, too: https://darekkay.com/flexbox-cheatsheet/

rererereferred

I like this! I can already see the effect without having to select it and the links to MDN are much appreciated.

atum47

You should let the use play around with the number of items inside the container and it's parameters as well, like flex-basis, grow... Good job never the less