Wrapping long words using CSS. How to add line breaks using CSS properties Force css wrapping
The Internet is made up of content, content is made up of words, and words can be very, very long. And sooner or later the webmaster is faced with the problem of hyphenating long words. This problem is especially relevant for responsive design, and for small blocks of content. So how do you deal with this problem?
Hyphen
The first solution for hyphenating long words is to use a hyphen.
Defisi ( -webkit-hyphens: auto; -moz-hyphens: auto; -ms-hyphens: auto; hyphens: auto; )
Browser support: CSS hyphens are supported by almost all modern browsers, with the exception of Chrome, Opera, Android. Also, very often a hyphen is inserted in places that are grammatically incorrect.
Word break
Wordbreak is a CSS property that indicates whether lines should be broken within words.
Obriv-slova ( -ms-word-break: break-all; word-break: break-all; word-break: break-word; )
Browser support: Word breaks are supported in all browsers except Opera Mini and older versions of Opera.
Overflow wrapper
The next solution is to use an overflow-wrap.
Obertka-perepolneniya ( word-wrap: break-word; overflow-wrap: break-word; )
Browser support: Supported in almost all browsers. Note: Some browsers require the use of "word-wrap" instead of "overflow-wrap".
Ellipsis
Another option is to use ellipses.
Multigotochiye ( overflow: hidden; white-space: nowrap; text-overflow: ellipsis; )
Browser support: Supported by all modern browsers.
This method works, but is far from ideal.
Final solution: Using the Overflow and Hyphen Wrapper.
Finalnoye-resheniye ( overflow-wrap: break-word; word-wrap: break-word; -webkit-hyphens: auto; -ms-hyphens: auto; -moz-hyphens: auto; hyphens: auto; )
This solution will allow a browser that supports it to insert a hyphen, and for browsers that do not support it to insert a line break.
Vlad Merzhevich
Unlike text in printing, hyphens are rarely used on a web page, since we are not strictly tied to the paper format. Sites can be viewed on different monitors, with different resolutions, in different operating systems and browsers. All this gives rise to such a combination of combinations that it is impossible to predict what the final text will look like for the user. Because of this, text is usually aligned to the left, and hyphens occur in entire words. But still, word hyphenation is necessary in some cases, for example, when long chemical or medical terms are used in narrow columns of a given width, for the sake of aesthetics. In HTML and CSS manual or automatic methods There aren't that many hyphens to add, so I'll list them all.
Using a tag
Tag
Example 1. Tag
One
Result this example shown in Fig. 1.
Rice. 1. Text with hyphens
Soft transfer
Application
Example 2: Soft transfer
An eleventh-grade student, Angelica, after graduating from school, chose the profession of business driver.
The result of this example is shown in Fig. 2. Notice how much more aesthetically pleasing and clearer the text looks compared to the figure. 1.
Rice. 2. Text with hyphens
word-break property
To automate the process of creating hyphens, use the word-break property with the value break-all (example 3). You no longer have to add any symbols or tags to HTML; styles take care of everything.
Example 3. Using word-break
Eleventh-grader Angelica chose the profession of office worker after graduating from school.
The result of this example is shown in Fig. 3. The rules for text hyphenation are not taken into account in this case, so words can be hyphenated in a very bizarre way.
Rice. 3. Text with hyphens
Of all the listed methods, “semi-manual” using - gives best result- the rules of the Russian language are observed, the text looks most aesthetically pleasing. Use it when there are long words in the text.
Hyphens property
And finally, the most powerful and convenient property for automatic adding hyphens - hyphens. Its action is based on the hyphenation dictionary built into the browser, so it gives the best result. Supported in IE10, Firefox, Android and iOS. Chrome and Opera do not support. For this to work, for the tag add the lang attribute with the value ru (example 4).
Example 4: Using hyphens
Eleventh-grader Angelica chose the profession of office worker after graduating from school.
The result of this example is shown in Fig. 4.
Rice. 4. Text with hyphens
Prohibition of transfers
Often the opposite task arises - to prohibit hyphenations in places where they are unacceptable according to the rules of the language. For example, you cannot separate units of measurement from a number (10 ml), the designation of a year (54 BC), initials from a surname, break stable abbreviations (etc.), etc. To prevent the browser from adding hyphens at the space, it should be replaced by non-breaking space(example 5).
Example 5: Usage
Lake at coordinates 70° 58′ 19″ N. w. 97° 24′ 5″ E. located in the Taimyr Dolgano-Nenets district of the Krasnoyarsk Territory of Russia.
In this example, to correctly write coordinates, we use , which does not allow text to be wrapped.
The white-space property specifies how to display spaces between words. Under normal circumstances, any number of spaces in HTML code appear as one on a web page. The exception is the element
, the text placed in this container is output with all spaces, as it was formatted by the user. So white-space simulates the workBut unlike it, it does not change the font to monospace.Brief information
Designations
Description | Example | |
---|---|---|
<тип> | Indicates the type of the value. | <размер> |
A && B | The values must be output in the order specified. | <размер> && <цвет> |
A | B | Indicates that you need to select only one value from the proposed ones (A or B). | normal | small-caps |
A || B | Each value can be used independently or together with others in any order. | width || count |
Groups values. | [ crop || cross ] | |
* | Repeat zero or more times. | [,<время>]* |
+ | Repeat one or more times. | <число>+ |
? | The specified type, word, or group is optional. | inset? |
(A, B) | Repeat at least A, but no more than B times. | <радиус>{1,4} |
# | Repeat one or more times separated by commas. | <время># |
Values
normal The text in the browser window is displayed as usual, line breaks are set automatically. nowrap Spaces are not taken into account, line breaks in HTML code are ignored, all text is displayed on one line; at the same time, adding
wraps text to new line. pre The text is shown taking into account all spaces and hyphens, as they were added by the developer in the HTML code. If the line is too long and does not fit in the browser window, a horizontal scroll bar will be added. pre-line Spaces are not taken into account in the text; the text is automatically moved to the next line if it does not fit into the specified area. pre-wrap All spaces and breaks are preserved in the text, but if the width of the line does not fit into the specified area, the text will automatically be wrapped to the next line.
The effect of values on text is presented in table. 1.
Example
Example
Fermat's Last Theorem
X n+ Y n= Z n
where n is an integer > 2
The result of this example is shown in Fig. 1.
Rice. 1. Applying the white-space property
Object Model
Object.style.whiteSpace
Note
Browser Internet Explorer up to version 7.0 inclusive does not support pre-line and pre-wrap values. For
Opera before version 9.5 does not support the pre-line value. For
In Firefox for
Specification
Each specification goes through several stages of approval.
- Recommendation - The specification has been approved by the W3C and is recommended as a standard.
- Candidate Recommendation ( Possible recommendation) - the group responsible for the standard is satisfied that it meets its goals, but requires help from the development community to implement the standard.
- Proposed Recommendation Suggested Recommendation) - at this stage the document is submitted to the W3C Advisory Council for final approval.
- Working Draft - A more mature version of a draft that has been discussed and amended for community review.
- Editor's draft ( Editorial draft) - a draft version of the standard after changes were made by the project editors.
- Draft ( Draft specification) - the first draft version of the standard.
The word-wrap property specifies whether or not to wrap long words that do not fit the width of the specified area.
Brief information
Designations
Description | Example | |
---|---|---|
<тип> | Indicates the type of the value. | <размер> |
A && B | The values must be output in the order specified. | <размер> && <цвет> |
A | B | Indicates that you need to select only one value from the proposed ones (A or B). | normal | small-caps |
A || B | Each value can be used independently or together with others in any order. | width || count |
Groups values. | [ crop || cross ] | |
* | Repeat zero or more times. | [,<время>]* |
+ | Repeat one or more times. | <число>+ |
? | The specified type, word, or group is optional. | inset? |
(A, B) | Repeat at least A, but no more than B times. | <радиус>{1,4} |
# | Repeat one or more times separated by commas. | <время># |
Values
normal Lines are not wrapped or are wrapped in places where wrapping is explicitly specified (for example, using
). break-word Line breaks are added automatically to ensure the word fits within the specified block width.
Example
Noun
Excellency
Animate noun
eleventh grader
chemical substance
methoxychlorodiethylaminomethylbutylaminoacridine
The result of this example is shown in Fig. 1.
Rice. 1. Wrapping long words
Object Model
Object.style.wordWrap
Specification
Each specification goes through several stages of approval.
- Recommendation - The specification has been approved by the W3C and is recommended as a standard.
- Candidate Recommendation ( Possible recommendation) - the group responsible for the standard is satisfied that it meets its goals, but requires help from the development community to implement the standard.
- Proposed Recommendation Suggested Recommendation) - at this stage the document is submitted to the W3C Advisory Council for final approval.
- Working Draft - A more mature version of a draft that has been discussed and amended for community review.
- Editor's draft ( Editorial draft) - a draft version of the standard after changes were made by the project editors.
- Draft ( Draft specification) - the first draft version of the standard.
Hello everyone and let's get started. Let's say we have the following text:
United Government United Arab Emirates and the administration are concentrating in this city all the newest technologies, based on the latest achievements of science and technology, trying to transform it into a city of the future, as if straight out of the screen of some science fiction film. These technologies include robot cops, robot cars and the Hyperloop transport system, and in the near future, an automated flying taxi service will begin operating in Dubai. And in preparation for this event, an electric two-seater Volocopter aircraft with 18 rotors, which will be used as a taxi, made its first flight in automatic mode, they write...
Here we have some text with which we will now begin to work.
And the first property that we will consider is called word-break
word-break: normal | keep-all | break-all
We are mainly interested in two values of this normal property - the default value and break-all, with the help of which we establish that word wrapping is carried out character by character. As for keep-all, this value is used to wrap words in Chinese, Japanese and Korean.
P( word-break: break-all;. )
Please note that after applying this style, all of our text is stretched to the full available width and hyphens are carried out not by words, but by characters. This property can be useful when we have a very long word that does not fit into the specified width. However, this creates a kind of inconvenience, since absolutely all words are transferred character by character, even those that fit into the given width.
Fortunately, there is a similar property that transfers only words that do not fit into characters. It's called overflow-wrap:
P( overflow-wrap: break-word; )
and after applying the break-word value, all our text is transferred word by word, and words that do not fit into the specified width are transferred character by character. We can say the task is completed! In addition to the break-word value, this property accepts:
overflow-wrap: normal | break-word | inherit;
Now let's move on to a more advanced css property for word wrapping in text.
Let's consider the white-space property, with which we can imitate the operation of the pre tag without changing the text itself to monospace.
white-space: normal | nowrap | pre | pre-line | pre-wrap | inherit
Essentially, using this property, we only work with spaces in the text. For example, if we apply the following style to our text:
White-space: nowrap;
all line breaks will be ignored and our text will be displayed as one line.
White-space: pre;
If the value is pre, all line breaks will be similar to those in the source code. Moreover, if the text does not fit into the specified width, then it will not be transferred. If we want it to be transferred, then we need to specify the pre-line value.
If we want to take into account not only line breaks source code, but also spaces between words, then you need to specify:
White-space: pre-wrap;
That’s basically all I wanted to tell you about word hyphenation using CSS. I hope this article was useful to you and you will use the knowledge gained more than once.
Well, I say goodbye to you. I wish you success and good luck! Bye!