×

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 Overflow

CSS Overflow: The Overflow property in CSS is used to manage the content if it overflows from its container of the block level. We have seen that all the elements...

3 minutes read.

CSS Background Color

It is one of the CSS properties to apply styling to html elements.This property is used to apply the background color to any HTML element.The background of an element includes...

2 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 Counters

CSS Counters: The CSS counter property is similar to the variables. CSS counters are controlled via CSS, and its values are incremented via rules of CSS to record how much...

4 minutes read.

What is Bootstrap CSS?

Bootstrap: - Bootstrap is a CSS framework that is free to use and is used for the front-end development of the website in a faster and easier way. Bootstrap was released as...

3 minutes read.

CSS Radio Button

Radio Button A radio button has been defined as an element of HTML. It can support us to take any input from the user. But it is challenging to design a...

4 minutes read.

CSS Animation

CSS Animation: The animation property of CSS is applied to make animation over any webpage. It is used as the animation replacement made by JavaScript and Flash. Rules of CSS3 @keyframes The...

3 minutes read.

What is a CSS File?

What is a CSS File and How to use it in Html? CSS: - CSS stands for Cascading Style Sheet. CSS is a file of style sheets that describe the presentation of...

4 minutes read.

CSS Text-Transform Property

Text-Transform in CSS The text-transform property permits us to alter the text case. It controls the capitalization of the text. This property is employed to design the text appearance in every...

3 minutes read.

CSS Opacity

The CSS Opacity property is used to set the clarity of the image. It is defined as degree and specify the transparency of an element. Note: Opacity value will be less...

1 minute read.

CSS Dropdowns

Dropdowns are one of the maximum crucial parts of an interactive internet site. CSS is a kind used to design the drop-down menus. A drop-down essentially is a group of...

5 minutes read.

CSS Box-shadow

CSS Box-shadow This property is referred to include effects as shadow-like around an element’s frame. Syntax box-shadow: h-offset v-offset blur spread color |inset|inherit|none; Property values Following are the various property values which can be used...

3 minutes read.

CSS Zoom

CSS Zoom: Zoom property in CSS extents the content. This property controls the content’s level of magnification. Rather than the use of this CSS property, we may also apply the...

3 minutes read.

Radial-gradient() CSS function

Radial-gradient() CSS function It defines a CSS in-built function, which produces a smooth transition among multiple colors. It is used to set any radial-gradient that is same as a background-image. This...

4 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 Font-Size

A Cascading Style Sheet (CSS) is a simple design scripting language intended to simplify the process of making web pages more visually appealing with the kind of high-level formatting. Cascading...

3 minutes read.

CSS Math Functions

The CSS math functions permit numerical articulations. These CSS math functions can particularly be utilized in maybe unforeseen ways, for example, inside angles and shading the functions and in the...

3 minutes read.

CSS Arrow

CSS Arrow The arrow of CSS is applied to insert an array with the tooltip. It can make a tooltip as any speech bubble. Also, this arrow can be illustrated in...

4 minutes read.

How to change the font in CSS

How to change the font in CSS? Professionals can't deny typefaces — how users organize and style text — if we want to completely customize the website's design. One may wish...

4 minutes read.

Untitled Reusable Block

  ...

1 minute read.