The text-overflow
CSS property sets how hidden overflow content is signaled to users. It can be clipped, display an ellipsis (‘…
‘), or display a custom string.
Try it
The text-overflow
property doesn’t force an overflow to occur. To make text overflow its container, you have to set other CSS properties: overflow
and white-space
. For example:
overflow: hidden;
white-space: nowrap;
The text-overflow
property only affects content that is overflowing a block container element in its inline progression direction (not text overflowing at the bottom of a box, for example).
Syntax
The text-overflow
property may be specified using one or two values. If one value is given, it specifies overflow behavior for the end of the line (the right end for left-to-right text, the left end for right-to-left text). If two values are given, the first specifies overflow behavior for the left end of the line, and the second specifies it for the right end of the line.
text-overflow: clip;
text-overflow: ellipsis ellipsis;
text-overflow: ellipsis " [..]";
/* Global values */
text-overflow: inherit;
text-overflow: initial;
text-overflow: revert;
text-overflow: revert-layer;
text-overflow: unset;
- one of the keyword values:
clip
,ellipsis
,fade
- the function
fade()
, which is passed a<length>
or a<percentage>
to control the fade distance - a
<string>
.
Values
clip
-
The default for this property. This keyword value will truncate the text at the limit of the content area, therefore the truncation can happen in the middle of a character. To clip at the transition between characters you can specify
text-overflow
as an empty string, if that is supported in your target browsers:text-overflow: '';
. ellipsis
-
This keyword value will display an ellipsis (
'…'
,U+2026 HORIZONTAL ELLIPSIS
) to represent clipped text. The ellipsis is displayed inside the content area, decreasing the amount of text displayed. If there is not enough space to display the ellipsis, it is clipped. <string>
Experimental
-
The
<string>
to be used to represent clipped text. The string is displayed inside the content area, shortening the size of the displayed text. If there is not enough space to display the string itself, it is clipped. fade
Experimental
-
This keyword clips the overflowing inline content and applies a fade-out effect near the edge of the line box with complete transparency at the edge.
fade( <length> | <percentage> )
Experimental
-
This function clips the overflowing inline content and applies a fade-out effect near the edge of the line box with complete transparency at the edge.
The argument determines the distance over which the fade effect is applied. The
<percentage>
is resolved against the width of the line box. Values lower than0
are clipped to0
. Values greater than the width of the line box are clipped to the width of the line box.
Formal definition
Initial value | clip |
---|---|
Applies to | block container elements |
Inherited | no |
Computed value | as specified |
Animation type | discrete |
Formal syntax
text-overflow =
clip |
ellipsis
Examples
One-value syntax
This example shows different values for text-overflow
applied to a paragraph, for left-to-right and right-to-left text.
HTML
<div class="ltr">
<h2>Left to right text</h2>
<pre>clip</pre>
<p class="overflow-clip">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<pre>ellipsis</pre>
<p class="overflow-ellipsis">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<pre>" [..]"</pre>
<p class="overflow-string">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
</div>
<div class="rtl">
<h2>Right to left text</h2>
<pre>clip</pre>
<p class="overflow-clip">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<pre>ellipsis</pre>
<p class="overflow-ellipsis">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<pre>" [..]"</pre>
<p class="overflow-string">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
</div>
CSS
p {
width: 200px;
border: 1px solid;
padding: 2px 5px;
/* Both of the following are required for text-overflow */
white-space: nowrap;
overflow: hidden;
}
.overflow-clip {
text-overflow: clip;
}
.overflow-ellipsis {
text-overflow: ellipsis;
}
.overflow-string {
text-overflow: " [..]";
}
body {
display: flex;
justify-content: space-around;
}
.ltr > p {
direction: ltr;
}
.rtl > p {
direction: rtl;
}
Result
Two-value syntax
This example shows the two-value syntax for text-overflow
, where you can define different overflow behavior for the start and end of the text.
To show the effect we have to scroll the line so the start of the line is also hidden.
HTML
<pre>clip clip</pre>
<p class="overflow-clip-clip">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<pre>clip ellipsis</pre>
<p class="overflow-clip-ellipsis">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<pre>ellipsis ellipsis</pre>
<p class="overflow-ellipsis-ellipsis">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<pre>ellipsis " [..]"</pre>
<p class="overflow-ellipsis-string">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
CSS
p {
width: 200px;
border: 1px solid;
padding: 2px 5px;
/* Both of the following are required for text-overflow */
white-space: nowrap;
overflow: scroll;
}
.overflow-clip-clip {
text-overflow: clip clip;
}
.overflow-clip-ellipsis {
text-overflow: clip ellipsis;
}
.overflow-ellipsis-ellipsis {
text-overflow: ellipsis ellipsis;
}
.overflow-ellipsis-string {
text-overflow: ellipsis " [..]";
}
JavaScript
// Scroll each paragraph so the start is also hidden
const paras = document.querySelectorAll("p");
for (const para of paras) {
para.scroll(100, 0);
}
Result
Specifications
Specification |
---|
CSS Overflow Module Level 3 # text-overflow |
A previous version of this interface reached the Candidate Recommendation status. As some not-listed-at-risk features needed to be removed, the spec was demoted to the Working Draft level, explaining why browsers implemented this property unprefixed, though not at the CR state.
Browser compatibility
BCD tables only load in the browser
See also
- Related CSS properties:
overflow
,white-space
- CSS properties that control line breaks in words:
overflow-wrap
,word-break
How can I prevent text in a div block from overflowing in CSS?
div {
width: 150px;
/* what to put here? */
}
<div>This div contains a VeryLongWordWhichDoesNotFitToTheBorder.</div>
j08691
203k31 gold badges259 silver badges271 bronze badges
asked Jul 22, 2009 at 14:02
2
If you want the overflowing text in the div to automatically newline instead of being hidden or making a scrollbar, use the
word-wrap: break-word
property.
answered Sep 24, 2010 at 21:16
AlkanshelAlkanshel
4,1231 gold badge34 silver badges52 bronze badges
4
You can try:
<div id="myDiv">
stuff
</div>
#myDiv {
overflow:hidden;
}
Check out the docs for the overflow property for more information.
answered Jul 22, 2009 at 14:04
brettkellybrettkelly
27.5k8 gold badges56 silver badges71 bronze badges
1
Short Answer
Simply use:
word-wrap: break-word;
white-space: pre-wrap;
word-break: break-word;
Long Answer
1. word-wrap
Allows long words to be able to break and wrap onto the next line.
Possible values:
normal
: Break words only at allowed break pointsbreak-word
: Allows unbreakable words to be broken
div {
width: 150px;
border: 2px solid #ff0000;
}
div.normal {
word-wrap: normal;
}
div.break {
word-wrap: break-word;
}
<h2>word-wrap: normal</h2>
<div class="normal"> This div contains a VeryLongWordWhichDoesNotFitToTheBorder.</div>
<h2>word-wrap: break-word</h2>
<div class="break"> This div contains a VeryLongWordWhichDoesNotFitToTheBorder.</div>
2. white-space
Specifies how white-space inside an element is handled.
Possible values:
nowrap
: Text will never wrap to the next line.pre-wrap
: Whitespace is preserved by the browser. Text will wrap when necessary, and on line breaks
div {
width: 150px;
border: 2px solid #ff0000;
}
div.nowrap {
white-space: nowrap;
}
div.pre-wrap {
white-space: pre-wrap;
}
<h2>white-space: nowrap</h2>
<div class="nowrap">This div contains a very long but normal paragraph with so many words and nothing else.</div>
<h2>white-space: pre-wrap</h2>
<div class="pre-wrap">This div contains a very long but normal paragraph with so many words and nothing else.</div>
3. word-break
Specifies how words should break when reaching the end of a line.
Possible values:
normal
: default line break rules.break-word
: To prevent overflow, word may be broken at arbitrary points.
div {
width: 150px;
border: 2px solid #ff0000;
}
div.normal {
word-break: normal;
}
div.break-word {
word-break: break-word;
}
<h2>word-break: normal (default)</h2>
<div class="normal"> This div contains a VeryLongWordWhichDoesNotFitToTheBorder.</div>
<h2>word-break: break-word</h2>
<div class="break-word"> This div contains a VeryLongWordWhichDoesNotFitToTheBorder.</div>
For browser-specific versions of white-space
, use:
white-space: pre-wrap; /* CSS3 */
white-space: -moz-pre-wrap; /* Firefox */
white-space: -pre-wrap; /* Opera <7 */
white-space: -o-pre-wrap; /* Opera 7 */
evandrix
6,0014 gold badges27 silver badges37 bronze badges
answered May 24, 2014 at 7:32
2
You can use the CSS property text-overflow to truncate long texts.
#greetings
{
width: 100px;
border: 1px red solid;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis; // This is where the magic happens
}
<div id="greetings">
Hello universe!
</div>
<div id="greetings">
Hello universe! 12345678901234567890
</div>
reference:
http://makandracards.com/makandra/5883-use-css-text-overflow-to-truncate-long-texts
Sachin
2,1541 gold badge21 silver badges43 bronze badges
answered Dec 18, 2013 at 10:57
0
You can control it with CSS, there is a few options :
- hidden -> All text overflowing will be hidden.
- visible -> Let the text overflowing visible.
- scroll -> put scroll bars if the text overflows
Hope it helps.
answered Jul 22, 2009 at 14:08
0
there is another css property :
white-space : normal;
The white-space property controls how text is handled on the element it is applied to.
div {
/* This is the default, you don't need to
explicitly declare it unless overriding
another declaration */
white-space: normal;
}
palaѕн
71.4k17 gold badges116 silver badges133 bronze badges
answered Dec 24, 2012 at 7:26
SachinSachin
2,1541 gold badge21 silver badges43 bronze badges
1
Try adding this class in order to fix the issue:
.ellipsis {
text-overflow: ellipsis;
/* Required for text-overflow to do anything */
white-space: nowrap;
overflow: hidden;
}
Explained further in this link http://css-tricks.com/almanac/properties/t/text-overflow/
answered Jul 8, 2014 at 4:48
0
overflow: scroll
? Or auto.
in the style attribute.
answered Jul 22, 2009 at 14:04
beanbean
1,0811 gold badge12 silver badges23 bronze badges
It’s now the css property:
word-break: break-all
answered Oct 16, 2020 at 11:25
CloudCloud
2474 silver badges10 bronze badges
0
.NonOverflow {
width: 200px; /* Need the width for this to work */
overflow: hidden;
}
<div class="NonOverflow">
Long Text
</div>
TylerH
20.6k64 gold badges76 silver badges97 bronze badges
answered Jul 22, 2009 at 14:05
KirtanKirtan
21.1k6 gold badges46 silver badges61 bronze badges
1
If your div has a set height in css that will cause it to overflow outside of the div.
You could give the div a min-height if you need to have it be a minimum height on the div at all times.
Min-height will not work in IE6 though, if you have an IE6 specific stylesheet you can give it a regular height for IE6. Height changes in IE6 according to the content within.
answered Jul 22, 2009 at 15:32
You can just set the min-width in the css, for example:
.someClass{min-width: 980px;}
It will not break, nevertheless you will still have the scroll-bar to deal with.
answered Oct 4, 2016 at 15:41
3xCh1_233xCh1_23
1,4651 gold badge20 silver badges39 bronze badges
Recommendations for how to catch which part of your CSS is causing the issue:
1) set style="height:auto;"
on all the <div>
elements and retry again.
2) Set style="border: 3px solid red;"
on all the <div>
elements to see how wide of an area your <div>
‘s box is taking up.
3) Take away all the css height:#px;
properties from your CSS and start over.
So for example:
<div id="I" style="height:auto;border: 3px solid red;">I
<div id="A" style="height:auto;border: 3px solid purple;">A
<div id="1A" style="height:auto;border: 3px solid green;">1A
</div>
<div id="2A" style="height:auto;border: 3px solid green;">2A
</div>
</div>
<div id="B" style="height:auto;border: 3px solid purple;">B
<div id="1B" style="height:auto;border: 3px solid green;">1B
</div>
<div id="2B" style="height:auto;border: 3px solid green;">2B
</div>
</div>
</div>
answered Dec 1, 2017 at 0:15
GeneGene
10.7k1 gold badge66 silver badges57 bronze badges
!! Hard coded tradoff ahead !! Depending on the surrounding code and the screen resolution this could lead to different / unwanted behaviour
If hidden overflow is out of the question and correct hyphenation is needed you could use the soft hyphen HTML entity where you want the word / text to break. This way you are able to predetermine a breaking point manually.
­
The hyphen will only appear when the word needs to break to not overflow its surrounding container.
Example:
<div class="container">
foo­bar
</div>
Result if the container is wide enough and the text would not overflow the container:
foobar
Result if the container is to small and the text would actually overflow the container:
foo-
bar
.container{
background-color: green;
max-width: 30px;
}
Example 1 - container to small => text overflows container:
<div class="container">
foobar
</div>
Example 2 - using soft hyphen => text breaks at predetermined break point:
<div class="container">
foo­bar
</div>
Example 3 - using soft hyphen => text still overflowing because the text before the soft hyphen is to long:
<div class="container">
foobar­foo
</div>
Further information: https://en.wikipedia.org/wiki/Soft_hyphen
answered Apr 7, 2020 at 10:47
janDojanDo
233 bronze badges
Example
Use of the text-overflow property:
div
{
white-space: nowrap;
overflow:
hidden;
text-overflow: ellipsis;
}
Try it Yourself »
More «Try it Yourself» examples below.
Definition and Usage
The text-overflow
property specifies how overflowed content that is not
displayed should be signaled to the user. It can be clipped, display an ellipsis
(…), or display a custom string.
Both of the following properties are required for text-overflow:
- white-space: nowrap;
- overflow: hidden;
Show demo ❯
Default value: | clip |
---|---|
Inherited: | no |
Animatable: | no. Read about animatable |
Version: | CSS3 |
JavaScript syntax: | object.style.textOverflow=»ellipsis» Try it |
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
Numbers followed by -o- specify the first version that worked with a prefix.
Property | |||||
---|---|---|---|---|---|
text-overflow | 4.0 | 6.0 | 7.0 | 3.1 | 11.0 9.0 -o- |
CSS Syntax
text-overflow: clip|ellipsis|string|initial|inherit;
Property Values
Value | Description | Demo |
---|---|---|
clip | Default value. The text is clipped and not accessible | Demo ❯ |
ellipsis | Render an ellipsis («…») to represent the clipped text | Demo ❯ |
string | Render the given string to represent the clipped text | |
initial | Sets this property to its default value. Read about initial | |
inherit | Inherits this property from its parent element. Read about inherit |
More Examples
Example
Text-overflow with a hover effect (show entire text on hover):
div.a {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
div.a:hover {
overflow: visible;
}
Try it Yourself »
Related Pages
CSS tutorial: CSS Text Effects
HTML DOM reference: textOverflow property
The text-overflow
CSS property sets how hidden overflow content is signaled to users. It can be clipped, display an ellipsis (‘…
‘), or display a custom string.
Try it
The text-overflow
property doesn’t force an overflow to occur. To make text overflow its container you have to set other CSS properties: overflow
and white-space
. For example:
overflow: hidden; white-space: nowrap;
The text-overflow
property only affects content that is overflowing a block container element in its inline progression direction (not text overflowing at the bottom of a box, for example).
Syntax
The text-overflow
property may be specified using one or two values. If one value is given, it specifies overflow behavior for the end of the line (the right end for left-to-right text, the left end for right-to-left text). If two values are given, the first specifies overflow behavior for the left end of the line, and the second specifies it for the right end of the line.
text-overflow: clip; text-overflow: ellipsis ellipsis; text-overflow: ellipsis " [..]"; text-overflow: inherit; text-overflow: initial; text-overflow: revert; text-overflow: revert-layer; text-overflow: unset;
- one of the keyword values:
clip
,ellipsis
,fade
- the function
fade()
, which is passed a<length>
or a<percentage>
to control the fade distance - a
<string>
.
Values
clip
-
The default for this property. This keyword value will truncate the text at the limit of the content area, therefore the truncation can happen in the middle of a character. To clip at the transition between characters you can specify
text-overflow
as an empty string, if that is supported in your target browsers:text-overflow: '';
. ellipsis
-
This keyword value will display an ellipsis (
'…'
,U+2026 HORIZONTAL ELLIPSIS
) to represent clipped text. The ellipsis is displayed inside the content area, decreasing the amount of text displayed. If there is not enough space to display the ellipsis, it is clipped. -
<string>
Experimental -
The
<string>
to be used to represent clipped text. The string is displayed inside the content area, shortening the size of the displayed text. If there is not enough space to display the string itself, it is clipped. -
fade
Experimental -
This keyword clips the overflowing inline content and applies a fade-out effect near the edge of the line box with complete transparency at the edge.
-
fade( <length> | <percentage> )
Experimental -
This function clips the overflowing inline content and applies a fade-out effect near the edge of the line box with complete transparency at the edge.
The argument determines the distance over which the fade effect is applied. The
<percentage>
is resolved against the width of the line box. Values lower than0
are clipped to0
. Values greater than the width of the line box are clipped to the width of the line box.
Formal definition
Formal syntax
text-overflow = clip | ellipsis
Examples
One-value syntax
This example shows different values for text-overflow
applied to a paragraph, for left-to-right and right-to-left text.
HTML
<div class="ltr"> <h2>Left to right text</h2> <pre>clip</pre> <p class="overflow-clip">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> <pre>ellipsis</pre> <p class="overflow-ellipsis">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> <pre>" [..]"</pre> <p class="overflow-string">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> </div> <div class="rtl"> <h2>Right to left text</h2> <pre>clip</pre> <p class="overflow-clip">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> <pre>ellipsis</pre> <p class="overflow-ellipsis">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> <pre>" [..]"</pre> <p class="overflow-string">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> </div>
CSS
p { width: 200px; border: 1px solid; padding: 2px 5px; white-space: nowrap; overflow: hidden; } .overflow-clip { text-overflow: clip; } .overflow-ellipsis { text-overflow: ellipsis; } .overflow-string { text-overflow: " [..]"; } body { display: flex; justify-content: space-around; } .ltr > p { direction: ltr; } .rtl > p { direction: rtl; }
Result
Two-value syntax
This example shows the two-value syntax for text-overflow
, where you can define different overflow behavior for the start and end of the text. To show the effect we have to scroll the line so the start of the line is also hidden.
HTML
<pre>clip clip</pre> <p class="overflow-clip-clip">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> <pre>clip ellipsis</pre> <p class="overflow-clip-ellipsis">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> <pre>ellipsis ellipsis</pre> <p class="overflow-ellipsis-ellipsis">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> <pre>ellipsis " [..]"</pre> <p class="overflow-ellipsis-string">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
CSS
p { width: 200px; border: 1px solid; padding: 2px 5px; white-space: nowrap; overflow: scroll; } .overflow-clip-clip { text-overflow: clip clip; } .overflow-clip-ellipsis { text-overflow: clip ellipsis; } .overflow-ellipsis-ellipsis { text-overflow: ellipsis ellipsis; } .overflow-ellipsis-string { text-overflow: ellipsis " [..]"; }
JavaScript
const paras = document.querySelectorAll("p"); for (const para of paras) { para.scroll(100, 0); }
Result
Specifications
A previous version of this interface reached the Candidate Recommendation status. As some not-listed-at-risk features needed to be removed, the spec was demoted to the Working Draft level, explaining why browsers implemented this property unprefixed, though not at the CR state.
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
text-overflow |
1 |
12 |
7 Until Firefox 10, handling of |
8 6 |
11 9-15 |
1.3 |
≤37 |
18 |
7 Until Firefox 10, handling of |
11 10.1-14 |
1 |
1.0 |
string |
No |
No |
9 |
No |
No |
No |
No |
No |
9 |
No |
No |
No |
two_value_syntax |
No |
No |
9 |
No |
No |
No |
No |
No |
9 |
No |
No |
No |
See also
- Related CSS properties:
overflow
,white-space
- CSS properties that control line breaks in words:
overflow-wrap
,word-break
CSS
-
text-justify
The text-justify CSS property sets what type of justification should be applied to when text-align: is an element.
-
text-orientation
The text-orientation CSS property sets of characters in line.
-
text-rendering
The text-rendering CSS property provides information to engine about what optimize when The browser makes trade-offs among speed, legibility, and geometric
-
text-shadow
The text-shadow CSS property adds shadows to This property specified as comma-separated list of shadows.
Internet Explorer | Chrome | Opera | Safari | Firefox | Android | iOS | |
6.0+ | 1.0+ | 9.0+ | 11.0+ | 1.3+ | 7.0+ | 1.0+ | 1.0+ |
Краткая информация
Значение по умолчанию | clip |
---|---|
Наследуется | Нет |
Применяется | К блочным элементам |
Ссылка на спецификацию | http://dev.w3.org/csswg/css3-ui/#text-overflow |
Версии CSS
CSS 1 | CSS 2 | CSS 2.1 | CSS 3 |
---|---|---|---|
Описание
Определяет параметры видимости текста в блоке, если текст целиком не помещается в заданную область. Возможны два варианта: текст обрезается; текст обрезается и к концу строки добавляется многоточие. text-overflow работает в том случае, если для блока значение свойства overflow установлено как auto, scroll или hidden.
Синтаксис
text-overflow: clip | ellipsis
Значения
- clip
- Текст обрезается по размеру области.
- ellipsis
- Текст обрезается и к концу строки добавляется многоточие.
Пример
HTML5CSS3IECrOpSaFx
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>text-overflow</title>
<style>
p.clip {
white-space: nowrap; /* Запрещаем перенос строк */
overflow: hidden; /* Обрезаем все, что не помещается в область */
background: #fc0; /* Цвет фона */
padding: 5px; /* Поля вокруг текста */
text-overflow: ellipsis; /* Добавляем многоточие */
}
</style>
</head>
<body>
<p class="clip">Магнитное поле ничтожно гасит большой круг небесной сферы,
в таком случае эксцентриситеты и наклоны орбит возрастают.</p>
</body>
</html>
Результат данного примера показан на рис. 1.
Рис. 1. Многоточие в конце текста
Браузеры
Opera с версии 9.0 до версии 11.0 использует нестандартное свойство -o-text-overflow.