How to set a maximum number of lines of text with CSS

Limiting the total visible lines of text within an element is something I get asked about pretty frequently, and while there are probably some complex JavaScript solutions, there is a simple, though slightly strange, way to do it with CSS only.
#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 KZread 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!

Пікірлер: 149

  • @janetrajkoski1424
    @janetrajkoski1424 Жыл бұрын

    I sometimes think that KZread and Kevin have some sort of a conspiracy in which they hear my problems and a day after that Kevin comes out with a video solving that same problem with CSS. Literally happened in the last two videos, in which i took the JS approach to solve them. If I only knew... (researched more...)

  • @sylviaeguzoro7978

    @sylviaeguzoro7978

    Жыл бұрын

    You know this! 😂

  • @joseph_connelly

    @joseph_connelly

    Жыл бұрын

    lol or he's good at addressing common problems :D not to intentionally add to your conspiracy, but this was my problem yesterday as well, I solved using JS too, then morning comes and there's this clean solution :D I assume it's KZread and Google whom are getting better at sharing our web history and providing like-content in across platforms. I personally like it - I think it's counterintuitive to think the process is "creepy" or "invasive" when more often than not it's helpful like in this situation :)

  • @salimal-tarsha5564

    @salimal-tarsha5564

    Жыл бұрын

    That did happend to me two !!

  • @nataliesmyth9013

    @nataliesmyth9013

    Жыл бұрын

    Seriously same.

  • @wchorski

    @wchorski

    11 ай бұрын

    You could use Google Trends to track what people are searching for

  • @sugacoma
    @sugacoma Жыл бұрын

    I've learned more from you in the last two weeks of viewing than the last 2 years of tinkering with existing CSS , thank you

  • @nerdydragon42
    @nerdydragon42 Жыл бұрын

    This is legit what I was wanting on a community post forum design I'm working on for our Blazor redesign! Thanks a ton!

  • @uridevmedia
    @uridevmedia Жыл бұрын

    This is powerful, you've not only made me love CSS, but I've gotten better watching your videos

  • @Ma1ne2
    @Ma1ne2 Жыл бұрын

    Damn this is sooo useful! Loving these shorter, one quick topic videos, keep em coming!

  • @thechessthinker
    @thechessthinker Жыл бұрын

    I wanted this! Thanks Kevin!

  • @jaidhingra2811
    @jaidhingra2811 Жыл бұрын

    Amazing! I was looking for the solution to this problem from last year, but found it now. Thank You!

  • @Happyendrew
    @Happyendrew6 ай бұрын

    Thank you, my dear friend! Your help is always appreciated. Wishing you all the best!

  • @waleedsharif618
    @waleedsharif618 Жыл бұрын

    I just needed this TODAY !

  • @Zakum
    @Zakum Жыл бұрын

    excellent work as always kevin

  • @MrKOHKyPEHT
    @MrKOHKyPEHT Жыл бұрын

    Really really really and extremely useful video. Few days ago faced that problem at work and investigation took around 1 hour. Thx for video. In favorites to not lose it.

  • @charlesxavier77
    @charlesxavier77 Жыл бұрын

    Love it men!!! Thank you!!!

  • @trammellwebdev
    @trammellwebdev Жыл бұрын

    This is great, hopefully we get full support for this soon!

  • @sabiruli
    @sabiruli Жыл бұрын

    That's amazing and helpful. Thanks @Kevin

  • @amilww
    @amilww Жыл бұрын

    Awesome feature! Thanks a lot!

  • @bobdinitto
    @bobdinitto Жыл бұрын

    Thanks Kevin I've often wondered how to do this.

  • @bimaltwayana2058
    @bimaltwayana2058 Жыл бұрын

    you're amazing Mr. Kevin Powell. Love your work. Thank you🙃

  • @MatheusSantos-rh5jo
    @MatheusSantos-rh5jo8 ай бұрын

    Works here. Thank you!

  • @mimmobordini9466
    @mimmobordini9466 Жыл бұрын

    I love you. I stopped using Bootstrap etc since 2021 thanks to your videos, now everything I do is with pure CSS

  • @LePhenixGD
    @LePhenixGD Жыл бұрын

    Amazing stuff! Was really wondering if you could create a multi-line ellipsis and your video came out in the right moment!

  • @ornous

    @ornous

    Жыл бұрын

    This! It's like he knows what we're up to ^_^

  • @MentorMode
    @MentorMode Жыл бұрын

    Amazing content. For the record, display: -webkit-box; is actually display: flex; as well as -webkit-box-orient: vertical: is flex-direction: column;

  • @vacitabdulrazak5968

    @vacitabdulrazak5968

    Жыл бұрын

    I think it's not really the same, tried it but nothing changed..

  • @MentorMode

    @MentorMode

    Жыл бұрын

    @@vacitabdulrazak5968 actually it's better to use display flex always, however some browsers doesn't know how to handle flex, so that -webkit-box comes to help (in some cases). So, it is recommended to add display: -webkit-box; after display: flex;

  • @anonymous-tg3sh
    @anonymous-tg3sh Жыл бұрын

    I have used this in the past, copied from stack overflow, but had no clue how it worked 😅.thanks for explaining.👏

  • @SebastianZartner
    @SebastianZartner Жыл бұрын

    Can't wait to get the standardized version of this! The prefixed solution is pretty hacky and has several issues besides clamping everywhere. At least all browsers already support the prefixed solution for quite some time, so browser support for that is really good.

  • @guyefrat2473
    @guyefrat2473 Жыл бұрын

    thanks for knowledge, always inspiring

  • @samahgad241
    @samahgad2414 ай бұрын

    Realy i am deeply in love with css and mostly with the way you teach us😍

  • @TomasMisura
    @TomasMisura Жыл бұрын

    Thank you for this video !!!

  • @DJMoreMuzic
    @DJMoreMuzic Жыл бұрын

    Great! Thank you always💯.

  • @ZackPyle
    @ZackPyle Жыл бұрын

    I use this a lot. To get around the height thing at the end, I often just give that element a fixed height since I'm already clamping it to 3 lines (meaning I know the height)

  • @theberengersniper

    @theberengersniper

    Жыл бұрын

    Do your sites prevent users from increasing browser font size? If someone increases the font size for accessibility, you can't know the height.

  • @zackpyle496

    @zackpyle496

    Жыл бұрын

    @@theberengersniper Of course. It's easy! Set the height in em

  • @kylevandeusen
    @kylevandeusen Жыл бұрын

    Just used this earlier this week! 🙌

  • @necatbolpaca7882
    @necatbolpaca7882 Жыл бұрын

    You can wrap the clamped content with a div has a "flex-grow:1;" property for shorter content. The div grows and pushes the button to bottom, so the buttons will be aligned. (For line-clamp: 5 situation.)

  • @snapstromegon

    @snapstromegon

    Жыл бұрын

    I was about to comment the same just using grid instead of flex, but both work here.

  • @The-Torbey

    @The-Torbey

    Жыл бұрын

    @@snapstromegon I know the flexbox approach but how would you do it with grid?

  • @BauldyBoys

    @BauldyBoys

    Жыл бұрын

    @@The-Torbey I don't think you would want to use a grid for the card content. But something to the affect of grid-template-row: max-content 1fr max-content; Note: you must wrap your content container in an additional element or the sizing will override the line-clamp.

  • @travelwithharshal
    @travelwithharshal Жыл бұрын

    Usefull thank you so much making this video

  • @Mirko_ddd
    @Mirko_ddd Жыл бұрын

    Very useful, thanks 😎

  • @juancastrillon5255
    @juancastrillon5255 Жыл бұрын

    it's works, thank you very much bro

  • @bilatungdulang9708
    @bilatungdulang9708 Жыл бұрын

    Wow, it's really usefull, thanks my friend

  • @SogMosee
    @SogMosee Жыл бұрын

    Hey Kevin, can you use css to make a shimmer effect for resizable placeholder skeletons for any width? Would be much appreciated

  • @tadeomunoz2332
    @tadeomunoz233211 ай бұрын

    Una solución super simple, muchas gracias!

  • @isancreativedesigns9259
    @isancreativedesigns9259Ай бұрын

    Oh you beauty! I had been using another method but it suddenly failed so I was doing some practice with your 4 card tutorial yesterday (The hour 11 mins one) and thought I'd try again ...... Thank you!

  • @FalioV
    @FalioV Жыл бұрын

    Well thats interesting aprouch to do. I usually do this as you said at the begining. Cut the string and some length and concat "..." :D I love your videos, you are always ahead of time if I can say :D

  • @miasbeck
    @miasbeck4 ай бұрын

    0:3:18 I think an additional min-height: 5lh; could solve the problem an make each the same height.

  • @AbhishekYadav-fy5yl
    @AbhishekYadav-fy5yl Жыл бұрын

    Thank you....your videos are very helpful to me.... a lot to learn about css....love from India❤️🇮🇳

  • @davidepalombo2141
    @davidepalombo2141 Жыл бұрын

    Thank you very much :)

  • @amjedh7133
    @amjedh7133 Жыл бұрын

    another useful trick from u thnx

  • @aaeonCodes
    @aaeonCodes Жыл бұрын

    Thank you

  • @user-ko8yx5th6m
    @user-ko8yx5th6m9 ай бұрын

    Thank You

  • @TryingCodeEng-58208
    @TryingCodeEng-582084 ай бұрын

    thaks for this video .. nice video sir

  • @AquaRedX
    @AquaRedX Жыл бұрын

    This was awesome I think. Yeah it's a line clamp instead of letter-clamp so you could specify when it cuts off.

  • @jemrl
    @jemrl Жыл бұрын

    I was doing this with JS but CSS is much cleaner, cheers!

  • @tcgvsocg1458
    @tcgvsocg1458 Жыл бұрын

    thx a lot for the video

  • @pierre-louisdrevon2213
    @pierre-louisdrevon2213 Жыл бұрын

    good simple tip 👍

  • @mh_kohansal
    @mh_kohansal Жыл бұрын

    thank's alot dude.

  • @Turabbo
    @Turabbo Жыл бұрын

    Edit: Just did a bit of digging and it sounds like the news on an official line-clamp is pretty bad? A proposed addition way back in 2018, and no signal from any browsers for future support... ☹️ --- Honestly this is probably my biggest personal niggle with the current state of CSS haha. I use line clamping all the time in my designs, and I clicked your video to see whether there was any good news. Over the moon to hear a single property is in the spec!! I can't wait! I have a sass mixin to handle this for other devs in my framework, but there's still so much jank with it. Like the block padding overflow issue, and the fact that -webkit-box behaves almost-but-not-quite like flex. Can't wait for a proper implementation!!

  • @KevinPowell

    @KevinPowell

    Жыл бұрын

    There's some movement on some parts that have been quiet for awhile now, so it might happen. One issue is, even though Chromium and Safari are run by massive companies, they have relatively small teams (and FF has an even smaller team), and they have to prioritize some features over others. This type of thing tends to get a lower priority, specially with an implementation, even if it's a bit buggy

  • @Turabbo

    @Turabbo

    Жыл бұрын

    @@KevinPowell Yeah makes sense. Very hard to justify something that "works" already.

  • @deniskotov
    @deniskotov Жыл бұрын

    The typo was nice. Whackbit - sounds great.

  • @shaunazar
    @shaunazar Жыл бұрын

    @kevin powell is there a way to use clamp function to evenly wrap a paragraph - think of a paragraph floating in a container, where the first line is the full width of the container and the second line is super short. I tried playing with the clamp function but I can't figure it out or perhaps it doesn't exist? What I would like to happen is for the each line would be the same width. Maybe this is only possible with JS?

  • @saqibiqbal9455
    @saqibiqbal9455 Жыл бұрын

    Awesome 👌

  • @stacymichael144
    @stacymichael144 Жыл бұрын

    Great video. Found a situation though where in chrome I do exactly what you do but want only 2 lines to show. As soon as I set overflow: hidden all but the first line displays and the ellipse goes away like the word wrap was turned off. What is breaking the line clamp is the parent container is a display:flex; flex-direction: row;

  • @sgt.powerpuff3107
    @sgt.powerpuff3107 Жыл бұрын

    my god this is a life-changer... in ONE LINE

  • @sanmeetsingh4
    @sanmeetsingh4 Жыл бұрын

    Super cool 😃

  • @Tiagocf2
    @Tiagocf2 Жыл бұрын

    i love it

  • @pal1bose
    @pal1bose10 ай бұрын

    Thanks for this css trick. I needed something like this, but came across a problem while using it. For some reason the -webkit-box-orient styling element gets removed from the transpiled code when I compile it in angular. Some posts suggested wrapping it in autoprefixer code will do the trick, but I was not able to make it work. Do you know why that happens? Any work around?

  • @olivierheimerdinger1629
    @olivierheimerdinger1629 Жыл бұрын

    So powerfull effect

  • @ByteCalm
    @ByteCalm Жыл бұрын

    Thanks Kevin for such a usefull feature. Just in case, the line-clamp acts weird when it comes to languages with longer words like German. It tries to truncate too early rather than just truncate some characters of the last lines word. the easy and dirty solution would be a combination of line-clamp and break-word: break-all

  • @FunDumb
    @FunDumb Жыл бұрын

    Much love for css.

  • @Ericsicons
    @Ericsicons7 ай бұрын

    Is there a way to get the ... to show based on container height? for example if the container's height can only accommodate one line it will make the ... on the end of the first line, if it can take two the ... will show on the second line

  • @nevengerencir5423
    @nevengerencir5423 Жыл бұрын

    Kevin! What css course I should take from you? I gotta start nesting and creating variables for my css in order to switch state in React fast. Recommend me something please! Till now my css structure looks really bad! I know grid flexbox, box model etc but my css structure is just BAD!

  • @user-iv8ec8ti4y
    @user-iv8ec8ti4y7 ай бұрын

    Hi kevin! thank for the video! any idea why this webkit css doesn't work on mobile?

  • @munbee_
    @munbee_ Жыл бұрын

    I have used this a lot of times 🙂

  • @mohammadamit6996
    @mohammadamit6996 Жыл бұрын

    Thanks a billion...💖💖💖💖

  • @perceptixn
    @perceptixn Жыл бұрын

    display: -webkit-box is the og flexbox (is the first iteration of it)

  • @blackpurple9163
    @blackpurple9163 Жыл бұрын

    Extremely useful, currently working on an e-commerce website clone with product card with description and this will help a lot there. Btw where do you learn about these upcoming and experimental CSS things from?

  • @KevinPowell

    @KevinPowell

    Жыл бұрын

    In this case, when I looked into it, I found the actual spec and wondered what was up with the prefixes, so just kept reading. I also spend way too much time reading the spec and keeping up when changes happen :P

  • @blackpurple9163

    @blackpurple9163

    Жыл бұрын

    @@KevinPowell yes but read where? Is there an official website or some place which I'm not aware of? I'm just a student but I love how much power CSS has to alter basic HTML and turn it into beautiful art pieces

  • @hrushikeshmarathe5513
    @hrushikeshmarathe551310 ай бұрын

    Thanks.

  • @JeremyBolanos
    @JeremyBolanos Жыл бұрын

    Drat, I had to implement this a couple of months ago and I pulled out my trusty old JavaScript code. Frick it, I'm not going back.

  • @GR_BackingTracks
    @GR_BackingTracks9 ай бұрын

    Trying this today... if I find time, that is

  • @patrykolejniczak8651
    @patrykolejniczak8651 Жыл бұрын

    Hey! I've just used this few days ago 😉

  • @MewtwoStar
    @MewtwoStar6 ай бұрын

    What we have to do for that less line content card?

  • @gauravvarma3645
    @gauravvarma36455 күн бұрын

    Yet another reason to use tailwind: with a simple line like line-clamp-3 tailwind does all the set up for you!

  • @arshad1781
    @arshad1781 Жыл бұрын

    Nice 🙂

  • @PhilLesh69
    @PhilLesh69Ай бұрын

    I wonder if there is a way to use shadow, or gradient or opacity and overlay to create a tiny blurring or greying out of the text to the left of the ellipsis to make any cut-off words less visible to make that cutoff look less clumsy or unintended. Sort of like the way "read more" is handled on some sites where it covers a paragraph and fades out that paragraph, and when you click read more it removes that overlay and displays the rest of the page text, or whatever.

  • @PhilLesh69

    @PhilLesh69

    Ай бұрын

    Like a :before on an ellipsis pseudo element the way you can style a scrollbar or whatever.

  • @OCEMTechZone
    @OCEMTechZone3 ай бұрын

    Great

  • @farahmalla3614
    @farahmalla3614 Жыл бұрын

    How i can stretch cards to be in same height and show all content in it (not hide the content and show just three or four lines)?

  • @KevinPowell

    @KevinPowell

    Жыл бұрын

    If they're flex items, the default is to stretch to match height

  • @Jason-mk3nn
    @Jason-mk3nn15 күн бұрын

    Is there any way to control it by number of characters?

  • @kiddhkane
    @kiddhkane Жыл бұрын

    According to caniuse, at this point every browser supports line-clamp :)

  • @patricknelson
    @patricknelson Жыл бұрын

    It’s interesting that this was implemented in all browsers (including Firefox) with the “-webkit-“ prefix. 🧐

  • @KevinPowell

    @KevinPowell

    Жыл бұрын

    Most of the non-standard properties have gone in that direction, not that there are many of them left.

  • @LarsEvanJ
    @LarsEvanJ Жыл бұрын

    Thank you..

  • @BryanMaxDobberstein
    @BryanMaxDobberstein Жыл бұрын

    Wekbit is the name of my next band.

  • @taz2392
    @taz2392 Жыл бұрын

    Hi Will WebKit work for subgrid? It’s been so long since subgrid is supported by browsers I’m losing my mind.

  • @KevinPowell

    @KevinPowell

    Жыл бұрын

    No, but Chrome is working on it, and it's supported by Safari and Firefox now, so it shouldn't be too much longer :)

  • @djblast101
    @djblast101 Жыл бұрын

    Mmm wonder 🤔 why it cuts off in a word looks like that specific word would wrap to the next line....but yet it's in the same line

  • @KevinPowell

    @KevinPowell

    Жыл бұрын

    It's hard to predict how it ends up cutting stuff

  • @mahedihasan7849
    @mahedihasan7849 Жыл бұрын

    What is the difference between flex vs grid

  • @KevinPowell

    @KevinPowell

    Жыл бұрын

    That's a big question 😅 - I do have a lot of content that looks at both, if you search on my channel for flex vs. grid you should find a few videos

  • @benloud8740
    @benloud87409 ай бұрын

    Needs to be standardised. Submit a proposal to the working group!

  • @KevinPowell

    @KevinPowell

    9 ай бұрын

    There is a standardized line-clamp in the spec, but no browsers support it yet

  • @JasonJA88
    @JasonJA88 Жыл бұрын

    👍

  • @ArielZab
    @ArielZab Жыл бұрын

    Please do one on env(safe-area-inset-*)

  • @KevinPowell

    @KevinPowell

    Жыл бұрын

    Oh yeah, that's a good one!

  • @ArielZab

    @ArielZab

    Жыл бұрын

    @@KevinPowell i cant figure it out for the life of me!

  • @KevinPowell

    @KevinPowell

    Жыл бұрын

    @@ArielZab it's mostly for edge cases, say for dealing with the notch on iPhones or other UI issues like that

  • @ArielZab

    @ArielZab

    Жыл бұрын

    @@KevinPowell i need to use it but i cant get it to work & I dont see a lot of tutorials on it

  • @Zakaros1000
    @Zakaros1000 Жыл бұрын

    Maybe you don't make me love CSS more, but at least hate it less 😅

  • @amiralishoja
    @amiralishoja3 ай бұрын

    Can we not enjoy css while we have you?❤

  • @unpollo93
    @unpollo93 Жыл бұрын

    this is what we used to call magic

  • @andreyt6699
    @andreyt66994 ай бұрын

    Worked for web browsers but not for Safari in IPhone :(

  • @Joey-ni8kh
    @Joey-ni8kh Жыл бұрын

    somebody put this on codepen ! lol

  • @NoitSaab
    @NoitSaab Жыл бұрын

    At 1:32 mark you mention there is a way to get it to work if css doesnt support the -webkit-line-clamp, is there a js polyfill? kzread.info/dash/bejne/lGqduKSyc7LeaKQ.html

  • @KevinPowell

    @KevinPowell

    Жыл бұрын

    I'd rather use a prefix than require JS 🤷‍♂️

  • @JayJames
    @JayJames Жыл бұрын

    wekbit

  • @deusprogrammer_thekingofspace
    @deusprogrammer_thekingofspace Жыл бұрын

    Everytime I think I know all of the display types, I get hit with another one.

  • @deusprogrammer_thekingofspace

    @deusprogrammer_thekingofspace

    Жыл бұрын

    Since I have your attention, just wanted to say your videos helped me a lot with modernizing my Twitch App I am working on. I am eternally grateful. Sent a lot of students to your videos too where I work. Keep up the amazing work!

  • @mmuralikrishna2881
    @mmuralikrishna2881 Жыл бұрын

    line-clamp doesn't support all browsers. We need use -webkit-line-clamp.

  • @mountaindev7161
    @mountaindev7161 Жыл бұрын

    it doesn't work with Safari so not a good option.