A flexbox trick to improve text wrapping

Поділитися
Вставка
  • Опубліковано 19 чер 2024
  • Checkout iCodeThis 👉 icodethis.com/?ref=kevin
    🔗 Links
    ✅ My finished version: icodethis.com/modes/design-to...
    ✅ ICodeThis: icodethis.com/?ref=kevin
    ✅ I have a 4-hour long course that deep dives flexbox: flexboxsimplified.com
    Disclaimer: Some of the links above might be affiliate links, which means that I'll receive a commission in case you like and decide to purchase the product or service. This comes at no extra cost for you, and helps support my channel 😊
    ⌚ Timestamps
    00:00 - Introduction
    00:45 - Why flex-grow or flex: 1 don't work
    01:30 - flex-wrap doesn't solve the issue either
    02:00 - min-width to the rescue
    04:05 - more about icodethis
    #css
    --
    Come hang out with other dev's in my Discord Community
    💬 / discord
    Keep up to date with everything I'm up to
    ✉ www.kevinpowell.co/newsletter
    Come hang out with me live every Monday on Twitch!
    📺 / kevinpowellcss
    ---
    Help support my channel
    👨‍🎓 Get a course: www.kevinpowell.co/courses
    👕 Buy a shirt: teespring.com/stores/making-t...
    💖 Support me on Patreon: / kevinpowell
    ---
    My editor: VS Code - code.visualstudio.com/
    ---
    I'm on some other places on the internet too!
    If you'd like a behind the scenes and previews of what's coming up on my UA-cam channel, make sure to follow me on Instagram and Twitter.
    Twitter: / kevinjpowell
    Codepen: codepen.io/kevinpowell/
    Github: github.com/kevin-powell
    ---
    And whatever you do, don't forget to keep on making your corner of the internet just a little bit more awesome!

