tailwindcss-line-clamp
A plugin that provides utilities for visually truncating text after a fixed number of lines.
Top Related Projects
A rugged, minimal framework for composing JavaScript behavior in your markup.
This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
The library for web and native user interfaces.
Cybernetically enhanced web apps
⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.
Lit is a simple library for building fast, lightweight web components.
Quick Overview
Tailwind CSS Line Clamp is an official plugin for Tailwind CSS that provides utilities for visually truncating text after a specific number of lines. It allows developers to easily implement multi-line text truncation with ellipsis, enhancing the readability and aesthetics of text-heavy interfaces.
Pros
- Easy integration with existing Tailwind CSS projects
- Provides a simple, declarative way to truncate text across multiple lines
- Responsive design support, allowing different truncation settings for various screen sizes
- Customizable number of lines for truncation
Cons
- Limited to Tailwind CSS ecosystem, not usable as a standalone solution
- May not work perfectly with all font families or complex text layouts
- Requires JavaScript for full browser compatibility, which might impact performance slightly
- Limited control over the ellipsis styling compared to custom CSS solutions
Code Examples
- Basic usage:
<p class="line-clamp-3">
This is a long paragraph that will be truncated after three lines. Any text beyond the third line will be hidden and replaced with an ellipsis.
</p>
- Responsive truncation:
<p class="line-clamp-2 md:line-clamp-3 lg:line-clamp-4">
This paragraph will be truncated after 2 lines on small screens, 3 lines on medium screens, and 4 lines on large screens.
</p>
- Removing truncation:
<p class="line-clamp-3 hover:line-clamp-none">
This text is truncated to 3 lines by default, but will expand to show all content when hovered over.
</p>
Getting Started
- Install the plugin:
npm install @tailwindcss/line-clamp
- Add the plugin to your
tailwind.config.js
file:
module.exports = {
// ...
plugins: [
require('@tailwindcss/line-clamp'),
// ...
],
}
- Use the utility classes in your HTML:
<p class="line-clamp-2">Your multi-line text here...</p>
Competitor Comparisons
A rugged, minimal framework for composing JavaScript behavior in your markup.
Pros of Alpine
- More versatile: Alpine is a lightweight JavaScript framework for building interactive web applications, while tailwindcss-line-clamp is a specific utility for text truncation
- Broader functionality: Alpine offers a wide range of features for DOM manipulation, event handling, and state management
- Standalone usage: Can be used independently of any CSS framework
Cons of Alpine
- Steeper learning curve: Requires knowledge of JavaScript and Alpine-specific directives
- Potentially larger file size: Alpine includes more functionality, which may result in a larger overall file size compared to the focused tailwindcss-line-clamp utility
Code Comparison
Alpine:
<div x-data="{ expanded: false }">
<p x-show="!expanded" class="line-clamp-3">Long text content...</p>
<button @click="expanded = !expanded">Toggle</button>
</div>
tailwindcss-line-clamp:
<p class="line-clamp-3">Long text content...</p>
While Alpine provides more flexibility for interactive clamp functionality, tailwindcss-line-clamp offers a simpler, CSS-only solution for basic text truncation.
This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
Pros of Vue
- Full-featured JavaScript framework for building user interfaces
- Offers a complete ecosystem with routing, state management, and tooling
- Suitable for large-scale applications and complex web projects
Cons of Vue
- Steeper learning curve compared to simple utility libraries
- Requires more setup and configuration for basic projects
- Larger bundle size, which may impact performance for smaller applications
Code Comparison
Vue:
<template>
<div v-for="item in items" :key="item.id">
{{ item.text }}
</div>
</template>
<script>
export default {
data() {
return {
items: [/* ... */]
}
}
}
</script>
Tailwind CSS Line Clamp:
<p class="line-clamp-3">
<!-- Content to be clamped -->
</p>
Summary
Vue is a comprehensive framework for building web applications, offering a wide range of features and capabilities. Tailwind CSS Line Clamp, on the other hand, is a focused utility for truncating multi-line text. While Vue provides a complete solution for complex projects, Tailwind CSS Line Clamp offers a simple, specific functionality that can be easily integrated into existing projects without the overhead of a full framework.
The library for web and native user interfaces.
Pros of React
- Comprehensive JavaScript library for building user interfaces
- Large ecosystem with extensive community support and third-party libraries
- Virtual DOM for efficient rendering and performance optimization
Cons of React
- Steeper learning curve compared to simple CSS utilities
- Requires additional setup and configuration for a full development environment
- Overkill for projects that only need basic UI styling
Code Comparison
React component:
function LineClamp({ children, lines }) {
return (
<div style={{
display: '-webkit-box',
WebkitLineClamp: lines,
WebkitBoxOrient: 'vertical',
overflow: 'hidden'
}}>
{children}
</div>
);
}
Tailwind CSS line-clamp:
<p class="line-clamp-3">
<!-- Content here -->
</p>
The React example requires more code and setup, but offers more flexibility and programmatic control. The Tailwind CSS approach is simpler and more declarative, ideal for quick styling tasks.
React is a full-featured library for building complex UIs, while tailwindcss-line-clamp is a focused utility for a specific styling need. Choose based on your project requirements and complexity.
Cybernetically enhanced web apps
Pros of Svelte
- Full-featured framework for building web applications
- Offers a complete solution for reactive UI development
- Compiles to highly efficient JavaScript
Cons of Svelte
- Steeper learning curve due to its unique syntax and concepts
- Smaller ecosystem compared to more established frameworks
Code Comparison
Svelte:
<script>
let count = 0;
function increment() {
count += 1;
}
</script>
<button on:click={increment}>
Clicks: {count}
</button>
Tailwindcss-line-clamp:
<p class="line-clamp-3">
This text will be truncated after three lines.
</p>
Summary
Svelte is a comprehensive framework for building web applications, offering a complete solution for reactive UI development. It compiles to highly efficient JavaScript, resulting in smaller bundle sizes. However, it has a steeper learning curve due to its unique syntax and concepts, and a smaller ecosystem compared to more established frameworks.
Tailwindcss-line-clamp, on the other hand, is a utility plugin for Tailwind CSS that provides a simple way to truncate text after a specific number of lines. It's focused on a single task and integrates seamlessly with Tailwind CSS projects.
While Svelte offers more functionality and flexibility for building entire applications, Tailwindcss-line-clamp is a specialized tool for handling text truncation in Tailwind CSS projects.
⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.
Pros of Preact
- Lightweight alternative to React with a smaller bundle size
- Compatible with most React libraries and components
- Faster performance due to its smaller codebase and optimizations
Cons of Preact
- Smaller ecosystem compared to React
- May lack some advanced features or experimental APIs found in React
- Potential compatibility issues with certain React libraries
Code Comparison
Preact:
import { h, render } from 'preact';
const App = () => <h1>Hello, World!</h1>;
render(<App />, document.body);
Tailwind CSS Line Clamp:
<p class="line-clamp-3">
This text will be truncated after three lines...
</p>
Note: The code comparison is not directly relevant as these projects serve different purposes. Preact is a JavaScript library for building user interfaces, while Tailwind CSS Line Clamp is a utility for truncating text in CSS.
Lit is a simple library for building fast, lightweight web components.
Pros of lit
- Full-featured web component library for building complex applications
- Offers reactive properties, templating, and lifecycle management
- Supports TypeScript out of the box
Cons of lit
- Steeper learning curve compared to simple CSS utilities
- Requires more setup and configuration for projects
- May be overkill for simple styling tasks
Code Comparison
tailwindcss-line-clamp:
.line-clamp-3 {
overflow: hidden;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
}
lit:
import { LitElement, html, css } from 'lit';
class MyElement extends LitElement {
static styles = css`
.content { overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; }
`;
render() {
return html`<div class="content"><slot></slot></div>`;
}
}
Summary
tailwindcss-line-clamp is a simple CSS utility for truncating text, while lit is a comprehensive web component library. tailwindcss-line-clamp is easier to use for basic styling tasks, but lit offers more power and flexibility for building complex, interactive web applications. The choice between them depends on the project's requirements and complexity.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
@tailwindcss/line-clamp
A plugin that provides utilities for visually truncating text after a fixed number of lines.
Warning As of Tailwind CSS v3.3 the
line-clamp
utilities are now included in the framework by default and this plugin is no longer required.
Installation
Install the plugin from npm:
npm install -D @tailwindcss/line-clamp
Then add the plugin to your tailwind.config.js
file:
// tailwind.config.js
module.exports = {
theme: {
// ...
},
plugins: [
require('@tailwindcss/line-clamp'),
// ...
],
}
Usage
Use the line-clamp-{n}
utilities to specify how many lines of text should be visible before truncating:
<p class="line-clamp-3">
Et molestiae hic earum repellat aliquid est doloribus delectus. Enim illum odio porro ut omnis dolor debitis natus. Voluptas possimus deserunt sit delectus est saepe nihil. Qui voluptate possimus et quia. Eligendi voluptas voluptas dolor cum. Rerum est quos quos id ut molestiae fugit.
</p>
To remove any line-clamping, use line-clamp-none
:
<p class="line-clamp-3 md:line-clamp-none">
Et molestiae hic earum repellat aliquid est doloribus delectus. Enim illum odio porro ut omnis dolor debitis natus. Voluptas possimus deserunt sit delectus est saepe nihil. Qui voluptate possimus et quia. Eligendi voluptas voluptas dolor cum. Rerum est quos quos id ut molestiae fugit.
</p>
Note that the
line-clamp-{n}
set other properties likedisplay
andoverflow
in addition to-webkit-line-clamp
which are not unset byline-clamp-none
, so depending on what you are trying to achieve you may need to explicitly override those properties with utilities likeflex
oroverflow-visible
as well.
Utilities are for clamping text up to 6 lines are generated by default:
Class | CSS |
---|---|
line-clamp-1 | overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 1; |
line-clamp-2 | overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; |
line-clamp-3 | overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; |
line-clamp-4 | overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 4; |
line-clamp-5 | overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 5; |
line-clamp-6 | overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 6; |
line-clamp-none | -webkit-line-clamp: unset; |
Configuration
You can configure which values and variants are generated by this plugin under the lineClamp
key in your tailwind.config.js
file:
// tailwind.config.js
module.exports = {
theme: {
extend: {
lineClamp: {
7: '7',
8: '8',
9: '9',
10: '10',
}
}
},
variants: {
lineClamp: ['responsive', 'hover']
}
}
Top Related Projects
A rugged, minimal framework for composing JavaScript behavior in your markup.
This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
The library for web and native user interfaces.
Cybernetically enhanced web apps
⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.
Lit is a simple library for building fast, lightweight web components.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot