VDone Demo VDone Demo
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)

Nikolay Tuzov

Backend Developer
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)
  • HTML

  • CSS

    • CSS Tutorials and Tips Collection
    • Flex Layout Syntax
    • Flex Layout Example - Basics
    • Flex Layout Example - Dice
    • Flex Layout Example - Holy Grail Layout
    • Flex Layout Example - Grid Layout
    • Flex Layout Example - Input Layout
    • CSS3 Transition
    • CSS3 Animation
    • Layout Tip - Auto-Expand Element Height Before Images Load
    • Text Overflow Ellipsis for Single and Multiple Lines
      • Single-Line Overflow Ellipsis
      • Two-Line (Multi-Line) Overflow Ellipsis
      • JS: Detect Whether an Ellipsis Is Shown
        • Knowledge Extension
    • Understanding the Box Model Through the box-sizing Property
    • Horizontal and Vertical Centering Methods - Examples
    • How to Automatically Respond to CSS Dark Mode Based on System Theme
    • CSS Trick - Custom Hover Tooltips with :hover and attr()
    • CSS Functions Summary
    • Adding a Scrollbar to a Table's tbody with CSS
    • Animations in Grid Layout
  • 页面
  • CSS
xugaoyi
2020-02-23
Contents

Text Overflow Ellipsis for Single and Multiple Lines

# Text Overflow Ellipsis for Single and Multiple Lines

# Single-Line Overflow Ellipsis

overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
1
2
3
<html>
   <div class="box-42b6">演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字</div>
</html>
<style>
    .box-42b6{
        border: 1px solid #999;
        width: 200px;

        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13

# Two-Line (Multi-Line) Overflow Ellipsis

overflow: hidden;
white-space: normal;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
1
2
3
4
5
6

The number of lines displayed is determined by the line-clamp style value.

<html>
   <div class="box2-42b6">演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字演示文字</div>
</html>
<style>
    .box2-42b6{
        border: 1px solid #999;
        width: 200px;

        overflow: hidden;
        white-space: normal;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;

      }
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# JS: Detect Whether an Ellipsis Is Shown

Sometimes we need to know whether the text has overflowed and an ellipsis is shown. This can be done using clientHeight and scrollHeight:

let cHeight = noWrapDiv.clientHeight;
let sHeight = noWrapDiv.scrollHeight;
if (sHeight > cHeight) {
      console.log("Overflow detected, ellipsis shown");
} else {
      console.log("No overflow");
}
1
2
3
4
5
6
7

This can be used to decide whether to show an expand/collapse button.

# Knowledge Extension

scrollHeight: The height of the element's content, including content not visible due to overflow. Does not include scrollbars, borders, or margins.

clientHeight: The visible height of the element's content, including padding but excluding horizontal scrollbars, borders, and margins.

offsetHeight: The pixel height of the element, including vertical padding and borders, as an integer.

Edit (opens new window)
Last Updated: 2026/03/21, 12:14:36
Layout Tip - Auto-Expand Element Height Before Images Load
Understanding the Box Model Through the box-sizing Property

← Layout Tip - Auto-Expand Element Height Before Images Load Understanding the Box Model Through the box-sizing Property→

Recent Updates
01
How I Discovered Disposable Email — A True Story
06-12
02
Animations in Grid Layout
09-15
03
Renaming a Git Branch
08-11
More Articles >
Theme by VDone | Copyright © 2026-2026 Nikolay Tuzov | MIT License | Telegram
  • Auto
  • Light Mode
  • Dark Mode
  • Reading Mode