How to set up smartphones and PCs. Informational portal
  • home
  • Interesting
  • The tag is wrapped to the next line. How to add line breaks using CSS properties

The tag is wrapped to the next line. How to add line breaks using CSS properties

April 10, 2016

Hello everyone). I continue to write about various useful properties of the css language that can somehow help in layout. And today I want to tell you how to wrap words in css if they do not fit into your container. I'll show you everything with a real example.

Turn on hyphenation for words that don't fit

Let's say I have a block 100 pixels wide and I need to write some text into it. The word comes across in the text Self-extracting... This is an archive, for example) But not the point. The word is very long, it just won't fit in 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 crawled out of the container, well, he has nowhere to go, what can you do. And this is without indentation. So, here the word-wrap property comes to the rescue. Here's what you need to ask a block to enable word wrap in it:

Word-wrap: break-word;

And for persuasiveness, you can also add padding. So, after applying the property, we see that words that are too long will wrap word by letter to another line. Moreover, even if the second line is not enough for a word, the rest of it will be transferred to the third, etc.

I would like to point out that the property can be used safely. First, it works great in browsers today. Second, it works smartly. Namely, for all normal words there will be no hyphenation, the whole words will be transferred to the next line, so readability will not be impaired. 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 ugly crawl out of the block. The second is when the designer conceived so that the name of the site should be in several lines.

So, in this article, we learned how to do word wrap in css. That's all for me today. See you.

Lesson 5.

In this tutorial, we:
1. Let's learn how to make the html code more convenient and easy to read for us.
2. Let's analyze how to correctly carry out the wrapping of a text line.

We make the html code handy.

Now our code is clear and easy to read, since there is little text in it and there are practically no tags. When we create a more complex page, there will be many tags, so it will be difficult to find the one you need.

So that there is no mess from the tags, you need to initially arrange the tags and lines so that they are visually perceived easier. When the browser reads information from the html page, it doesn't care how many spaces or how many empty lines there are in the code.

I changed the text in the page code relative to the one we created, but it doesn't matter. The left and right pictures show the same code. Both options will be displayed by the browser on the monitor screen in exactly the same way. Agree, working with the code shown on the right will be much easier than with the one on the left.

The code we are looking at is very simple, but even now there is a noticeable difference in visual perception. There are no definite rules for "putting things in order", each master decides for himself how it is more convenient for him to work.

Html line break. The & ltbr & gt tag.

Pay attention to the picture. In the first version, the text is written in one line, in the second version in two lines.


The browser will display both options the same way. The text will be written on one line:


Why is this so, you ask? Indeed, in one of the codes, part of the text has been transferred to another line. It would be logical if in the browser some of the text would also be transferred to another line, but html has its own logic in this regard. If we make a line break in the html code, then for the browser it is equivalent to one space(like a normal space between words in the text). If we move part of the text not one line down, but 2 or 3 (any number), then the browser will still count this distance as one ordinary space between words and when displayed on the screen, the text will be written in one line.

& Ltbr & gt tag

When we got acquainted with tags in the third lesson, I mentioned that there are tags that do not require closing. Tag & ltbr & gt one of them, it serves for line breaks.
Let's apply it in code:

We have inserted a tag & ltbr & gt into our html code and now when the file is launched through the browser, part of the text will be transferred to the next line.
* Do not forget to save changes in Notepad (Ctrl + S) and refresh the page in the browser (F5).

During layout, publishers periodically have a question: how will the text transfer be carried out? In most cases, the browser handles this task on its own. But sometimes this process has to be taken under control, especially by designing long words and phrases that, if incorrectly hyphenated, lose their meaning.

Word-wrap property

In HTML, there is a special tag for separating strings
... But its too frequent use is considered bad form among developers and often indicates unprofessionalism. As proof, imagine you have a logo and want each letter to start on a new line:

Checking word rewriting

The result is cumbersome and ugly code that will give any developer a culture shock. And what if you want the logo to be horizontal in the desktop version, and vertically when the screen is less than 550 pixels wide? Therefore, always use cascading style sheets to customize the appearance of elements. Moreover, with the help of CSS tools, line breaks are done in a more elegant way. At the same time, there is no redundant markup, which only slows down the page loading speed.

The first property to look at for word processing is word-wrap. Accepts three values: normal, break-all, and keep-all. You only need to remember break-all to work. Normal is the default, and there is no point in specifying it. Keep-all means that line breaks are not allowed in a CSS document. Designed specifically for Chinese, Japanese and Korean characters. Therefore, if you are not going to blog in any of these languages, the property will not be useful to you. It is also not supported by Safari browser and iOS mobile phones.

To assign a line break for each letter to the logo from the previous example using CSS, 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 a break-all value tells the browser to wrap the word on a new line each time. This property cannot be called irreplaceable. But it comes in handy when designing small blocks with text, for example, fields for entering comments.

White-space property

A common mistake for novice web developers is to try to edit text with spaces or press Enter, and then wonder why their efforts are not visible on the page. No matter how many times you hit Enter, the browser will ignore it. But there is a way to make it display the text the way you want it to, and taking all the spacing into account.

