karaterobot 2 days ago

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.

  • nox101 2 days ago

    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 2 days ago

      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 2 days ago

    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 2 days ago

    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 2 days ago

      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 2 days ago

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

    Once you understand this, all of flexbox becomes easy.

    • itake 2 days ago

      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.

      • phist_mcgee 2 days ago

        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 2 days ago

      Is this something to understand or memorize?

      • cynicalpeace 2 days ago

        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

  • 65 2 days ago

    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 2 days ago

    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

    • WorldMaker a day ago

      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 2 days ago

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

      • namibj a day ago

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

    • JamesSwift 2 days ago

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

    • pjmlp 2 days ago

      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 2 days ago

      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 2 days ago

        Fraction. As in 1/n.

        • dylan604 2 days ago

          right, but if I want a simple 2 column row with the first field being narrow and the second field stretching to fill the width "1fr auto" doesn't work nor does "1fr 2fr"

          • recursive 2 days ago

            > second field stretching

            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;
            • dylan604 2 days ago

              but i don't know if 10em is too much or too small. that's the point. i don't want a specific width defined

              • robertoandred 2 days ago

                Try min-content, fit-content, or max-content instead of auto.

                • dylan604 2 days ago

                  This is probably the way.

                  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

              • recursive a day ago

                Then you can use 1fr 10fr or any ratio you want.

                • dylan604 a day ago

                  the any ratio I want is the point of the exercise. i don't know how many or what size. i want the first column to be just wide enough, and the second column to fill the space. what you suggested does not do that, but thanks for playing

                  • recursive a day ago

                    You're welcome.

                    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.

                    • dylan604 20 hours ago

                      The requirements have not changed.

                      • recursive 18 hours ago

                        Maybe not, but the way they were described has.

                        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.

  • cyberax 2 days ago

    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.

  • holoduke 2 days ago

    In almost every framework there is confusion about if a property is applied on itself or its children. Always the same thing.

codedrivendev 2 days ago

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/

smarkov 2 days ago

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 a day ago

    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.

p0w3n3d 2 days ago

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 2 days ago

    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 2 days ago

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

      • esprehn 2 days ago

        Yes.

        • p0w3n3d 19 hours ago

          It's because both USB-A and flexbox are in dimension of a higher order

  • lenkite 2 days ago

    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 2 days ago

    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 2 days ago

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 2 days ago

    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 2 days ago

      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 2 days ago

      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 2 days ago

    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 2 days ago

    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 2 days ago

    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 2 days ago

      Tailwind sure is a blight upon frontend maintainability.

      • 9dev 2 days ago

        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?

        • tracker1 2 days ago

          ... except for where you aren't wanting to replace.

          • 9dev 2 days ago

            Well, what is it then that is so complex about Tailwind? I’ve been hearing that for so long, but nobody was ever able to explain succinctly what Tailwind made so awfully complicated.

      • gjsman-1000 2 days ago

        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.

        • cluckindan 2 days ago

          There is no semantic connection between design and code.

          • gjsman-1000 2 days ago

            I don’t even know what this means, but it sounds either stupid, or a theory that only works in a clean room laboratory.

            • cluckindan 2 days ago

              Maybe you need to figure it out before jumping to conclusions like that.

              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”>
              • gjsman-1000 2 days ago

                Bull. I have no clue at this glance what .input specifies, or what I can override without a conflict. Heck, .input might not even exist, resulting in only stock browser styles. The bottom one, I can fully picture while it isn’t much longer, the above is opaque and gives me no information at all about the look.

                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.

                • cluckindan a day ago

                  It says ”input” right there in the classname. @apply might be the only sane way to use Tailwind, but it seems like a post-hoc mixin construct, and definitely has the same level of indirection of styles that you claim is an issue with classnames.

                  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.

  • sod 2 days ago

    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 2 days ago

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

      • rererereferred 2 days ago

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

vidyesh 2 days ago

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.

Kwpolska 2 days ago

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

n144q 2 days ago

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 2 days ago

    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 2 days ago

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 2 days ago

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

atum47 2 days ago

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

philippta 2 days ago

I never struggle with flexbox itself. But getting a flexbox right in which the content overflows is always a mystery to me.

If anyone has a guide or spec how overflow rules apply in CSS, please share.

  • insin 2 days ago

    Every time I re-encounter this, I end up re-learning that min-width on flex items defaults to 'auto' and min-width: 0; is the secret sauce.

    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.
    • pcchristie 2 days ago

      You've just given me a flashback to a learning project I did 6 months ago where I think I had this exact issue with this exact type of layout, and I ended up going with some other forced hack to fix that problem because I never worked out the solution...

    • jbeninger 2 days ago

      For my last few projects I've resorted to

      *,*: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.

      • nicoburns 2 days ago

        I totally agree. And not only is it unintuitive, it's bad for layout performance!

  • cluckindan 2 days ago

    Use box-sizing: border-box on everything and your life with flexbox will be a lot easier.

sota_pop 2 days ago

Having come from WPF with dotnet, XAML/WPF used to feel clunky and dated.

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

cluckindan 2 days ago

Overuse of flexbox and grid leads to hard to comprehend and slow layout.

hk1337 2 days ago

I understand the basics of flexbox. The part that gets me is when I start putting pieces together and nesting divs and flex box items.

scotty79 2 days ago

Still better than floats. Maybe some day we will again approach ease of use of the html tables.

andrewstuart 2 days ago

Flexbox makes sense when you understand it is 2 dimensional.

Everything in FB is 2 dimensional.

mediumsmart a day ago

Why struggle with flexbox when you have a real grid

tiznow 2 days ago

I needed this 3 days ago before I approached a mental breakdown, haha.

jbverschoor 2 days ago

Use flexbox to pack multiple elements. Use grids for layout

sn0wtrooper 2 days ago

I really love flex. Helped me get over bootstrap and floats.

yoav_sbg 2 days ago

I added the github Link if you have any feedback or changes!

bigtex88 2 days ago

As a heads-up to any other developer - this is the type of thing at which LLM's excel. I would absolutely never mess with Flexbox on my own anymore. It's too esoteric and weird, and I don't need to waste time trying to get things to line up when ChatGPT or any other LLM can get it done almost instantaneously.

  • mhuffman a day ago

    I have never had any trouble with flexbox or grid, but am curious how do you use LLMs to do your layout or to get it to do layout with flexboxes? Do you just give it a very detailed description of the layout? Or do you give it an image that you have put together? Do you have an example of this? I am very curious to try it myself.

  • CamperBob2 2 days ago

    "Here, have your mandatory downvote for suggesting a valid AI-based approach on HN." -- HN

    This is exactly what LLMs are good for -- applications where you can immediately test and iterate on their output.

indulona 2 days ago

we should not be needing "guides". it should be simple and intuitive. if we need all of these tools to understand what we are doing, the spec is scrap and should have never been approved.

wouldbecouldbe 2 days ago

Hahaha yeah developers thinking of too complicated abstract solutions and forcing it on us seems to be a recurring theme.

dom96 2 days ago

This is great, but the problem is I won't be able to find it when I am working with CSS Flexbox

  • nayuki 2 days ago

    Bookmark it in your web browser