КОМЕНТАРІ • 253

  • @KevinPowell
    @KevinPowell  Місяць тому +167

    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 Місяць тому +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 Місяць тому

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

    • @detore
      @detore Місяць тому +1

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

    • @ismailcreatvt
      @ismailcreatvt Місяць тому +1

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

    • @dashx2
      @dashx2 Місяць тому

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

  • @stairjoke
    @stairjoke Місяць тому +205

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

    • @whyisthiscodenotworking
      @whyisthiscodenotworking Місяць тому +7

      Yeah correct 💯 💯💯 this guy knows everything in CSS

    • @1lllllllll1
      @1lllllllll1 24 дні тому +1

      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 24 дні тому

      ​@@1lllllllll1Ease of maintenance is why

    • @kadir572
      @kadir572 23 дні тому

      ​@@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 23 дні тому +1

      Bruv 😂

  • @KB04
    @KB04 Місяць тому +273

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

  • @AlFasGD
    @AlFasGD 24 дні тому +6

    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 24 дні тому

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

  • @artneo7
    @artneo7 19 днів тому +5

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

  • @Sagan1995
    @Sagan1995 Місяць тому +32

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

    • @KlethonioFerreira
      @KlethonioFerreira Місяць тому +1

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

    • @Sagan1995
      @Sagan1995 Місяць тому +7

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

    • @jaysonbautista95
      @jaysonbautista95 Місяць тому

      This is what I usually do when I encounter this issue, just text wrapping.

    • @Pixelarter
      @Pixelarter 26 днів тому +2

      @@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 25 днів тому

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

  • @pascalmaranus
    @pascalmaranus Місяць тому +65

    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!

  • @sourabhgupta3811
    @sourabhgupta3811 Місяць тому +31

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

  • @matizbrave
    @matizbrave Місяць тому +4

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

  • @iamtharunraj
    @iamtharunraj Місяць тому +2

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

  • @Feedback406
    @Feedback406 Місяць тому +10

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

  • @ichiroutakashima4503
    @ichiroutakashima4503 Місяць тому +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!

  • @DevelTime
    @DevelTime Місяць тому +1

    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!

  • @fakeyfakerson2000
    @fakeyfakerson2000 14 днів тому

    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.

  • @alexjagi3448
    @alexjagi3448 Місяць тому +2

    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.

  • @marcuszeal
    @marcuszeal Місяць тому

    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!

  • @larrybahr489
    @larrybahr489 Місяць тому +10

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

  • @lewybagz
    @lewybagz 25 днів тому

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

  • @joshuamitchell6204
    @joshuamitchell6204 Місяць тому +7

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

    • @FlorinPop
      @FlorinPop Місяць тому +1

      Glad to see you in the comments section also! 🤩

  • @JeremiahKellogg
    @JeremiahKellogg Місяць тому

    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!

  • @venomus9286
    @venomus9286 Місяць тому +12

    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

  • @jiwoo6788
    @jiwoo6788 Місяць тому

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

  • @FlorinPop
    @FlorinPop Місяць тому +5

    New lesson for today! Thanks Kevin! 🤩

  • @gasparsigma
    @gasparsigma Місяць тому +1

    That's great. Very succinct without skipping the details

  • @samahgad241
    @samahgad241 Місяць тому +1

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

  • @fabbahiense
    @fabbahiense Місяць тому

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

  • @JennyZibreva
    @JennyZibreva Місяць тому

    this is so beautiful, thank you Kevin!!!

  • @poischiche2933
    @poischiche2933 11 днів тому

    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 👏

  • @RahulKumar-te3ob
    @RahulKumar-te3ob Місяць тому

    Brilliant! Loved the solution. Going to try this!

  • @invictuz4803
    @invictuz4803 Місяць тому

    Love these videos, tackling the real issues!

  • @zenu5344
    @zenu5344 Місяць тому

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

  • @srsh12345
    @srsh12345 Місяць тому

    Loved the breakdown. Thank you for the explanation.

  • @yarikar
    @yarikar 27 днів тому

    Amazing! Thank you so much for this tip!

  • @binoypatel09
    @binoypatel09 Місяць тому

    Simply awesome trick, you are the best

  • @irfanMiral
    @irfanMiral Місяць тому

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

  • @exactzero
    @exactzero Місяць тому

    The amount times I encountered this problem. Thanks!

  • @WilliamShrek
    @WilliamShrek Місяць тому

    Thanks. This is really cool!

  • @source_out
    @source_out Місяць тому

    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 👍

  • @Dipenparmar12
    @Dipenparmar12 29 днів тому

    Amazing out the box tutorial ❤❤

  • @user-kw6sv7kf5l
    @user-kw6sv7kf5l Місяць тому

    This is beyond cool. Thanks a lot!

  • @sanketgawande3667
    @sanketgawande3667 Місяць тому +2

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

  • @teugene
    @teugene Місяць тому

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

  • @Imperial_Squid
    @Imperial_Squid Місяць тому +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"

  • @jasoncook2715
    @jasoncook2715 Місяць тому

    Your saving my life there. Thank you

  • @k1mpman
    @k1mpman Місяць тому

    Beautiful!

  • @dvillegaspro
    @dvillegaspro Місяць тому

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

  • @curefilms
    @curefilms Місяць тому

    Thanks Kevin. Great Tip

  • @Avean
    @Avean Місяць тому +2

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

  • @fredhair
    @fredhair Місяць тому

    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.

  • @Play_Streams
    @Play_Streams Місяць тому

    That's genius! Thanks for this

  • @ZiafatAli
    @ZiafatAli Місяць тому

    Whooo 🎉 thank you for sharing ❤

  • @thedigitalceo
    @thedigitalceo Місяць тому

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

  • @GetPsyched6
    @GetPsyched6 Місяць тому

    Great video! great tip as well!

  • @Cringe_Username
    @Cringe_Username 28 днів тому

    woaow super useful, thank you

  • @eduardointech
    @eduardointech 23 дні тому

    Thank you!

  • @mfrodyma1480
    @mfrodyma1480 Місяць тому

    Great!!! Good explanation.

  • @onio907
    @onio907 Місяць тому

    this is great solution, thx for sharing

  • @fexofenadinaGenerica
    @fexofenadinaGenerica Місяць тому

    I love these short videos

  • @xreev0x
    @xreev0x Місяць тому

    this was super cool!

  • @LetsGenocide
    @LetsGenocide Місяць тому

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

  • @TheLollercaster
    @TheLollercaster Місяць тому

    great stuff!

  • @pnx1990
    @pnx1990 Місяць тому

    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

  • @AFTstorm
    @AFTstorm 3 дні тому

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

  • @yahireduardobravotafur5118
    @yahireduardobravotafur5118 6 днів тому

    El mejor trucazo de CSS, gracias

  • @JacksonDiKey
    @JacksonDiKey Місяць тому

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

  • @amilww
    @amilww Місяць тому

    Very handy!

  • @CristianKirk
    @CristianKirk Місяць тому

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

  • @EvestTech
    @EvestTech Місяць тому

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

  • @alfanur_rizal
    @alfanur_rizal 12 днів тому

    Omg super thank you 😭

  • @OCEMTechZone
    @OCEMTechZone Місяць тому

    Useful fix found thanks 🎉

  • @shreejipaliwal1215
    @shreejipaliwal1215 Місяць тому +1

    Very precious video

  • @shahfaisal3923
    @shahfaisal3923 24 дні тому

    Thanks Kevin

  • @zgo99
    @zgo99 14 днів тому

    Bro! Instant subscribed.

  • @R_Y_Z_E_N
    @R_Y_Z_E_N 22 дні тому

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

  • @QwDragon
    @QwDragon Місяць тому

    Very cool!

  • @zonaloca
    @zonaloca 16 днів тому

    This can spare a lot of headaches.

  • @reikooters
    @reikooters Місяць тому

    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.

  • @modi_hemnt77
    @modi_hemnt77 Місяць тому

    Very helpful

  • @kebunkarta6257
    @kebunkarta6257 Місяць тому

    Thank you

  • @user-pv3rq5ib3y
    @user-pv3rq5ib3y Місяць тому

    Wow I learned a new thing Kevin

  • @UnoUrong
    @UnoUrong 16 днів тому

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

  • @Javic167
    @Javic167 Місяць тому

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

  • @AndreaCuchetto
    @AndreaCuchetto Місяць тому

    So smart!

  • @rodrigocorvalan4895
    @rodrigocorvalan4895 Місяць тому

    Nice trick! I use white space but I will try this

  • @rfryanfavour4369
    @rfryanfavour4369 Місяць тому

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

  • @hoaxygen
    @hoaxygen Місяць тому

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

  • @mattsmith5856
    @mattsmith5856 Місяць тому

    Nice!

  • @clevermissfox
    @clevermissfox Місяць тому +3

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

  • @colindante5164
    @colindante5164 Місяць тому

    thanks))

  • @AndyGaskin
    @AndyGaskin Місяць тому

    great tip

  • @thelaitas
    @thelaitas Місяць тому

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

  • @keessonnema
    @keessonnema Місяць тому

    What are the chances! I ran across this problem last week, this will solve it! Thanks

  • @orange1890
    @orange1890 Місяць тому

    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

  • @user-ko1cw6mv4q
    @user-ko1cw6mv4q 27 днів тому

    Incredible 😛😛

  • @IchorDragon
    @IchorDragon 11 днів тому

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

  • @patricknelson
    @patricknelson Місяць тому

    Awesome tip! A very rare “ah hah!” moment from me. 😅

  • @ernestoj.suarezpons9352
    @ernestoj.suarezpons9352 Місяць тому

    K, your videos are greater on time!

  • @ScottSavageTechnoScavenger
    @ScottSavageTechnoScavenger Місяць тому

    NICE!!!!!!!

  • @devonchia5976
    @devonchia5976 Місяць тому

    Cool sharing! Not going to try out now but jus wondering if this works for both chrome and Firefox? Edge case fixes like this sometimes don't work on either one

  • @joyofdevotion
    @joyofdevotion Місяць тому

    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?