7559
4:57 am, June 18, 2022

Truncate Text to an amount of lines using line-clamp

In a previous post (Truncate your Text with CSS) we truncated some text into a smaller single line element, which can be good for single line items, but what if you had a paragraph or a box of text that you wanted to also be truncated automatically with css.


demo box with line clamp 3

How can we truncate the text?

To truncate the text to a certain amount of lines, we can use the css property line-clamp.

line-clamp support

We can check over here on can i use, if line-clamp is supported by the most used browsers. It seems that is is supported by most of the browsers now, but it has to be prefixed with -webkit like this -webkit-line-clamp.


here is a can i use on the css clamp, you might need to check the latest support here.

line clamp other requirements

Other requirements for this to work is to have the display set to -webkit-box, so on the element you also need to add: display: -webkit-box

Setting up the classes

Setup some classes for 3 and 4 line clamp. demo-box and line-clamp-3, 4. You can see these in action below.

Allowing the full text to still be read

If we still want the text to be read even when its truncated, you can add a title tag on the element and then it will appear as a tooltop if you mouse over the element.


adding a title tag


example showing how the title tooltip text will appear

Element Padding

I originally had some padding on the element as i usually do to make it look less cramped, but this does not work well with the line clamping, as you can see the hidden lines.

So rather than using padding on the element i added a border, which still allows the element to be padded and also hides the text underneath the border.

Truncate Text to an amount of lines using line-clamp Demo

View Demo Full Screen View Demo New Tab

Truncate Text to an amount of lines using line-clamp Code

HTML

<!-- HTML -->
<h3>Line Clamp 3</h3>
<div class='demo-box line-clamp-3'>
	This is some demo line clamp text. This text should be clamped to three lines unless there is not enough text to be clamped.
</div>
<h3>Line Clamp 4</h3>
<div class='demo-box line-clamp-4'>
	This is some demo line clamp text. This text should be clamped to four lines unless there is not enough text to be clamped. Also this box needs to have overflow hidden on it and the webkit box orient set to vertical.
</div>

<h3>Allowing the full text to still be read</h3>
<div class='demo-box line-clamp-4' title='This is some demo line clamp text. This text should be clamped to four lines unless there is not enough text to be clamped. Also this box needs to have overflow hidden on it and the webkit box orient set to vertical.'>
	This is some demo line clamp text. This text should be clamped to four lines unless there is not enough text to be clamped. Also this box needs to have overflow hidden on it and the webkit box orient set to vertical.
</div>

CSS

/* CSS */
.demo-box {
	max-width:220px;
	padding:0px;
	background:#CCC;
	color:#444;
	border-radius:10px;
	margin:10px;
	overflow: hidden;
	border:10px solid #CCC;
	box-shadow:0px 2px 10px #000;
}
.line-clamp-3 {
	display: -webkit-box;
	-webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
	overflow: hidden;
}
.line-clamp-4 {
	display: -webkit-box;
	-webkit-box-orient: vertical;
  -webkit-line-clamp: 4;
	overflow: hidden;
}

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Items in CSS Text Formatting
Search Code
Search Code by entering your search text above.
Welcome to CSSBundle.com

Hi there and welcome to CSSBundle. Here at CSS Bundle we aim to find the best css and javascript bundles and demos and add them here to make them easy to find all in one place.

If you would like to stay up to date with our latest updates you can subscribe to our mailing list where we will send you one email per week with the latest updates, no spam emails just the latest updates.

Random Quote


Latest Code
css animations Falling Stars CSS Animation html Video different sources on screen sizes video embed html responsive css animations Animated Triangles on Canvas JS HTML HTML Canvas Instant colour fill with HTML Canvas javascript Transform Text into Images using SnapText JS CSS Tools Fancy Border Radius CSS Generator Tool - spin animation keyframes CSS Tools CSS Drop Filter Shadow Creator CSS Slider A CSS and HTML Only Carousel Slider CSS Z-Index Z-Index Code Front - CSS to change z-index when element is active sections section with image to the right text to the left gradient Sections Two Clickable Logo Promo Box Section Foundation HTML CSS Sections Inline Contact Form Formatting HTML CSS Sections Overview full width section in Poppins font with light gray background using foundation Interactive Images Map Image with Easy Dots and Titles using only CSS HTML Sections Social Header Links easy Drop in Code with Font Awesome Icons Sections Responsive Friendly Subscribe to our Newsletter Section CSS Tips Create a button with CSS, HTML and Center it! CSS Text Formatting Truncate Text to an amount of lines using line-clamp CSS Positions Item Positioning in CSS CSS Tips Draw a Circle in CSS css animations Magical Text Effect Bootstrap 4.2 Kitchen Sink CSS Frameworks Make your site look like windows 7 CSS Backgrounds Fancy up your banner image hero sections using overlay gradients CSS Modal Floating Modal Message Fixed on the Bottom of the Screen CSS Modal Create a modal with only CSS css grid display items in a css grid CSS Text Effects Using the selection selector to change the default highlight color Text Truncate your Text with CSS CSS Cursors Change your cursor into an Emoji Cursor or Image Cursor CSS How to Center Everything or Anything with CSS CSS Animations Smooth Scrolling with just CSS CSS Animations Radial Glow under cursor on box hover over effect CSS Shadows Using a filter drop shadow for transparent images CSS Animations Pure CSS 3D Flipping Book with Animations CSS Create a checkerboard background pattern with CSS CSS Animations button animations hover and fill animation CSS Animations button on focus swing animation CSS Animations Animated Button Border when Active Quick CSS create a border with top triangle in css CSS Animations Single Element CSS Spinners CSS Animations CSS Shake - Shake up your elements CSS Framework NES-style(8bit-like) CSS Framework CSS Tips Why to use a CSS Reset? Web Developer Checklists Front-End Checklist Icons Font Awesome Icons CSS Animation Animate Stuff with Animate.css CSS Animations Check Wave Click a Checkbox and Watch the Animation Animation Cloudy spiral animation with CSS3 Notifications Toast Messages and Notifications Standalone Library no JQuery - notify.js