Languages
[Edit]
EN

CSS - text overflow ellipsis in vertical

8 points
Created by:
Isaiah89
721

In this article, we would like to show you how to use text overflow ellipsis in vertical mode in JavaScript.

Quick solution:

:root {
    --lines-count: 3;
    --line-height: 20px;
    --element-height: calc(var(--lines-count) * var(--line-height));
}

div {
    padding: 0;                              /* <------ optional */
    height: var(--element-height);           /* <------ optional */
    text-overflow: ellipsis;                 /* <------ required */
    line-height: var(--line-height);         /* <------ optional */
    overflow: hidden;                        /* <------ required */
    display: -webkit-box;                    /* <------ required */
    -webkit-line-clamp: var(--lines-count);  /* <------ required */
    -webkit-box-orient: vertical;            /* <------ required */
}

Note: the solution presented in the article uses -webkit-* style properties that may be not supported in some or older web browsers (versions released before 2019).

Simple preview:

Overflowing text cut in horizontal using CSS.
Overflowing text cut in horizontal using CSS.

Practical example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    :root {
        --lines-count: 3;
        --line-height: 20px;
        --element-height: calc(var(--lines-count) * var(--line-height));
    }

    div.text {
        padding: 0;                              /* <------ optional */
        border: 1px solid red;
        height: var(--element-height);           /* <------ optional */
        text-overflow: ellipsis;                 /* <------ required */
        line-height: var(--line-height);         /* <------ optional */
        overflow: hidden;                        /* <------ required */
        display: -webkit-box;                    /* <------ required */
        -webkit-line-clamp: var(--lines-count);  /* <------ required */
        -webkit-box-orient: vertical;            /* <------ required */
    }

  </style>
</head>
<body>
  <div class="text">
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </div>
</body>
</html>

 

Legacy version

In this section, you can find solution that may be used when -webkit-box, -webkit-line-clamp or -webkit-box-orient are not supported. It makes last line gradient-transparent giving signal to the user the text is longer than visible area.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    :root {
        --lines-count: 3;
        --line-height: 20px;
        --element-height: calc(var(--lines-count) * var(--line-height));
    }

    div.text {
        padding: 0;                              /* <------ optional */
        position: relative;
        border: 1px solid red;
        height: var(--element-height);           /* <------ optional */
        text-overflow: ellipsis;                 /* <------ required */
        line-height: var(--line-height);         /* <------ optional */
        overflow: hidden;                        /* <------ required */
        display: -webkit-box;                    /* <------ required */
        -webkit-line-clamp: var(--lines-count);  /* <------ required */
        -webkit-box-orient: vertical;            /* <------ required */
    }
    
    div.text::after {
        position: absolute;
        left: 0; bottom: 0; right: 0;
        background: linear-gradient(to top, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0) 120%);
        height: var(--line-height);
        content: ' ';
    }

  </style>
</head>
<body>
  <div class="text">
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </div>
</body>
</html>

 

See also

  1. CSS - cut with dots (...) overflowing text outside element

References

  1. -webkit-line-clamp - MDN Docs

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join