Hello everyone and let's get started. Let's say we have the following text:
The government of the 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 turn it into a city of the future, as if straight from 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 of 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!
Determine how to wrap long words:
- only where there is a dash, space or Enter (excl., non-breaking space and non-breaking hyphen ‑). A hyphen is written between parts of one word (for example, red-yellow), and a dash is written between words. "Soft hyphen" - appears only when a transfer is necessary. If the word goes beyond the parent, then
or transfers its part without a line. Mathematical expressions use a minus sign (for example, 5 − 2). Phone numbers display a digit bar (for example, +7 800 000‒00‒00). And all this is not the same familiar thing that is on the keyboard. - after any character.
- according to the rules of the Russian language with automatic use of a hyphen.
overflow-wrap
word-wrap
word-break
line-break
hyphens
our one thousand nine hundred and ninety nine kilogram has already been re-inspected lifter-bulldozer-loader
What is the difference between one property and another?
By default, long words are not hyphenated unless explicitly indicated by a hyphen, and begin on a new line.
In order to ignore dashes that are immediately visible to the eye, we add word-break: keep-all; .
To prevent the browser from paying attention to the soft hyphen, insert hyphens: none; .
If word wrapping is required, then word-wrap: break-word; I advise you to always use it, since it is understood by all browsers. It is different from word-break: break-all; , which takes precedence in that words that do not fit in the block start on a new line and the soft hyphen recommendation is taken into account.
When used together word-break: break-all; with hyphens: auto; , the latter is ignored. hyphens: auto; Places any hyphens at its discretion. But in order for it to work, you need to indicate your language by specifying the lang="ru" attribute in the div.
Don't wrap words on another line
Let's say a menu item or button won't look good if it breaks apart. Therefore, their separation must be prohibited. To do this, all the above properties should be set to “default” mode and added. Click on and look at our training ground.
Control word wrapping with hyphens: auto;
Tetrahydropyranylcyclopentyltetrahydropyridopyridine
Hi all). I continue to write about various useful properties of the CSS language, which in one way or another can help with layout. And today I want to tell you how to do css transfer words if they don't fit into their container. I'll show you everything with a real example.
Enable hyphenation for words that don’t fit
Let's say I have a block 100 pixels wide and I need to write some text in it. The text contains the word " self-extracting". This is how an archive happens, for example) But that’s not the point. The word is very long, it simply won’t fit into the width of the container. Let's write it in such a narrow block (100 pixels). What will happen?
As you can see, the poor word has crawled out of the container, well, it has nowhere to go, what can you do. And this is without indentation. So, this is where the word-wrap property comes to the rescue. Here's what you need to set to the block to enable word wrapping in it:
Word-wrap: break-word;
And to be more convincing, you can also write padding(padding). So, after applying the property, we see that words that are too long will be transferred letter by letter to another line. Moreover, even if the second line is not enough for the word, the rest of it will be transferred to the third, etc.
I would like to note that the property can be used without fear. First, it works great in browsers today. Secondly, it works smart. Namely, for all normal words there will be no hyphens; the entire words will be transferred to the next line, so readability will not be affected. You can see it in this screenshot.
When to use word-wrap
Actually, so far I see 2 use cases. The first is just for narrow blocks, where you are afraid that long words may unsightly stick out beyond the boundaries of the block. The second is when the designer intended the site title to be in several lines.
So, in this article we learned how to do word wrapping in css. That's all for me today. See you.
The white-space property specifies how to display spaces between words. IN normal conditions 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 the text on a 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 transferred to 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
An 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.
When making layouts, webmasters periodically have a question: how will the text be transferred? In most cases, the browser handles this task on its own. But sometimes this process has to be taken under control, especially when formatting long words and phrases that, if translated incorrectly, lose their meaning.
word-wrap property
In HTML there is a special tag to separate lines
. But its too frequent use is considered bad manners among developers and often indicates unprofessionalism. As proof, imagine you have a logo and you want each letter to start on a new line:
The result is cumbersome and ugly code that will cause culture shock to any developer. And what should you do if you want the logo to be positioned horizontally on the desktop version, and vertically if the screen width is less than 550 pixels? Therefore, to configure appearance elements always use cascading style sheets. Moreover, with the help of CSS tools, line breaks are carried out in a more elegant way. In this case, there is no redundant markup, which only reduces page loading speed.
The first property you should access for text processing is word-wrap. Accepts three values: normal, break-all and keep-all. To work, you only need to remember break-all. Normal is the default and there is no point in specifying it. Keep-all means in CSS document prohibition of line breaks. Designed specifically for Chinese, Japanese and Korean characters. Therefore, if you are not going to blog in one of these languages, the property will not be useful to you. And also it is not supported Safari browser And mobile phones based on iOS.
To assign the logo from the previous example with using CSS To move each letter to a new line, you need to write the following code:
P( font: bold 30px Helvetica, sans-serif; width: 25px; word-wrap: break-all; )
The width and size of the font is selected in such a way that there is enough space for only one letter. Word-wrap with the value break-all tells the browser to break the word onto a new line each time. This property cannot be called irreplaceable. But it is useful when designing small blocks of text, such as fields for entering comments.
white-space property
A common mistake new web developers make is trying to edit text with spaces or Enter keys, and then wondering why their efforts don't show up on the page. No matter how many times you press Enter, the browser will ignore it. But there is a way to make it display the text the way you want, taking into account all the spacing.
In a CSS document, line breaks assigned using the white-space property can be configured to respect spaces or Enter keystrokes. White-space with the value pre-line will force the browser to see Enter in the text.
If you change pre-line to pre-wrap in your CSS code, the line wrap will take into account whitespace. And vice versa, prohibit any wrapping by assigning the text a white-space property with the value nowrap:
#wrapper p( color: #FFF; padding: 10px; font: bold 16px Helvetica, sans-serif; white-space: nowrap; )
Text-overflow
Another useful tool for working with text is text-overflow. In addition to line breaks, the CSS property allows you to trim content when the container is full. Takes two values:
- clip - simply trims the text;
- ellipsis - adds an ellipsis.
For the property to work, the element must also be set to prohibit line breaks and overflow with the value hidden.