Limit number of lines of text with CSS only featured image

Every now and then, my Clients want me to implement a card-based layout for a list of posts, reports or real estates. In this layout, each card has an image, heading and some text. This looks perfect on the designs, where the text is the same size on all cards. However, when the real content comes in, usually the design gets broken and each card has a different size. In this web dev idea I will show you how to limit the number of lines with CSS only!

Youtube Video – Limit the Number of Lines with CSS only

Demo and code (tl;dr)

See the Pen Untitled by Artur Karczmarczyk (@arturkarczmarczyk) on CodePen.

How to Limit Number of Lines in Block with CSS?

Let’s start by creating an HTML file with cards:

Base HTML file with card-based layout and three sample cards.
Base HTML file with card-based layout and three sample cards.

You will need some images to put in the cards. For that, you can use some free service, like pixabay.com or pexels.com. You can also use Envato Elements, which is a subscription-based platform offering millions of stock photos, graphics, videos at a single monthly subscription fee:

Envato Elements Image
Envato Elements – a subscription-based source of stock photos.

Base Styling for Card-Based Layout

Once the HTML is ready and some images for cards are in place, we can start styling. Here is my basic idea how the styling of cards for card-based layout can be achieved:

Basic styling of CSS cards for a sample card-based layout.
Basic styling of CSS cards for a sample card-based layout.

Use line-clamp CSS Property to Limit Lines of Text in a Paragraph

Once the HTML is ready and the basic styling is in place, we can do our magic! We can use CSS line-clamp property. It would be amazing if we could just type:

A non-prefixed line-clamp CSS property added for limiting the count of lines of text displayed.
A non-prefixed line-clamp CSS property added for limiting the count of lines of text displayed.

However, the line-clamp property is not supported yet. Instead we need to use a prefixed version: -webkit-line-clamp. You can find all details on it here. The -webkit-line-clamp property works only when the display is set to -webkit-box or -webkit-inline-box and the -webkit-box-orient is set to vertical. Let’s try!

Added -webkit-line-clamp property along with other requirements - display and -webkit-box-orient.
Added -webkit-line-clamp property along with other requirements – display and -webkit-box-orient.

The results are not satisfactory yet:

The number of lines of text is still not limited, although -webkit-line-clamp property has been used.
The number of lines of text is still not limited, although -webkit-line-clamp property has been used.

You can notice, that the card boxes are the same size now, however the text is overflowing them. That’s simple to solve, just use the hidden value of the CSS overflow property:

Added overflow:hidden for hiding lines of code exceeding the specified limit of lines in paragraph or block.
Added overflow:hidden for hiding lines of code exceeding the specified limit of lines in paragraph or block.

Finally, our cards look exactly as we wanted!

Results of cards with limited lines with the use of -webkit-line-clamp CSS property.
Results of cards with limited lines with the use of -webkit-line-clamp CSS property.

Summary

In this web dev idea I showed you how you can easily limit the lines in paragraph or block with CSS line-clamp property. I hope that this idea will let you create card-based layouts easily.

I encourage you to check out the Youtube video of this limiting number of lines web dev idea!

If you’d like to learn more on HTML, CSS and LESS in a structured, no-nonsense, hands-on manner, try my Udemy course!

Leave a Reply