- Свойство text-overflow
- Синтаксис
- Значения
- Пример . Вылезающий текст
- Пример . Добавим свойство overflow
- Пример . Значение ellipsis
- Пример . С полосами прокрутки
- Пример . Если нет очень длинных слов
- Пример . Текст в одну строку
- Пример . Ширина в процентах
- Смотрите также
- text-overflow
- Try it
- Syntax
- Values
- Formal definition
- Formal syntax
- Wrapping and breaking text
- What is overflowing text?
- Finding the min-content size
- Breaking long words
- Adding hyphens
- The element
- See also
- Found a content problem with this page?
- CSS text-overflow Property
- Browser Support
- CSS Syntax
- Property Values
- More Examples
- Example
Свойство text-overflow
Свойство text-overflow добавляет троеточие в конец обрезанного текста в знак того, что текст не помещался в блок и был обрезан.
Для работы свойства текст должен быть обрезан через свойство overflow или свойство overflow-x в значении hidden , auto или scroll . Если задано visible (а так и есть по умолчанию) — text-overflow работать не будет.
Синтаксис
Значения
Значение | Описание |
---|---|
ellipsis | Добавляет в конец обрезанного текста троеточие. |
clip | Не добавляет троеточие в конец (это значение по умолчанию, нужно для отмены действия свойства при необходимости). |
Значение по умолчанию: clip .
Пример . Вылезающий текст
В данном примере очень длинное слово не поместится в контейнер и вылезет за его границы. Обрезания не происходит:
#elem < width: 200px; overflow: visible; border: 1px solid red; >
Пример . Добавим свойство overflow
Сейчас все, что вылезло за границы контейнера, просто обрежется:
#elem < width: 200px; overflow: hidden; border: 1px solid red; >
Пример . Значение ellipsis
Сейчас в дополнение к свойству overflow добавим еще и text-overflow в значении ellipsis . Обрезанному тексту добавится троеточие в конец:
#elem < width: 200px; overflow: hidden; text-overflow: ellipsis; border: 1px solid red; >
Пример . С полосами прокрутки
Если задать overflow: auto и text-overflow: ellipsis, то появится полоса прокрутки, но троеточие по-прежнему будет добавлено. Попробуйте прокрутить полосу прокрутки в примере:
#elem < width: 200px; overflow: auto; text-overflow: ellipsis; border: 1px solid red; >
Пример . Если нет очень длинных слов
Если нет настолько длинных слов, чтобы они вылазили за границу контейнера, то обрезания не произойдет (ничего же не вылазит). Посмотрите следующий пример:
Пример . Текст в одну строку
Однако, бывают ситуации, когда мы хотим, чтобы текст обрезался, если он слишком длинный (текст вообще, а не отдельные слова) и не переносился на следующую строку. Это делается с помощью добавления свойства white-space в значении nowrap , которое запретит перенос текста на другую строку. Посмотрите на пример, теперь текст обрезается:
Пример . Ширина в процентах
Если задать ширину блока в %, то обрезание тоже будет работать корректно:
#elem < width: 80%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; border: 1px solid red; >
Смотрите также
- свойства word-break и overflow-wrap ,
которые позволяют перенести буквы длинного слова на новую строку - свойство hyphens ,
которое включает переносы слов по слогам - свойство overflow ,
которое обрезает вылезающие за границу блока части
text-overflow
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
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;
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.
Values
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: »; .
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.
The 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.
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.
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.
Formal definition
Formal syntax
Wrapping and breaking text
This guide explains the various ways in which overflowing text can be managed in CSS.
What is overflowing text?
In CSS, if you have an unbreakable string such as a very long word, by default it will overflow any container that is too small for it in the inline direction. We can see this happening in the example below: the long word is extending past the boundary of the box it is contained in.
CSS will display overflow in this way, because doing something else could cause data loss. In CSS data loss means that some of your content vanishes. So the initial value of overflow is visible , and we can see the overflowing text. It is generally better to be able to see overflow, even if it is messy. If things were to disappear or be cropped as would happen if overflow was set to hidden you might not spot it when previewing your site. Messy overflow is at least easy to spot, and in the worst case, your visitor will be able to see and read the content even if it looks a bit strange.
In this next example, you can see what happens if overflow is set to hidden .
Finding the min-content size
To find the minimum size of the box that will contain its contents with no overflows, set the width or inline-size property of the box to min-content .
Using min-content is therefore one possibility for overflowing boxes. If it is possible to allow the box to grow to be the minimum size required for the content, but no bigger, using this keyword will give you that size.
Breaking long words
If the box needs to be a fixed size, or you are keen to ensure that long words can’t overflow, then the overflow-wrap property can help. This property will break a word once it is too long to fit on a line by itself.
Note: The overflow-wrap property acts in the same way as the non-standard property word-wrap . The word-wrap property is now treated by browsers as an alias of the standard property.
An alternative property to try is word-break . This property will break the word at the point it overflows. It will cause a break-even if placing the word onto a new line would allow it to display without breaking.
In this next example, you can compare the difference between the two properties on the same string of text.
This might be useful if you want to prevent a large gap from appearing if there is just enough space for the string. Or, where there is another element that you would not want the break to happen immediately after.
In the example below there is a checkbox and label. Let’s say, you want the label to break should it be too long for the box. However, you don’t want it to break directly after the checkbox.
Adding hyphens
To add hyphens when words are broken, use the CSS hyphens property. Using a value of auto , the browser is free to automatically break words at appropriate hyphenation points, following whatever rules it chooses. To have some control over the process, use a value of manual , then insert a hard or soft break character into the string. A hard break ( ‐ ) will always break, even if it is not necessary to do so. A soft break ( ) only breaks if breaking is needed.
You can also use the hyphenate-character property to use the string of your choice instead of the hyphen character at the end of the line (before the hyphenation line break).
This property also takes the value auto , which will select the correct value to mark a mid-word line break according to the typographic conventions of the current content language.
The element
In the below example the text breaks in the location of the .
See also
- The HTML element
- The CSS word-break property
- The CSS overflow-wrap property
- The CSS white-space property
- The CSS hyphens property
- Overflow and Data Loss in CSS
Found a content problem with this page?
This page was last modified on May 25, 2023 by MDN contributors.
Your blueprint for a better internet.
CSS text-overflow Property
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:
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.
CSS Syntax
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;
>