×

CSS Text-Overflow

Introduction:

The text-overflow property of CSS is used to format the text which is overflowing the boundaries of the container. This property helps user to clip the text, to display the dots indicating that the text is incomplete (ellipsis) or to show the custom string.

It should be noted that text-overflow can be used only when the overflow property of a container has the value hidden, scroll or auto and white-space: nowrap;

This property is used when the text strings are too long, do not have spaces and are exist within the container which is not as wide as necessary.

<example>

Property Values:

text-overflow may property can have one or two values. If there is one value, it specifies overflow behaviour for the end of the line, and if there are two values, first value depicts the overflow for left end and second value depicts overflow for the right end.

The following values are values used with the text-overflow property:

  • clip
  • ellipsis
  • string
  • fade
  • fade( <length> | <percentage>)
  • initial
  • inherit
  • clip
    clip is a default value of text-overflow. It truncates the text to the limit of the content area allowing the truncation to occur in the middle of word or character.
    Once the text is clipped, it is not accessible by the user.
    Syntax:
    text-overflow: clip;

Example:

<!DOCTYPE html>
<html>
<head>
<style>
div{
border: 2px solid;  
font-size: 25px;    
height: 50px; 
width: 300px; 
/*following are required for text-overflow */
white-space: nowrap;
overflow: hidden;
} 
.id{
text-overflow: clip;
}
h2{
color: tomato;
}
div:hover {
overflow: visible;  
} 
</style>
</head>
<body>
<h2>text-overflow: clip; </h2>
<div class="id">
This is an example of CSS text-overflow. The example display the clip value of this property
</div>
</body>
</html>

Output:

css-text-overflow

Here the text is clipped within the container specified. Once you hover over the box, it will display the whole text as shown below.

css-text-overflow
  • ellipsis
    If the text is overflowing and you want to display an ellipsis ('…') in front of it, ellipsis value can be used. This property is used when the text is clipped and you want the user to know that there is further text which is clipped.
    Syntax:
    text-overflow: ellipsis;

Example:

<!DOCTYPE html>
<html>
<head>
<style>
div{
border: 2px solid; 
font-size: 25px;
height: 50px; 
width: 300px; 
/*following are required for text-overflow *
white-space: nowrap;
overflow: hidden; 
} 
.id{
text-overflow: ellipsis;
}
h2{	
color: tomato;
}
div:hover {
overflow: visible; 
}
</style>
</head>
<body>
<h2>text-overflow: ellipsis; </h2>
<div class="id">
This is an example of CSS text-overflow. The example display the ellipsis value of this property
</div>
</body>
</html>

Output:

css-text-overflow

Here, the overflowing text is clipped and an ellipsis ('…') is displayed. Once you hover on the text, complete paragraph is displayed as shown below.

css-text-overflow
  • string
    To clip the text and display a custom string, string type can be used. This string is visible inside the container. If the custom string itself is overflowing the container, the string is clipped
    Syntax:
    text-overflow: string;

Example:

<!DOCTYPE html>
<html>
<head>
<style>
div{
border: 2px solid; 
font-size: 25px;
height: 50px;width: 300px; 
/*following are required for text-overflow */
white-space: nowrap;
overflow: hidden; 
} 
.id{
text-overflow: "Clipped text"; 
} 
h2{
color: tomato;
}
div:hover {
overflow: visible;
} 
</style>
</head>
<body>
<h2>text-overflow: string; </h2>
<div class="id">
This is an example of CSS text-overflow. The example display the string value of this property
</div>
</body>
</html>

Output:

css-text-overflow

Here "Clipped text" is the custom string used to depict that the overflown text is clipped. When you hover over the text, it displays the complete text as shown below.

css-text-overflow
  • fade:
    This value clips the inline text which is overflowing and displays a fade-out effect near the border of the container.
    Even though it may seem amazing, this effect is not supported by any known web browser.
  • fade( length | percentage):
    This function clips the inline text which is overflowing and displays a fade-out effect near the border of the container.
    The parameters <length> or <percentage> determines the distance over which the effect will be applied.
    The fade() is not supported by any of the web browsers.
  • initial:
    The initial keyword assigns default value to the text-overflow property.
    Syntax:
    text-overflow: initial;

Example:

<!DOCTYPE html>
<html>
<head>
<style>
div{
width: 400px;
font-size: 25px;
white-space: nowrap; 
overflow: hidden;
text-overflow: initial;
}
div:hover {
overflow: visible; 
} 
h2{
color: skyblue;
}
</style>
</head>
<body>
<h2>text-overflow: initial; </h2>
<div class="id">
This is an example of CSS text-overflow. The example display the initial value of this property
</div>
</body>
</html>

