How to wrap words in css that don't fit in a block? How to add a line break using CSS properties Force a line break in html.
Description
The white-space property sets how to display spaces between words. Under normal circumstances, any number of spaces in HTML code will show as one on a web page. The exception is the tag
Text placed in this container is displayed with all spaces as it was formatted by the user. So white-space mimics how the tag worksBut unlike it, it does not change the font to monospace.Syntax
white-space: normal | nowrap | pre | pre-line | pre-wrap | inherit
Values
normal The text in the browser window is displayed as usual, line breaks are set automatically. nowrap Spaces are ignored, line breaks in HTML code are ignored, all text is displayed on one line; however, adding the tag
wraps text on a new line. pre The text is shown including all spaces and hyphens as they were added by the developer in the HTML code. If the line is too long to 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 wrapped to the next line if it does not fit in the specified area. pre-wrap All spaces and hyphens are preserved in the text, but if the width of the line does not fit into the specified area, then the text will automatically wrap to the next line. inherit Inherits the value of the parent.The effect of values on the text is presented in Table. one.
HTML5 CSS2.1 IE Cr Op Sa Fx
white space Example
Fermat's Last Theorem
X n+Y n=Z n
where n is an integer > 2The result of this example is shown in Fig. one.
Rice. 1. Applying the white-space property
Object Model
document.getElementById("elementID ").style.whiteSpace
Browsers
Browser internet explorer up to version 7.0 does not support pre-line , pre-wrap , and inherit values. For
Opera prior to version 9.5 does not support the pre-line value. For
Safari prior to version 3.0 and iOS do not support pre-wrap and pre-line values.
Firefox up to and including version 2.0 does not support pre-line and pre-wrap values. For
The white-space property sets how to display spaces between words. Under normal circumstances, any number of spaces in HTML code will show as one on a web page. The exception is the element
, the text placed in this container is displayed with all spaces, as it was formatted by the user. So white-space mimics workingBut unlike it, it does not change the font to monospace.short information
Notation
Description | Example | |
---|---|---|
<тип> | Specifies the type of the value. | <размер> |
A&&B | The values must be output in the specified order. | <размер> && <цвет> |
A | B | Indicates that only one of the suggested values (A or B) should be selected. | normal | small caps |
A || B | Each value can be used alone or in combination 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 ignored, line breaks in HTML code are ignored, all text is displayed on one line; However, adding
wraps text on a new line. pre The text is shown including all spaces and hyphens, as they were added by the developer in the HTML code. If the line is too long to 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 wrapped to the next line if it does not fit in the specified area. pre-wrap All spaces and hyphens are preserved in the text, however, if the width of the line does not fit into the specified area, the text will automatically wrap to the next line.
The effect of values on the text is presented in Table. one.
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. one.
Rice. 1. Applying the white-space property
Object Model
An object.style.whiteSpace
Note
Internet Explorer up to and including version 7.0 does not support pre-line and pre-wrap values. For
Opera prior to version 9.5 does not support the pre-line value. For
In Firefox for
Specification
Each specification goes through several stages of approval.
- Recommendation (Recommendation) - the specification is approved by the W3C and recommended as a standard.
- Candidate Recommendation ( Possible recommendation) - the group responsible for the standard is satisfied that it meets its goals, but the support of the development community is required to implement the standard.
- Proposed Recommendation ( Suggested recommendation) - at this stage, the document is submitted to the W3C Advisory Board for final approval.
- Working Draft - A more mature version of the draft after discussion and amendments for community review.
- Editor's draft ( Editorial draft) is a draft version of the standard after changes have been made by the project editors.
- draft ( Specification draft) is the first draft version of the standard.
Vlad Merzhevich
Unlike text in printing, hyphenation is rarely used on a web page, since we are not rigidly tied to the paper format. Sites can look at different monitors, with different resolutions, in different operating systems and browsers. All this generates such a combination of combinations that it is impossible to predict how the final text will look like for the user. Because of this, usually the text is left-aligned, and hyphenation occurs in whole words. But still, word wrapping 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 ways adding transfers is not so much, so I will list everything.
Tag usage
Tag
Example 1. Tag
One
The result of this example is shown in Fig. one.
Rice. 1. Text with word wraps
Soft transfer
Application
Example 2: Soft Transfer
Anzhelika, an eleventh grade student, after graduating from school, chose the profession of a business-manufacturer.
The result of this example is shown in Fig. 2. Notice how much more aesthetic and understandable the text began to look in comparison with fig. one.
Rice. 2. Text with word wraps
word-break property
To automate the process of creating hyphens, use the word-break property with the value break-all (example 3). You don’t have to add any symbols or tags to HTML anymore, styles take care of everything.
Example 3: Using word-break
Eleventh-grader Angelica, after graduating from school, chose the profession of a clerk.
The result of this example is shown in Fig. 3. Text hyphenation rules are not taken into account in this case, so words can be hyphenated in a very bizarre way.
Rice. 3. Text with word wraps
Of all listed methods"semi-manual" using - gives the best result - the rules of the Russian language are observed, the text looks the 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 addition hyphens - hyphens. Its action is based on the built-in hyphenation dictionary in the browser, therefore 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, after graduating from school, chose the profession of a clerk.
The result of this example is shown in Fig. four.
Rice. 4. Text with word wraps
Transfer prohibition
Quite often, the inverse problem also arises - to prohibit hyphenation in those places where, according to the rules of the language, they are unacceptable. For example, you cannot separate units of measurement from a number (10 ml), a year designation (54 BC), initials from a surname, break stable abbreviations (etc.), etc. To prevent the browser from adding hyphens at the space, its should be replaced with a non-breaking space (example 5).
Example 5 Usage
Lake at coordinates 70° 58′ 19″ N. sh. 97° 24′ 5″ E located in the Taimyrsky Dolgano-Nenetsky District Krasnoyarsk Territory Russia.
AT this example for the correct writing of coordinates, is used, which does not allow text wrapping.
Hi everyone and let's get started. Let's say we have the following text:
United Government United Arab Emirates and the administration concentrates in this city all the latest technologies based on the latest achievements of science and technology, trying to turn it into a city of the future, as if descended from the screen of some sci-fi movie. These technologies include police robots, robot cars and the Hyperloop transport system, and in the near future, an automatic flying taxi service will begin to operate in Dubai. And in preparation for this event, the Volocopter electric two-seat aircraft with 18 rotors, which will be used as a taxi, made its first automatic flight, 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 given property normal is the default value and break-all with which we set that word wrapping is done character by character. As for keep-all, then given value used to translate words in Chinese, Japanese, and Korean.
P( word-break: break-all;. )
note that after applying this style, all our text is stretched to the full available width and hyphenation is carried out not by words, but by characters. This property can be useful when we have a very long word that doesn't fit within the given width. However, this creates a kind of inconvenience, since absolutely all words are transferred character-by-character, even those that fit into a given width.
Luckily, there is a similar property that only wraps non-fitting words character-by-character. It's called overflow-wrap:
P( overflow-wrap: break-word; )
and after applying the break-word value, all our text is wrapped word by word, and words that do not fit into the specified width are wrapped character by character. You can say mission accomplished! 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 wrapping words in text.
Consider the white-space property with which we can mimic the operation of the pre tag without changing the text itself to monospace.
white-space: normal | nowrap | pre | pre-line | pre-wrap | inherit
In fact, using this property, we work only with spaces in the text. For example, if we apply the following style to our text:
Whitespace: nowrap;
all line breaks will be ignored and our text will be displayed as a single line.
White-space: pre;
In the case of the pre value, all line breaks will be the same as line breaks in the source code. Moreover, if the text does not fit in the specified width, then it will not be transferred. If we want it to wrap, then we need to specify a 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 css. I hope this article was useful for 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!
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 faces the problem of wrapping long words. This problem is especially relevant for responsive design, and for small blocks of content. So, how to deal with this problem?
Hyphen
The first solution for wrapping long words is with 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 except Chrome, Opera, Android. Also, very often a hyphen is inserted in places that are grammatically wrong.
Word break
Word break is a CSS property that specifies whether to break lines within words.
Obriv-slova ( -ms-word-break: break-all; word-break: break-all; word-break: break-word; )
Browser support: Breaking is supported in all browsers except Opera Mini and older versions of Opera.
overflow wrapper
The next solution is to use Overflow Wrap (overflow-wrap).
Obertka-perefilleniya ( word-wrap: break-word; overflow-wrap: break-word; )
Browser Support: Supported by almost all browsers. Note: Some browsers require the use of "word-wrap" instead of "overflow-wrap".
ellipsis
Another option is to use dots.
Mnogotochiye ( overflow: hidden; white-space: nowrap; text-overflow: ellipsis; )
Browser Support: Supported by all modern browsers.
It's a working method, but far from ideal.
Final decision: 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 don't support it, insert a line break.