In a CSS document, line breaks assigned using the white-space property can be configured to take into account spaces or press Enter. White-space with the pre-line value will force the browser to see Enter in the text.

Checking word rewriting

If you change pre-line to pre-wrap in your CSS code, line breaks will take into account whitespace. Conversely, disallow any hyphenation by setting the white-space property with the value nowrap to the text:

#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 content to be clipped when the container is full. It takes two values:

  • clip - just cuts the text;
  • ellipsis - adds ellipsis.
#wrapper p (color: #FFF; padding: 10px; font: bold 16px Helvetica, sans-serif; text-overflow: ellipsis; / * Add ellipsis * / white-space: nowrap; / * Disable line wrapping * / overflow: hidden; / * Hide everything outside the container * /)

For the property to work, the element must also be assigned line break and overflow with the value hidden.

There is often a need to add a new paragraph, but without a blank line, which is inserted by the paragraph tag

For some passages of text, the standard spacing used for paragraphs is simply inappropriate. These can be inscriptions under pictures and in tables, poems, quotes, footnotes and notes.

For forced line wrapping, a special tag is provided, the function of which is in its name br (break row - "to break a row, line"). Tag
html means that all content following it must start on a new line. If necessary, you can put several tags in a row to achieve the required spacing.

Tag
is not case sensitive and does not require an end tag because it is an empty element, but it is better to get in the habit of closing all tags. In XHTML, the break tag must be "tightly closed" with a backslash.

Break tag usage example

