TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

An interactive guide to Flexbox

813 pointsby brandrickover 2 years ago

28 comments

slhckover 2 years ago
As someone who only needs to use Flexbox&#x2F;Grid every once in a while, this is precisely what I needed.<p>I&#x27;ve been struggling with static documentation like the one from Tailwind [1] or MDN [2]. Writing good and intuitive documentation is hard, surely this must have been quite an effort.<p>[1]: <a href="https:&#x2F;&#x2F;tailwindcss.com&#x2F;docs&#x2F;flex-direction" rel="nofollow">https:&#x2F;&#x2F;tailwindcss.com&#x2F;docs&#x2F;flex-direction</a><p>[2]: <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Learn&#x2F;CSS&#x2F;CSS_layout&#x2F;Flexbox" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Learn&#x2F;CSS&#x2F;CSS_layou...</a>
评论 #33718896 未加载
评论 #33720563 未加载
评论 #33719329 未加载
评论 #33727774 未加载
ad404b8a372f2b9over 2 years ago
I love flex, it&#x27;s made CSS so easy. One of the things that improved my flex usage was using Penpot to draw my designs before implementing them.<p>They have alignment properties for graphical elements that work like flex&#x27;s justify-content and align-items properties so once you design a view in Penpot it becomes almost trivial to translate it into HTML&#x2F;CSS using flex.<p>It really changed my mindset from working with relative or absolute positioning, blocks, margins, padding, etc... to simply working with flex everywhere. And it&#x27;s responsive automatically!
评论 #33720571 未加载
tppiotrowskiover 2 years ago
&quot;I like to think of CSS as a collection of layout modes. Each layout mode is an algorithm that can implement or redefine each CSS property. We provide an algorithm with our CSS declarations (key&#x2F;value pairs), and the algorithm decides how to use them.&quot;<p>Brilliant. The way basic CSS properties are taught often ignores the layout mode. Even MDN does not mention that &quot;width&quot; is a CSS property that is not always respected when &quot;display: flex&quot;. Making this distinction more prominent would reduce the amount of confusion&#x2F;frustration when certain CSS properties appear to &quot;not work&quot;.<p>[1] <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;CSS&#x2F;width" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;CSS&#x2F;width</a>
justin_oaksover 2 years ago
As someone who does more back-end work in my job, I love learning about this kind of front-end stuff that I don&#x27;t get to use as often.<p>Flexbox has been great to work with because it&#x27;s much easier to control your responsive design. You can choose what lengths stay fixed, which expand and shrink, etc.<p>When I learned how to use flexbox I went searching for how to add tools to my responsive-design toolbox. I learned about how to use media queries so that different CSS rules are applied based on the width of the page.<p>Flexbox, grid, and media queries are some of the heavy hitters in responsive design these days, but wanted to know more about effective use of things like CSS functions like calc, min, max, clamp, minmax (grid only) and when to use them. I can find reference material, but I haven&#x27;t found any good resources that explain when you&#x27;d use them instead of something else.<p>Does anyone have a good resource for responsive design that covers use of CSS functions too? It&#x27;s no good to have a tool in your toolbox if you don&#x27;t know you should be using it.
评论 #33725663 未加载
评论 #33722855 未加载
frankzanderover 2 years ago
I use CSS basically every day but flex-basis, flex-grow and flex-shrink now clear. I had a gut feeling but no motivation to investigate further. If something doesn&#x27;t work with flexbox I just use grid. Now Flexbox gets maybe some more use cases where grid is bit of an overkill.
thedanglerover 2 years ago
Perfect timing. I&#x27;m working with tailwindcss, not by choice. And all good tailwind templates, buttons, styles are for purchase. I guess that is why I gravitate towards boostrap when I can.<p>But this site is helping a lot. Thank you.
评论 #33719575 未加载
评论 #33723956 未加载
评论 #33726706 未加载
telagraphicover 2 years ago
I created <a href="https:&#x2F;&#x2F;www.flexulator.com" rel="nofollow">https:&#x2F;&#x2F;www.flexulator.com</a> for a deeper look into how the spacing algorithm works, you can see how the values are calculated.
评论 #33736607 未加载
steveridoutover 2 years ago
Nice work on the interactive elements in this guide, they&#x27;re delightful!
exodustover 2 years ago
&gt; <i>&quot;One of the biggest Flexbox quality-of-life improvements in recent years has been the gap property. Gap allows us to create space in-between each Flex child, along the primary axis.&quot;</i><p>He is not wrong. It&#x27;s an underrated improvement.<p>I couldn&#x27;t believe it when gap arrived last year (2021) out of nowhere to solve the biggest issue I had with flexbox. No more assigning margins to flex items and dealing with the annoying side effect of that margin applying between item and container edge, rather than just between each item. CSS gap is applied on the flex container, and items can have zero margin, and everything just works now in flexbox.
评论 #33763045 未加载
victor106over 2 years ago
This is great.<p>Unrelated but I was just wondering<p>“Content warning I make a food-related metaphor later in this tutorial.”<p>Why did the author have to include a content warning for food?
评论 #33728161 未加载
评论 #33734574 未加载
Waterluvianover 2 years ago
A challenge for anyone who wants one, and because I’ve been pulling my hair out over this:<p>Make a taller-than-wide rectangle that always fills the maximum available space of the viewport without breaking aspect ratio. Inside have two elements: a square that always fills the max space while maintaining aspect ratio, and a rectangle that takes up all of whatever is left. Inside the inner square lay out 64 (or any n^2) elements as the tiles (like a chess board). No JS.<p>I’ve been fighting with flex and grid and aspect-ratio and getting into one of those rare moments where I feel viscerally angry. I think the main issue is aspect-ratio is a very very weak property.
评论 #33727425 未加载
jdhzzzover 2 years ago
Old guy here. I can remember fighting with and complaining about horizontal scroll bars on wide html pages. I didn&#x27;t realize how good I had it. Now I fight with browser based UI&#x27;s that rearrange themselves to fit in whatever width the browser is set to. Sounds good. Oh, that nav-bar on the left side doesn&#x27;t fit? No problem. Now it appears as a set of sub-tabs arranged horizontally. The app knows where things are, but I don&#x27;t. No thanks.<p>Don&#x27;t get me started on links that aren&#x27;t underlined blue font and only reveal themselves on mouse-over.
评论 #33726195 未加载
jdthediscipleover 2 years ago
This is a beautiful, immensely valuable resource - straight into my bookmarked notes. Thank you!
ricardobayesover 2 years ago
This is really good, the language, the examples, just everything. It&#x27;s right up my alley.
quickthrower2over 2 years ago
It seems like in the example widgets baseline and flex-start appear the same. But according to <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;34606879&#x2F;whats-the-difference-between-flex-start-and-baseline" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;34606879&#x2F;whats-the-diffe...</a> they do different things. Josh&#x27;s examples have text in them, so why is it not aligning to the baseline of that text?<p>Windows 11&#x2F;Latest Chrome FWIW :-)
评论 #33724121 未加载
shloskyover 2 years ago
Easily one of the best guides of flexbox, and this is coming from a mostly backend guy that can never remember flexbox for long term, and always needs a refresh about it.
c-smileover 2 years ago
That reminds me again that flexbox is so needlessly complex that you need quite non-trivial guides each time you use it.<p>It could be significantly simpler and, well, flexible:<p><a href="https:&#x2F;&#x2F;terrainformatica.com&#x2F;2018&#x2F;12&#x2F;11&#x2F;10-years-of-flexboxing&#x2F;" rel="nofollow">https:&#x2F;&#x2F;terrainformatica.com&#x2F;2018&#x2F;12&#x2F;11&#x2F;10-years-of-flexboxi...</a>
评论 #33724638 未加载
bob1029over 2 years ago
I noticed a <i>lot</i> of developers rave about flexbox, but not so much about grid.<p>It is my understanding that flexbox was intended for 1D layouts and grid was intended for 2D layouts. We&#x27;ve been using grid for the overall page layout and flex inside certain regions.<p>Is there something I am missing here, or are we finding that the hammer is pretty good at screws too?
评论 #33722203 未加载
评论 #33721339 未加载
评论 #33722139 未加载
评论 #33726304 未加载
tehsauceover 2 years ago
I’d love an interactive guide that reflects real life experience with css, where every property mysteriously doesn’t work.
评论 #33726105 未加载
tuukkahover 2 years ago
To make complete layouts, you still need to creatively combine (nested) Flexbox with other CSS features. Flexbox was a huge improvement, but I continue to see the need for &quot;tricks&quot; articles that give you well thought-out solutions to practical layout cases.
Existenceblinksover 2 years ago
I wonder if the most use cases today could justify flex as a default where `display:block` is a default and s&#x2F;inline-block&#x2F;inline-flex&#x2F; .. I often have an anxiety of using too many flex elements. How many cases where there&#x27;s a need to un-flex?
coding123over 2 years ago
This is the one I use to get me some layout needs quickly:<p><a href="https:&#x2F;&#x2F;codepen.io&#x2F;justd&#x2F;full&#x2F;yydezN&#x2F;" rel="nofollow">https:&#x2F;&#x2F;codepen.io&#x2F;justd&#x2F;full&#x2F;yydezN&#x2F;</a>
lewantmontrealover 2 years ago
Is there a way to remember what justify&#x2F;align-content&#x2F;items do on a column or row? I write css semi-rarely and go through all options in dev tools until it’s the one I want..
nailerover 2 years ago
Try grid too - unless you need flexing, it&#x27;s simpler than flexbox, even for single columns or rows.
评论 #33720132 未加载
markdownover 2 years ago
Who forgets their middle initial?
tigreznoover 2 years ago
Amazing guide, thanks!
fijiaaroneover 2 years ago
Why is it that any 2 year old can come up with a more concise, precise, and easy to understand layout system than any that has ever been invented?
skilledover 2 years ago
I think this is a great tutorial but I also think it&#x27;s absolutely not needed because of how many other similar guides have already been done.<p>Now, I&#x27;m not trying to be arrogant about it either.<p>This tutorial could have been 10 times better if Josh didn&#x27;t pretend like what he wrote hasn&#x27;t already been done (it has been, a million times, and I don&#x27;t know why you&#x27;d keep beating this dead horse over and over again) - and instead made this same exact tutorial but actually built functional layouts in the process. You know, web pages that use Flexbox.<p>People don&#x27;t learn by clicking on fancy buttons that rearrange some boxes in a specific order. People learn by example and by visually seeing the consequence of an action (in this case, the value of a property).<p>No offense meant at all to the author, of course.
评论 #33723484 未加载