A flexbox trick to improve text wrapping

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ธ.ค. 2024

ความคิดเห็น • 286

  • @KevinPowell
    @KevinPowell  7 หลายเดือนก่อน +221

    Quick note: I mispoke when I said flex: 1; will make it a flex-shrink: 0. It makes it flex-grow: 1; flex-shrink: 1; and flex-basis: 0; You'd almost never notice the difference as long as the flex-basis is 0, but still, I did make a mistake there, sorry about that!

    • @dashx2
      @dashx2 7 หลายเดือนก่อน +5

      I usually use white-space: nowrap; in such scenarios. Is there any difference in this scenario between min-width: fit-content or white-space: nowrap?

    • @eioluseyi
      @eioluseyi 7 หลายเดือนก่อน

      @@dashx2 I wish I could link replies here, but @jenstornell answered this in a reply on this video

    • @detore
      @detore 7 หลายเดือนก่อน +2

      Would setting flex-basis:calc(50% - var(--gap)); fix the small screen being different widths problem without affecting anything else?

    • @ismailcreatvt
      @ismailcreatvt 7 หลายเดือนก่อน +1

      Yes I did catch that mistake! But I guess we can let it slide as you showed us such amazing trick. 👌

    • @dashx2
      @dashx2 7 หลายเดือนก่อน

      @@eioluseyi I read his comment. Usually these situtations happen for buttons and most of the time buttons dont have long text taking up more than 100px width so white-space-nowrap rarely is problematic on mobile.

  • @KB04
    @KB04 7 หลายเดือนก่อน +334

    Had the same problem, literally never thought of using fit-content on min/max width.

    • @LePhenixGD
      @LePhenixGD 7 หลายเดือนก่อน +5

      Same

    • @peasantlord135
      @peasantlord135 7 หลายเดือนก่อน +4

      Yeah, I just rolled with no text break haha

    • @hellowill
      @hellowill 5 หลายเดือนก่อน

      I used it the other day, much simpler. Actually chatgpt suggested it.

  • @stairjoke
    @stairjoke 7 หลายเดือนก่อน +395

    I’ve been writing CSS for like 20 years and I feel like I know nothing.

    • @whyisthiscodenotworking
      @whyisthiscodenotworking 7 หลายเดือนก่อน +13

      Yeah correct 💯 💯💯 this guy knows everything in CSS

    • @1lllllllll1
      @1lllllllll1 6 หลายเดือนก่อน +6

      that's because you've been writing CSS since the dawn of time.
      I'm constantly aghast at the improvements of the cascade behavior and element addressing. Why people still use SCSS is becoming increasingly questionable.
      If you're a skilled front end dev, there really shouldn't need to be a need for any of the frameworks any longer. same for javascript and HTML.

    • @zayne-sarutobi
      @zayne-sarutobi 6 หลายเดือนก่อน

      ​@@1lllllllll1Ease of maintenance is why

    • @kadir572
      @kadir572 6 หลายเดือนก่อน +1

      ​@@1lllllllll1well I challenge you to write a frontend framework from scratch then, let's see if it makes sense to use it or not

    • @MRJMXHD
      @MRJMXHD 6 หลายเดือนก่อน +1

      Bruv 😂

  • @pascalmaranus
    @pascalmaranus 7 หลายเดือนก่อน +72

    This is brilliant! Coincidentally, I had to make a small change to an older website today. And I noticed they have a similar issue with two buttons, because the client's changed the text on the buttons. It's bothered me for years at this point. I'm applying this there right away!

  • @Sagan1995
    @Sagan1995 7 หลายเดือนก่อน +58

    Neat! You can also achieve the same results by adding "white-space: nowrap" to the buttons as well.

    • @KlethonioFerreira
      @KlethonioFerreira 7 หลายเดือนก่อน +3

      I think this might even kept the widths the same, I didn't test it

    • @Sagan1995
      @Sagan1995 7 หลายเดือนก่อน +10

      @@KlethonioFerreira it unfortunately doesn't because the minimum width of one button is less than the other. Only giving the buttons a fixed minimum width will keep them equal in size.

    • @Pixelarter
      @Pixelarter 7 หลายเดือนก่อน +8

      @@Sagan1995 It's a bit overkill, but I think with a CSS grid instead of flexbox, it's possible to keep them the same size and make them properly wrap.

    • @Sagan1995
      @Sagan1995 7 หลายเดือนก่อน

      @@Pixelarter I am imagining you can only achieve that in grid by providing a fixed minimum width, which can also be done in flexbox. So unless I'm wrong and there's another way to do it in grid, I am not seeing the advantage.

    • @Pixelarter
      @Pixelarter 7 หลายเดือนก่อน

      @@Sagan1995 You are right. I thought maybe it was possible to do something like _grid-template-columns: repeat(auto-fit, minmax(min-content, 1fr)),_ and get them to always be the same size when squished.
      But it's not possible to use _min-content_ in this case, it only accepts an explicit value.

  • @sourabhgupta3811
    @sourabhgupta3811 7 หลายเดือนก่อน +35

    You just saved developers so much time with your solution to this common issue!

  • @Feedback406
    @Feedback406 7 หลายเดือนก่อน +12

    The best css channel on TH-cam and it’s not even close. Been watching for years Thank you!!!

  • @artneo7
    @artneo7 6 หลายเดือนก่อน +8

    The "min-width: fit-content;" declaration helped me out on a project, really cool tip when working with the flex layout! Thanks! 🙌🏼

  • @larrybahr489
    @larrybahr489 7 หลายเดือนก่อน +13

    I love that this solution does not need media queries! Great video as always

  • @cwirus99
    @cwirus99 7 หลายเดือนก่อน +6

    That's the stuff! A simple, elegant solution to a common problem. Love it!

  • @tonydrake462
    @tonydrake462 5 หลายเดือนก่อน

    thanks - after 40 years dev avoiding CSS, loving finally embracing it, you have added some cool stuff to my toolbox. thanks again.

  • @venomus9286
    @venomus9286 7 หลายเดือนก่อน +13

    I fixed this with white-space: preline; but wasnt a fan how it ended in the end. This solution seems much better. Preline would just force the text not to break, but also had to add prettier ignore so it doesnt break the line. Takes a bit more setup. Thanks a lot Kevin for this solution, much appreciated :D

  • @joshuamitchell6204
    @joshuamitchell6204 7 หลายเดือนก่อน +7

    I used this exact trick on this exact iCodeThis challenge! Love it!

    • @FlorinPop
      @FlorinPop 7 หลายเดือนก่อน +1

      Glad to see you in the comments section also! 🤩

  • @alexjagi3448
    @alexjagi3448 7 หลายเดือนก่อน +3

    Bloody hell that's a nice solution. I was literally working on the same problem last night. But this blows my solution out of the water. Gonna use this right away.
    Thanks.

  • @DevelTime
    @DevelTime 7 หลายเดือนก่อน +2

    I love this format, short gem, one subject, great for archiving for reminding yourself later. Btw. thank you VERY MUCH for being consistent I don't even remember for how long and presenting code and examples in clear way (i.e. big fonts, big pictures, etc) so your viewer don't have to buy new 40" monitor plus pair of new eyes. Thank you!

  • @DeejayShani
    @DeejayShani 2 หลายเดือนก่อน

    This is cool. I use flexbox in a nocode builder and here is how I tackle this issue. I take the minimum width for the button with longer content at which the content stays linear. Then I set that as the minimum width for both boxes. This makes sure that they stay the same size. The only downside is that they will stack much sooner that if we had the min-width: fit-content.

  • @zenu5344
    @zenu5344 7 หลายเดือนก่อน +1

    Great sir! I had this issue some days ago and I just remove the space itself by adding -  and got it solved

  • @JohnH-jt5wq
    @JohnH-jt5wq 5 หลายเดือนก่อน

    Watched this a couple week ago, came in handy today! Thanks Kevin!

  • @adamrmoss
    @adamrmoss 6 หลายเดือนก่อน +27

    I’m just grateful when the text isn’t trunc…

  • @keharacek
    @keharacek 5 หลายเดือนก่อน

    Funnily enough, I came to the exact same solution about a week ago on a bit intricate flex layout. Good tip!

  • @poischiche2933
    @poischiche2933 6 หลายเดือนก่อน

    Once you're facing the solution, the reaction is something like "yes, obviously obvious" but I before never tried to put fit-content to a min-width.
    Great tip 👏

  • @fakeyfakerson2000
    @fakeyfakerson2000 6 หลายเดือนก่อน

    I appreciate you spelling out the whole thought process. I feel like it’ll really help improve my intuition for solving css issues in general.

  • @iamtharunraj
    @iamtharunraj 7 หลายเดือนก่อน +2

    I have had this issue for so long and never knew how to fix them.
    *Thank you so much, Kevin!*

  • @FlorinPop
    @FlorinPop 7 หลายเดือนก่อน +5

    New lesson for today! Thanks Kevin! 🤩

  • @AlFasGD
    @AlFasGD 6 หลายเดือนก่อน +30

    And now a UX tip: don't add too many inferrable words on your buttons. "Add to" is common to both buttons, and doesn't hint the user of any different behavior, so you can either move that to an outer label, or completely skip that text, as the user can infer that "cart" and "wishlist" buttons on the item mean "add to [...]". That makes your buttons shorter, and easier to find and read (reading left-to-right, you have to read "Add to" before reading the "cart" or "wishlist" that you're more interested in).

    • @1lllllllll1
      @1lllllllll1 6 หลายเดือนก่อน

      that's nonsense sorry. consider the ACA, consider the context. you can't ask users to successfully infer anything. what if i already have this in the cart? does "cart" just show me the cart?
      UX people like you are what makes the web horrible. please learn your craft before you spew nonsensical advice in comments. 👎👎👎👎👎👎

  • @DeffQ
    @DeffQ 5 หลายเดือนก่อน

    Someone mentioned you in different video and I'm so happy I discovered your channel. Useful stuff.

  • @WhyWouldHeSayThat
    @WhyWouldHeSayThat 7 หลายเดือนก่อน

    Your timing is insane i was literally just struggling with this on a landing page yesterday. The Goat

  • @sanketgawande3667
    @sanketgawande3667 7 หลายเดือนก่อน +2

    I knew this one!
    Also make sure to add text ellipsis in case text is little longer than available width.

  • @samahgad241
    @samahgad241 7 หลายเดือนก่อน +1

    thanks kevin i faced the same issue ,text wrap not satisfied me also . Awsome solution😍

  • @teugene
    @teugene 7 หลายเดือนก่อน

    Thanks for the reminder! Keep forgetting this exists but we kept relying on flex-direction in media queries.

  • @Avean
    @Avean 7 หลายเดือนก่อน +2

    Had no idea about min-width: fit-content. Love it!

  • @exactzero
    @exactzero 7 หลายเดือนก่อน

    The amount times I encountered this problem. Thanks!

  • @irfanMiral
    @irfanMiral 7 หลายเดือนก่อน

    never thought about this could be a workaround. great solution!

  • @fredhair
    @fredhair 7 หลายเดือนก่อน

    I've done this before, it works well exactly for this sort of case! I think I ended up using it fairly recently in a flexy table where I wanted some columns such as email address column to basically take up more space if needed to prevent the text wrapping but otherwise should try and make the columns even width or at least take up space based on their content, I think I may have also used a min-width: max-content; at one point.

  • @AFTstorm
    @AFTstorm 6 หลายเดือนก่อน

    You know, i never thought of using fit-content this way. Thanks!

  • @dvillegaspro
    @dvillegaspro 7 หลายเดือนก่อน

    Crazy I was literally dealing with this issue the same day you posted this video

  • @marcuszeal
    @marcuszeal 7 หลายเดือนก่อน

    This is clever. I like it! I feel like every video I watch of yours, Kevin, I learn about new CSS variables that I never knew existed or never thought to use the way you're using. Well done and thank you for sharing!

  • @ichiroutakashima4503
    @ichiroutakashima4503 7 หลายเดือนก่อน +1

    I honestly love short contents like these. I had this same issue and your solution was straightforward! I don't have time to watch a 30 minute or 12 hour video and then try to find which chapter and minute is the content I'm looking for. Thanks!

  • @gasparsigma
    @gasparsigma 7 หลายเดือนก่อน +1

    That's great. Very succinct without skipping the details

  • @pnx1990
    @pnx1990 7 หลายเดือนก่อน

    I needed to do that for a two column layout, and keep content centered, I also found it worked using flex and min-with as fit-content

  • @orange1890
    @orange1890 7 หลายเดือนก่อน

    to fix the "downside" we could make it so that the texts take the same size by adding a fixed margin. really cool as always

  • @fabbahiense
    @fabbahiense 7 หลายเดือนก่อน

    It's a nice way to solve it. I've been using this trick for a while now, and it works on most cases!

  • @thedigitalceo
    @thedigitalceo 7 หลายเดือนก่อน

    Oooo fit-content. This is perfect. Run into this issue all the time ❤

  • @EvestTech
    @EvestTech 7 หลายเดือนก่อน

    0:46 Thanks sooo much for explaining. I finally understand XD

  • @JennyZibreva
    @JennyZibreva 7 หลายเดือนก่อน

    this is so beautiful, thank you Kevin!!!

  • @Imperial_Squid
    @Imperial_Squid 7 หลายเดือนก่อน +1

    Oooooh very helpful! I was fiddling around with the youtube CSS the other day because I really hate how it squashes the channel name of there's a join button on the page, had no idea you can tell it like "wrapping text is a last resort, go through all the other rearrangement options first"

  • @source_out
    @source_out 7 หลายเดือนก่อน

    Nice! Definitely better than a fixed min-width which is what I would normally do to solve this, but that can just get messy, this is cool 👍

  • @yahireduardobravotafur5118
    @yahireduardobravotafur5118 6 หลายเดือนก่อน

    El mejor trucazo de CSS, gracias

  • @LetsGenocide
    @LetsGenocide 7 หลายเดือนก่อน

    Thank you very much! This one should be taught in all CSS classes >:o

  • @jiwoo6788
    @jiwoo6788 7 หลายเดือนก่อน

    This came in big time! I'm currently working on something similar! Thanks Kevin!

  • @Play_Streams
    @Play_Streams 7 หลายเดือนก่อน

    That's genius! Thanks for this

  • @Javic167
    @Javic167 7 หลายเดือนก่อน

    I didn't knew you could use fit-content in a width propperty, that's some real usefull thing

  • @shreejipaliwal1215
    @shreejipaliwal1215 7 หลายเดือนก่อน +1

    Very precious video

  • @srsh12345
    @srsh12345 7 หลายเดือนก่อน

    Loved the breakdown. Thank you for the explanation.

  • @hoaxygen
    @hoaxygen 7 หลายเดือนก่อน

    In Tailwind it's "min-w-fit". Super handy!

  • @yarikar
    @yarikar 7 หลายเดือนก่อน

    Amazing! Thank you so much for this tip!

  • @reikooters
    @reikooters 7 หลายเดือนก่อน

    Interesting to see the raw css stuff with flexbox. I started learning HTML 26 years ago, CSS probably been about 20 years and then I never kept up to date or used the newer features like flex and grid etc - by the time I started using those I had switched to tailwind, so I'm used to seeing this type of thing with just a few classes. I must admit if I had to use regular css for flex I wouldn't know what I'm doing. But that fit-content with the width is interesting and will keep it in mind for when it comes in handy one day.

  • @invictuz4803
    @invictuz4803 7 หลายเดือนก่อน

    Love these videos, tackling the real issues!

  • @JeremiahKellogg
    @JeremiahKellogg 7 หลายเดือนก่อน

    Nice! How glad am I this popped up as a recommended video right now. This solved a similar problem I was having and couldn't quite solve properly. This did exactly what I wanted. Thanks!

  • @clevermissfox
    @clevermissfox 7 หลายเดือนก่อน +3

    I’m so excited for this. Had the exact same issue on this exact icodethis challenge !

    • @FlorinPop
      @FlorinPop 7 หลายเดือนก่อน +1

      Miss!! 🫡

    • @clevermissfox
      @clevermissfox 7 หลายเดือนก่อน

      @@FlorinPop Pop!!!! Our guy!

  • @zonaloca
    @zonaloca 6 หลายเดือนก่อน

    This can spare a lot of headaches.

  • @binoypatel09
    @binoypatel09 7 หลายเดือนก่อน

    Simply awesome trick, you are the best

  • @Dipenparmar12
    @Dipenparmar12 7 หลายเดือนก่อน

    Amazing out the box tutorial ❤❤

  • @fexofenadinaGenerica
    @fexofenadinaGenerica 7 หลายเดือนก่อน

    I love these short videos

  • @ZiafatAli
    @ZiafatAli 7 หลายเดือนก่อน

    Whooo 🎉 thank you for sharing ❤

  • @osawereao
    @osawereao 7 หลายเดือนก่อน

    So let me just say " I LOVE YOUR INTELIGENCE"

  • @alfanur_rizal
    @alfanur_rizal 6 หลายเดือนก่อน

    Omg super thank you 😭

  • @RahulKumar-te3ob
    @RahulKumar-te3ob 7 หลายเดือนก่อน

    Brilliant! Loved the solution. Going to try this!

  • @thelaitas
    @thelaitas 7 หลายเดือนก่อน

    It's kinda funny how exciting did I find this haha

  • @jasoncook2715
    @jasoncook2715 7 หลายเดือนก่อน

    Your saving my life there. Thank you

  • @R_Y_Z_E_N
    @R_Y_Z_E_N 6 หลายเดือนก่อน

    Wow i guess im good at css , i figured this out when i heard your problem 😅

  • @UnoUrong
    @UnoUrong 6 หลายเดือนก่อน

    Aaaahhhggghhhh!!! I wish I knew this 2 months ago!

  • @shahfaisal3923
    @shahfaisal3923 7 หลายเดือนก่อน

    Thanks Kevin

  • @k1mpman
    @k1mpman 7 หลายเดือนก่อน

    Beautiful!

  • @eduardointech
    @eduardointech 6 หลายเดือนก่อน

    Thank you!

  • @grugbrain
    @grugbrain 7 หลายเดือนก่อน

    Thanks. This is really cool!

  • @xreev0x
    @xreev0x 7 หลายเดือนก่อน

    this was super cool!

  • @JacksonDiKey
    @JacksonDiKey 7 หลายเดือนก่อน

    Awesome! Thanks for doing our life easier :)))

  • @ВасилийЗорин-з2в
    @ВасилийЗорин-з2в 7 หลายเดือนก่อน

    This is beyond cool. Thanks a lot!

  • @curefilms
    @curefilms 7 หลายเดือนก่อน

    Thanks Kevin. Great Tip

  • @zgo99
    @zgo99 6 หลายเดือนก่อน

    Bro! Instant subscribed.

  • @ssygon2
    @ssygon2 5 หลายเดือนก่อน

    Can you do a tutorial on
    text-wrap: balance;
    Whether still works well with min-width: fit-content.
    Also not sure what difference is using stable, pretty, etc. 😅

  • @Cringe_Username
    @Cringe_Username 7 หลายเดือนก่อน

    woaow super useful, thank you

  • @rfryanfavour4369
    @rfryanfavour4369 7 หลายเดือนก่อน

    i think you save a lot of us a lot but we just don't find your videos sooner 😂 😂

  • @Omar45
    @Omar45 7 หลายเดือนก่อน +2

    Hi Kevin!
    Wouldn't it be better to use grid here instead of flex?
    To achieve the equal-width columns with the buttons taking the whole cell?
    I think this way it's much easier that way, are there any caveats to this approach?

    • @shaunpatrick8345
      @shaunpatrick8345 7 หลายเดือนก่อน

      Using auto-fill/auto-fit I presume. You'd need to find the right width to move the 2nd button to a new row when the text starts to wrap, and if it's international or using a system font, that's not possible.

  • @joyofdevotion
    @joyofdevotion 7 หลายเดือนก่อน

    This is great! I was scratching my head how to do it nicely. One issue I found when there are multiple button. The gap breaks it and keeps space on the right in the container. How do you deal with that?

  • @fscaramuzza
    @fscaramuzza 5 หลายเดือนก่อน

    How do you feel about a more adaptive approach where you reduce the label to it's icon in small view ports?

  • @jenstornell
    @jenstornell 7 หลายเดือนก่อน +1

    Really nice! I usually solve it with white-space: nowrap but that require a different solution on mobile.

    • @eioluseyi
      @eioluseyi 7 หลายเดือนก่อน

      I'm curious about the different solution for mobile; what it is and why it's needed?

    • @jenstornell
      @jenstornell 7 หลายเดือนก่อน +2

      @@eioluseyi A mobile is not very wide. Nowrap prevent the text from wrapping even when it's tight. With accessible in mind it's recommended to be able to zoom 200%. So nowrap on mobile is a big risk and should probably always be avoided, on mobile.

    • @eioluseyi
      @eioluseyi 7 หลายเดือนก่อน +1

      @@jenstornell hmm.. this is actually something I haven't really thought about.
      Thanks for sharing.
      Now, let me go and revisit all my codebases - CMD + SHIFT + F "no-wrap"

    • @jenstornell
      @jenstornell 7 หลายเดือนก่อน

      @@eioluseyi * "nowrap" 😉

  • @kirstenellis1693
    @kirstenellis1693 7 หลายเดือนก่อน

    fit-content.. what a lovely solution to min-width!

  • @IchorDragon
    @IchorDragon 6 หลายเดือนก่อน

    Thought that I'd use experimental @ container to do such dynamic effect, didn't knew that flexbox can do so!

  • @Տ·̇
    @Տ·̇ 7 หลายเดือนก่อน

    Wow I learned a new thing Kevin

  • @mfrodyma1480
    @mfrodyma1480 7 หลายเดือนก่อน

    Great!!! Good explanation.

  • @onio907
    @onio907 7 หลายเดือนก่อน

    this is great solution, thx for sharing

  • @TheLollercaster
    @TheLollercaster 7 หลายเดือนก่อน

    great stuff!

  • @CristianKirk
    @CristianKirk 7 หลายเดือนก่อน

    Pretty cool! I've been using fit-content a lot lately for height specially.

  • @sambasivarao3392
    @sambasivarao3392 17 วันที่ผ่านมา

    Really good.

  • @GetPsyched6
    @GetPsyched6 7 หลายเดือนก่อน

    Great video! great tip as well!

  • @hitmusicworldwide
    @hitmusicworldwide 7 หลายเดือนก่อน

    Are there a transparent characters that can be used to make the length of character lines the same or even them out in some approximate way so that when the boxes themselves are resized due to the length of the text within them the transparent characters will equalise everything out? Back in the old tape setting days when things were printed on paper there were even minor adjustments made between the distances of each letter in a word and between words and a sentence that would be used to justify and even out the amount of space for every container in printing.

  • @hosea_br
    @hosea_br 7 หลายเดือนก่อน

    3:10 to fix that problem, can't we set the min-width to be the width of the content of the larger button? (Hard coding the width in pixels)
    Great vid as always!

  • @QwDragon
    @QwDragon 7 หลายเดือนก่อน

    Very cool!