The br tag in action< /title></p><p><р>Truancy in service</р></p><p><p>Nowhere and never <br></p><p>I have not been that bad <br></p><p>The avid horde of the chiefs <br></p><p>Gnaws at me alive</р></p><p>Truancy in service</p><p>Nowhere and never <br>I was not so bad. <br>The avid horde of the chiefs <br>Gnaws at me alive.</p><h2>Tag attribute <br></h2><p>The only attribute that the html tag has <br>, called It tells the browser what to do with the wrapped line if the text has to wrap around a so-called floating element, which can be, for example, an image with an align attribute using the right / left values, or a CSS block that has a float property.</p><p>In the XHTML 1.0 / HTML 4.01 specifications, the clear attribute can only be used with Transitional, Frameset, and<!DOCTYPE>otherwise the code will not work.</p><h2>Tag attribute properties</h2><p>The effect of using the clear attribute depends on its value and the position of the floated element that is wrapped around the text. The attribute can take 4 values:</p><p><br clear = "right | left | all | none"></p><p><img src='https://i1.wp.com/fb.ru/misc/i/gallery/21147/458111.jpg' width="100%" loading=lazy loading=lazy></p><p>The left value prevents the left-aligned element from flowing around, so the text stumbling over the tag <br>, will be positioned below the image or other floating element.</p><p>Exactly the same result will come from using the all argument, which will never allow either on the right or on the left.</p><p>The right value prevents text from flowing around a right-aligned element, so after the tag <br>the text will have no choice but to walk around the image, flowing around it to the right.</p><p>The none value ("neither yours, nor ours") generally removes all permissions from the clear attribute, and the tag <br>silently moves the line down.</p><p>As such, there is no default value for the clear attribute of the tag.</p><h2>Tag <br>is a soft transfer</h2><p>The line break tag is very useful for creating the necessary spacing between paragraphs, within which it is used as a soft wrap, but not as a means of dividing text into paragraphs.</p><p><img src='https://i0.wp.com/fb.ru/misc/i/gallery/21147/458084.jpg' width="100%" loading=lazy loading=lazy></p><p>Don't get too carried away with the linefeed tag for formatting text, because the results of its application are not always graceful.</p><p>For example, if you use the tag <br>for line breaks within a paragraph, this can lead to the appearance of a "comb" in the user window if it is smaller than the window that the webmaster was targeting.</p> <p>Hello everyone and let's get started. Let's say we have the following text:</p><p> <p>The government of the United Arab Emirates and the administration concentrate in this city all the newest technologies based on the latest achievements of science and technology, trying to turn its city of the future, as if it came off the screen of some science fiction film. These technologies include police robots, robotic cars and the Hyperloop transportation system, and in the not too distant future, an automatic flying taxi service will start operating in Dubai. And in preparation for this event, the electric two-seater Volocopter with 18 rotors, which will be used as a taxi, made its first flight in automatic mode, they write ...</p> </p><p>here we have indicated some text with which we will now begin to work.</p> <p>And the first property that we will consider is called word-break.</p> <p>word-break: normal | keep-all | break-all</p> <p>We are mainly interested in two values ​​of this property normal - the default value and break-all with which we set that word wrap is carried out character by character. With regard to keep-all, this value is used for word wrapping in Chinese, Japanese, and Korean.</p><p>P (word-break: break-all ;.)</p><p>Please 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 does not fit into the given 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.</p> <p>Fortunately, there is a similar property that only transfers non-fit words across characters. It's called overflow-wrap:</p><p>P (overflow-wrap: break-word;)</p><p>and after applying the value break-word, all our text is wrapped by words, and words that do not fit into the specified width are wrapped character by character. We can say the task is completed! In addition to the break-word value, this property accepts:</p> <p>overflow-wrap: normal | break-word | inherit;</p> <p>Now let's move on to the more advanced css property for word wrap in text.</p> <p>Consider the white-space property with which we can simulate the work of the pre tag without changing the text itself to monospace.</p> <p>white-space: normal | nowrap | pre | pre-line | pre-wrap | inherit</p> <p>In fact, using this property, we only work with spaces in the text. For example, if we apply the following style to our text:</p><p>White-space: nowrap;</p><p>all line breaks will be ignored and our text will be displayed as one line.</p><p>White-space: pre;</p><p>In the case of pre, all line breaks will be the same as in the source code. Moreover, if the text does not fit into the specified width, then it will not be wrapped. If we want it to be carried over, then we need to specify the pre-line value.</p> <p>If we want to take into account not only line breaks in the source code, but also spaces between words, then we need to specify:</p><p>White-space: pre-wrap;</p><p>That's basically all I wanted to tell you about word wrap using css. I hope this article was useful for you and you will use the knowledge gained more than once.</p> <p>Well, I say goodbye to you. I wish you success and luck! Bye!</p> <script>document.write("<img style='display:none;' src='//counter.yadro.ru/hit;artfast_after?t44.1;r"+ escape(document.referrer)+((typeof(screen)=="undefined")?"": ";s"+screen.width+"*"+screen.height+"*"+(screen.colorDepth? screen.colorDepth:screen.pixelDepth))+";u"+escape(document.URL)+";h"+escape(document.title.substring(0,150))+ ";"+Math.random()+ "border='0' width='1' height='1' loading=lazy loading=lazy>");</script> </div> <div class="post-social-counters-block"> <div style="margin-top: 12px"> <noindex></noindex> </div> </div> </div> </div> <a name="comments"></a> <h3 class="best-theme-posts-title">Top related articles</h3> <div class="container-fluid"> <div class="best-theme-posts row"> <div class="theme-post col-sm-4"> <a href="https://bumotors.ru/en/funkciya-select-case-proveryaet-vse-usloviya-operator-vetvleniya-select-case.html"> <div class="img_container"><img src="/uploads/fa7eb80daf682089fbbab9ee0a4163e6.jpg" border="0" alt="Branch operator"Select Case"" width="320" height="180" / loading=lazy loading=lazy></div> <span class="theme-post-link">Branch operator "Select Case"</span> </a> </div> <div class="theme-post col-sm-4"> <a href="https://bumotors.ru/en/osnovnye-html-tegi-chem-neparnye-tegi-otlichayutsya-ot-parnyh-tegi-parnye-i.html"> <div class="img_container"><img src="/uploads/9a2b4cb21cb71173e02f31d9c4b0933a.jpg" border="0" alt="How are unpaired tags different from paired tags?" width="320" height="180" / loading=lazy loading=lazy></div> <span class="theme-post-link">How are unpaired tags different from paired tags?</span> </a> </div> <div class="theme-post col-sm-4"> <a href="https://bumotors.ru/en/kondensatory-kondensatory-raznost-potencialov-mezhdu-obkladkami.html"> <div class="img_container"><img src="/uploads/fae0ff671b8e8fb1fe289d649203d919.jpg" border="0" alt="Capacitors Potential difference between capacitor plates with capacitance" width="320" height="180" / loading=lazy loading=lazy></div> <span class="theme-post-link">Capacitors Potential difference between capacitor plates with capacitance</span> </a> </div> </div> </div> </div> <a name="comments"></a> </div> <div class="right-column col-sm-4 col-md-4"> <div class="write"> <span class="tags-title">Categories:</span> <ul style="height: 286px;" id="right-tags" data-tagscount="18" data-currentmaxtag="10" class="tags"> <li class=""><a href="https://bumotors.ru/en/category/programs/">Programs</a></li> <li class=""><a href="https://bumotors.ru/en/category/safety/">Security</a></li> <li class=""><a href="https://bumotors.ru/en/category/windows-10/">Windows 10</a></li> <li class=""><a href="https://bumotors.ru/en/category/iron/">Iron</a></li> <li class=""><a href="https://bumotors.ru/en/category/windows-8/">Windows 8</a></li> <li class=""><a href="https://bumotors.ru/en/category/vkontakte/">In contact with</a></li> <li class=""><a href="https://bumotors.ru/en/category/errors/">Errors</a></li> </ul> </div> <div class="banner"> </div> </div> </div> </div> <div style="clear:both"></div> </div> <div class="footer"> <div class="subscribe"> <div class="main-wrapper container"> <div class="row"> <div class="col-sm-8"> </div> <div class="col-sm-4"> <div class="social"> <a href="" class="vk social-ico"></a> <a href="https://facebook.com/" class="fb social-ico"></a> <a href="https://twitter.com/" class="tw social-ico"></a> </div> </div> </div> </div> </div> <div class="info"> <div class="main-wrapper container"> <div class="row"> <span class="footer-info col-xs-12">© 2021 bumotors.ru. How to set up smartphones and PCs. Informational portal.</span> </div> </div> </div> </div> </body> </html>