Output:

css-text-overflow

Here the initial value sets the property to its default value. You can view the complete text by hovering over it.

css-text-overflow
  • inherit:
    This value inherits the text-overflow property of its parent element.
    Syntax:
    text-overflow: inherit;

Example:

<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 400px;
font-size: 25px;
border: 2px solid;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
h2{
width: 400px;
white-space: nowrap;
overflow: hidden;
text-overflow: inherit;
color: skyblue;
}
div:hover {  
overflow: visible; 
} 
h2:hover{
overflow: visible;
} 
</style>
</head>
<body>
<div>
<h2>text-overflow: inherit derives the property of its parent element </h2>  This is an example of CSS text-overflow. The example display the initial value of this property.
</div>
</body>
</html>

Output:

css-text-overflow

Here the <h2> tag inherits the ellipsis property of its parent element <div> tag. When you hover the mouse cursor on the text, it displays the complete text as shown below.

css-text-overflow

Thus, we have studied the CSS text-overflow property CSS and its various values used to manage the overflown text.


Related Topics

CSS Gradient

CSS Gradient: The CSS gradient property is applied to show the smooth transition inside two specified colors or more than two specified colors. Need of CSS Gradient The following are a few...

4 minutes read.

Difference between HTML and CSS

HTML HTML stands for HyperText Markup Language and is used to create web pages and websites and web applications, which means we can create static web pages. Html is a scripting...

3 minutes read.

How to include CSS

How to include CSS CSS is included in HTML pages for formatting the documents based on the information inside a style sheet. We can add CSS in HTML documents in the following three ways: Inline...

5 minutes read.

CSS Pseudo-class

Pseudo-class The pseudo-class may be depicted as the keyword which can be mixed to any selector that specifies the elected element’s unique state. It can be included to a selector to...

6 minutes read.

CSS Page-break-after Attribute

Page-break-after Attribute As its name implies, the page-break-before property is applied to include page break after any element, at the time of printing a document. It adds the page break after...

5 minutes read.

CSS Browser Support

The desk underneath lists of all CSS properties and the way each belongs to it is supported in the generally unique browsers: - The quantity to the right of the browser...

2 minutes read.

CSS Text-stroke

Text-stroke Text-stroke CSS property inserts the stroke to any text and gives a decoration option to them also. It describes the stroke’s width and color for various text characters. It is...

2 minutes read.

CSS Page-break-before Attribute

Page-break-before Attribute As its name implies, the page-break-before property is applied to include page break before any element, at the time of printing any document. It adds the page break before...

5 minutes read.

CSS Color

CSS Colors: The CSS color property is used for setting the HTML element’s color. The color of the fonts and the color of the background can be set-up by using...

5 minutes read.

How to center a table in CSS

The table is used to store and arrange the data in tabular format. The data here can be of any form like text, image, links, etc. The table is made...

3 minutes read.

CSS translate() Function

Translate() CSS function This function in CSS is a CSS in-built function. It relocates the element in the horizontal direction or in the vertical direction. It can move the items from...

2 minutes read.

CSS Inline

What is CSS? CSS is an acronym for Cascading Style Sheets. CSS is the language that we use to style an HTML document. It specifies how HTML components should appear. CSS...

4 minutes read.

CSS Filter

CSS Filter CSS filter is used to apply visual effects on the images, text, and other factors of the webpage. This filter property in CSS allows us to admit the effects like...

7 minutes read.

How to get rid/remove of bullet pioints in CSS

We include a list of items on the web pages that were sometimes numbered and sometimes bulleted in various styles. In HTML, there are two kinds of lists: ordered and unordered...

3 minutes read.

CSS Lists

CSS Lists: Several properties are available in CSS that are used to manage the lists. Lists are distributed as ordered and unordered kind of lists. The ordered lists provide the...

7 minutes read.

CSS Sticky

CSS Sticky The position property in CSS is applied to set an element’s position. This property is also applied to position an element behind the other element. It is useful for...

1 minute read.

CSS Comments

CSS Comments: Generally, comments in CSS are used to describe our code. Further, it will provide support to many users who are concerned with the code. Somewhat, these are ignored...

2 minutes read.

CSS Specificity

CSS Specificity: If one or more conflicting rules of CSS express a similar element, a browser will pursue a few rules to examine the specific one. Specificity can be specified...

3 minutes read.

CSS Float

CSS Float: The float property of CSS is the positioning property. This property is applied to elements to push them either to left or right. It permits for wrapping facility...

4 minutes read.

CSS Important

CSS Important: CSS !important property is used to provide more importance, as compared to other CSS properties. The meaning of this property is “This is important.” This property enables CSS...

2 minutes read.