The Wayback Machine - https://web.archive.org/web/20240919012326/https://www.geeksforgeeks.org/how-to-avoid-css-text-overflow-to-ellipsis/
Open In App

How to Avoid CSS text-overflow to Ellipsis?

Last Updated : 24 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The CSS text-overflow: ellipsis property is used to truncate text and display an ellipsis at the end of the text when it overflows its container. However, there may be cases where you want to avoid using this property. Fortunately, there are several approaches you can take to achieve this.

Approach 1: Using a Responsive Layout: One approach to avoiding the use of text-overflow: ellipsis is to use a responsive layout for your web page. This can help ensure that your text never overflows its container in the first place. You can achieve this by using CSS media queries to adjust the layout of your web page based on the size of the screen.

Example: In this example, we will make a responsive layout.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" 
          content="IE=edge">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>
          Document
      </title>
    <style>
        body {
            text-align: center;
        }
        h1 {
            color: green;
        }
        .container {
            width: 100%;
        }
        .text {
            font-size: 2em;
        }
        @media only screen and (max-width: 768px) {
            .text {
                font-size: 1em;
            }
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <b>How to avoid CSS text-overflow: ellipsis?</b>
    <div class="container">
        <p class="text">
              This is a long text that may 
              overflow its container.
          </p>
    </div>
</body>

</html>

Output:

Approach 2: Using Flexbox: Another approach to avoiding the use of text-overflow: ellipsis is to use CSS Flexbox to create a flexible layout. This can help ensure that your text adjusts to fit its container without overflowing. You can achieve this by setting the display property of the container to flex and using the appropriate flexbox properties to position the text within the container.

Example: Using Flexbox:

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" 
          content="IE=edge">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            text-align: center;
        }
        h1 {
            color: green;
        }
        .container {
            display: flex;
            align-items: center;
            justify-content: center;
            border: 2px solid pink;
        }
        .text {
            flex: 1;
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <b>
          How to avoid CSS text-overflow: ellipsis?
      </b>
    <div class="container">
        <p class="text">
              This is a long text that may 
              overflow its container.
          </p>
    </div>
</body>
</html>

Output:

How to avoid CSS text-overflow: ellipsis?

Approach 3: Adjust Font Size: You can also try adjusting the font size of your text to ensure that it fits within its container without overflowing. However, be careful not to make the font size too small, as this can make the text difficult to read.

Example: In this example, we will adjust the font size for avoiding tex-overflow.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" 
          content="IE=edge">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>
          Document
      </title>
    <style>
        body {
            text-align: center;
        }
        h1 {
            color: green;
        }
        .container {
            border: 2px solid red;
            overflow: hidden;
            margin-top: 20px;
        }
        .text {
            font-size: 18px;
            line-height: 1.5;
        }
        @media (max-width: 600px) {
            .text {
                font-size: 25px;
            }
        }
        @media (max-width: 400px) {
            .text {
                font-size: 42px;
            }
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <b>
          How to avoid CSS text-overflow: ellipsis?
      </b>
    <div class="container">
        <p class="text">
              This is a long text that may 
              overflow its container.
          </p>
    </div>
</body>

</html>

Output:

Approach 4: Using a Scrollable Container: If you have a large amount of text that needs to be displayed, you can consider using a scrollable container to display it. This can help ensure that all the text is visible without truncating it. You can achieve this by setting the overflow property of the container to scroll or auto.

Example: In this example, we will create a scrollable container

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <style>
        body {
            text-align: center;
        }

        h1 {
            color: green;
        }

        .container {
            margin: 20px;
            width: 400px;
            height: 200px;
            border: 1px solid #ccc;
            overflow: scroll;
        }

        .container::-webkit-scrollbar {
            width: 10px;
        }

        .container::-webkit-scrollbar-track {
            background: #f1f1f1;
        }

        .container::-webkit-scrollbar-thumb {
            background: #888;
        }

        .text {
            font-size: 18px;
            line-height: 1.5;
            padding: 10px;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <b>How to avoid CSS text-overflow: ellipsis?</b>
    <center>
        <div class="container">
            <p class="text">
                This CSS tutorial, whether you’re a 
                student or a professional, this 
                tutorial is a valuable resource to 
                enhance the visual appeal of your 
                websites or personal blogs. It covers 
                all the basic and advanced concepts 
                of CSS, such as properties, selectors,
                functions, media queries and more. 
                CSS is used to enhance the visual 
                presentation of web pages. Without 
                the use of CSS, a web page will appear 
                visually unappealing. Styling is
                an essential property for any website. 
                It increases the standards and overall 
                look of the website that makes it
                easier for the user to interact with 
                it. A website can be made without CSS, 
                as styling is MUST since no user would 
                want to interact with a dull and shabby 
                website. So for knowing Web Development,
                learning CSS is mandatory.
            </p>
        </div>
    </center>
</body>

</html>

Output:



Similar Reads

What is the difference between overflow: auto and overflow: scroll in CSS ?
In this article, we will understand the difference between the `scroll` &amp; `auto` values for the overflow CSS shorthand property. The Overflow property specifies the preferred behaviors for an element's overflow i.e., when the content of an element is too large to fit within its block formatting context in both directions. In simple words, it co
3 min read
CSS overflow-x: visible and overflow-y: hidden causing scrollbar issue
CSS overflow-x and overflow-y attributes are used to limit content overflow in the horizontal and vertical directions, respectively. The scrollbar may show on the page even when there is nothing to scroll when overflow-x is set to "visible" and overflow-y is set to "hidden". Syntax: For horizontal overflow:overflow-x: visible | hidden | scroll | au
2 min read
How to Apply an Ellipsis to Multiline Text in CSS ?
Ellipsis refers to three dots (...) and is a user-friendly UI modification that indicates more information is available but has been truncated due to lack of space. In this article, we will explore two approaches to applying ellipses to multi-line text in CSS. Understanding Ellipsis in CSSEllipsis helps in providing a visual cue to users that there
3 min read
How to hide text-overflown as ellipsis using dynamic bootstrap cols ?
Various utilities and layouts provided by default in Bootstrap 4 to hide overflown text as ellipsis using dynamic bootstrap cols. Here dynamic bootstrap cols mean columns in row or flex have equal properties even in any numbers. The following approaches will explain it clearly. Approach 1: To hide overflown text as ellipsis using CSS properties.
3 min read
Primer CSS Loader Animated Ellipsis
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approach to CSS is influenced by Object-Oriented CSS principles, functional CSS, an
2 min read
How to use text overflow property in CSS ?
In this article, we will see how to set text overflow property in CSS. The text-overflow property is used to specify that some text has overflown and hidden from view from the user. The text-overflow property only affects content that is overflowing a block container element. Syntax: text-overflow: clip| ellipsis Property Value: clip: Text is clipp
2 min read
Tailwind CSS Text Overflow
This class accepts more than one value in tailwind CSS. All the properties are covered in class form. It is the alternative to the CSS text-overflow property. This class is used to specify that some text has overflown and hidden from view. The whitespace class must be set to nowrap and the overflow class must be hidden. Text Overflow classes: trunc
2 min read
CSS text-overflow Property
The text-overflow property in CSS is your go-to feature when dealing with overflowing and hidden text. It works in tandem with the white-space property set to nowrap and the overflow property set to hidden. The overflowing content can be clipped, display an ellipsis (‘…’), or display a custom string. Syntax: text-overflow: clip|string|ellipsis|init
4 min read
JavaScript Ellipsis
JavaScript Ellipsis (also known as the spread/rest operator) is represented by three dots (...). It is used for various tasks, such as spreading elements of an array into individual values or collecting multiple values into an array or object. It simplifies data manipulation and function parameter handling. We will explore the basic implementation
3 min read
Angular PrimeNG Form MultiSelect Ellipsis Mode Component
Angular PrimeNG is a collection of hundreds of UI components that can be used by developers to speed up the development process of their Applications. It is developed by PrimeTek Informatics also known as PrimeFaces. In this article, we will be discussing Angular PrimeNG Form MultiSelect Ellipsis Mode Component. The MultiSelect Component allows use
4 min read
JavaScript Program to Truncate a String to a Certain Length and Add Ellipsis (...)
In this article, we will see how to truncate a string to a certain length and add an ellipsis (...) in JavaScript. To truncate a string to a specific length and add an ellipsis (...) in JavaScript means shortening the string to a predefined character count, indicating that the content continues beyond the truncated portion. There are several method
3 min read
How to use Ellipsis in react-bootstrap pagination
While developing the React JS application, we came up with the requirement of making the Pagination for accessing more content without going to other tabs or pages. In react-bootstrap, we have the component of Pagination that allows us to go through any page using the sequence of numbers. Along with the Pagination in react-bootstrap, we can use the
5 min read
How to use text-overflow in a table cell ?
In this article, we will see how to use text-overflow in a table cell. A text-overflow property in CSS is used to specify that some text has overflown and hidden from view. Approach: The white-space property must be set to "nowrap" and the overflow property must be set to "hidden". The overflowing content can be clipped, display an ellipsis (‘…’),
2 min read
Bootstrap 5 Text wrapping and overflow
Bootstrap 5 Text wrapping and overflow class is used to specify that the browser may break lines of text inside any targeted element to prevent overflow when the original string is too long to fit. Bootstrap 5 Text wrapping and overflow Classes: text-wrap: This class is used to wrap up the content.text-nowrap: This class is used to not wrap the ele
1 min read
CSS overflow-y Property
The overflow-y property of CSS specifies the behavior of content when it overflows a block-level element's top and bottom edges. The content may be clipped, hidden or a scrollbar may be displayed accordingly based on the value assigned to the overflow-y property. Syntax: overflow-y: scroll | hidden | visible | auto Property values: Scroll: If the v
5 min read
How to set the overflow property to scroll in CSS ?
In this article, we will see how to set the overflow property to scroll in CSS. The overflow property is used to control the big content. It tells what to do when an element's content is too big to fit in the specified area. When the overflow property is set to scroll, the overflow is clipped, but a scrollbar is added to see the rest. Example 1: In
2 min read
CSS | overflow-wrap Property
The overflow-wrap property in CSS is used to specify that the browser may break lines of text inside any targeted element to prevent overflow when the original string is too long to fit. This property was earlier known as word-wrap that still supported by some browsers but it was renamed to overflow-wrap in a CSS3 draft. Syntax: .box{ overflow-wrap
1 min read
Foundation CSS Prototyping Utilities Overflow
Foundation CSS is an open-source &amp; responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to design beautiful responsive websites, apps, and emails that look amazing &amp; can be accessible to any device. It is used by many companies such as Facebook, eBay, Mozilla, Adobe, and even Disney. The framework is
4 min read
Primer CSS Layout Overflow
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approach to CSS is influenced by Object-Oriented CSS principles, functional CSS, an
6 min read
Does overflow: hidden create a new block formatting context in CSS ?
In this article, we will see "Does overflow:hidden create a new block formatting context(BFC)?". The answer is yes, the overflow property with value anything but visible(because its default) will create a new block formatting context. And as we know block formatting context prevents edges from collapsing, prevents content wrapping drifts, and helps
2 min read
How to fix CSS underflow &amp; overflow ?
In this article, we'll see how to manage the overflow issue in CSS. This type of issue generally arises when the content exceeds the container size or when the dimension(i.e. width and height) of the container is fixed. Here, the content can be text or an image and the container can be a &lt;div&gt; tag or another HTML tag that contains the text or
5 min read
Tailwind CSS Overflow
This overflow is for controlling how an element content is handled that is too large for the container. It tells whether to clip content or add scroll bars. This class accepts more than one value in Tailwind CSS. It is the alternative to the CSS Overflow property. There is a separate property in CSS for CSS Overflow-x and CSS Overflow-y,  Overflow
7 min read
CSS overflow Property
The CSS overflow property is used to set the overflow behavior of an element. It is the shorthand property of overflow-x and overflow-y properties. This property is used to control the large content. Syntax: overflow: visible | hidden | clip | scroll | auto;Property Values: visible: The content is not clipped and is visible outside the element box.
2 min read
CSS Overflow
The CSS overflow controls the big content. It tells whether to clip content or to add scroll bars. Try It: .custom-item { border: 1px solid gray; font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif; border-radius: 3px; margin: auto; margin-top: 5px; max-width: 320px; min-width: 180px; width: 80%; cursor: pointer; backgroun
4 min read
CSS overflow-x Property
The overflow-x property in CSS specifies whether to add a scroll bar, clip the content, or display overflow content of a block-level element, when it overflows at the left and right edges. In other words, this property helps us to display the content which is overflowing from the page horizontally. The default value of the overflow-x property is vi
3 min read
HTML | DOM Style overflow Property
The Style overflow property in HTML DOM is used to specify the behavior of the content when it overflows the element box. The content may be hidden, shown or a scrollbar maybe shown according to the value. Syntax: It returns the overflow property.object.style.overflowIt is used to set the overflow property.object.style.overflow = "visible|hidden|sc
6 min read
React Suite Modal Overflow
React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we'll learn about React suite Modal Overflow. This props automatically sets the height when the bo
5 min read
React Suite CheckPicker Container and prevent overflow
React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we’ll learn about React suite Checkpicker Container and prevent overflow. Checkpicker is very usef
5 min read
React Suite Tooltip Container and prevent overflow
React Suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we'll learn about React Suite Tooltip Container and prevent overflow. The tooltip component allows
3 min read
React Suite Popover Container and prevent overflow
React Suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we'll learn about React Suite Popover Container and prevent overflow. The Popover Component is use
3 min read