New line html. How to add line breaks using CSS properties

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 it's too much 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:

Checking the rewording of words

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. Especially with the help of tools CSS transfer strings is done 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 transfer to new line each letter, 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.

Checking the rewording of words

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 - this 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.
#wrapper p( color: #FFF; padding: 10px; font: bold 16px Helvetica, sans-serif; text-overflow: ellipsis; /*Add an ellipsis*/ white-space: nowrap; /* Disable line wrapping */ overflow: hidden;/*Hide everything that goes beyond the container*/ )

For the property to work, the element must also be set to linebreak and overflow with the value hidden.

Lesson 5.

In this lesson we:
1. Let's find out how to do it html code was more convenient and easy to read for us.
2. Let's look at how to correctly wrap a text line.

Making the html code convenient.

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

To avoid a mess of tags, you need to initially arrange tags and lines so that they are visually easier to perceive. When the browser reads information from html pages, it doesn’t matter to him how many spaces and how many empty lines 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 exactly the same. 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 the difference in visual perception is noticeable. There are no specific rules for “putting things in order”; each master decides for himself how it is more convenient for him to work.

HTML line break. Tag <br>.

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. The text will be written in one line:


You ask why is this? Indeed, in one of the codes, part of the text is moved to another line. It would be logical if in the browser some of the text was also moved 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 this is equivalent to one space(like a regular space between words in text). If we move part of the text not one line down, but 2 or 3 (any number), then the browser will still consider this distance as one regular space between words and when displayed on the screen, the text will be written in one line.

Tag <br>

When we got acquainted with tags in the third lesson, I mentioned that there are tags that do not require closing. Tag <br> one of them is used for line breaks.
Let's apply it in code:

We have inserted a tag <br> into our html code and now when you launch the file through the browser, part of the text will be transferred to the next line.
* Don't forget to save changes in Notepad (Ctrl + S) and refresh the page in the browser (F5).

CSS3 text Includes text properties to help solve problems with text wrapping and text trimming within content.

CSS3 properties for text formatting

1. Trimming the text-overflow line

The property allows you to limit the length of text when it does not fit into the container, visually cutting it off or displaying it with ellipses. The property only works if the following conditions are specified: the width of the container must be defined, the element must have the values ​​overflow: hidden and white-space: nowrap . Not inherited.

Syntax

P (width: 300px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; ) Fig. 1. Crop text using the text-overflow property

2. Hyphenation within words word-break

In a normal situation, words are hyphenated to another line where there are spaces or hyphens ("soft hyphen"), or in the case of forced hyphenation using the element
. This property allows you to set hyphenation rules within words in order to long lines completely filled the space inside the container. Not used for CJK languages ​​(Chinese-Japanese-Korean). Inherited.

Syntax

P (word-break: normal;) p (word-break: break-all;) p (word-break: keep-all;)

3. Wrap words in the word-wrap line

It is used in cases where the text does not fit into its container and goes beyond its border. Allows you to break long words and transfer them to next line. Inherited. Only fires if the white-space value allows line breaks.

When displayed text documents In the browser, the location of the line break within a paragraph is determined automatically depending on the size of the fonts and the size of the viewing window. Line breaks can only be carried out using word separator characters (for example, spaces). Sometimes in documents you need to set a forced line feed, which is implemented regardless of the browser settings. This is done using the forced line feed tag.
, which does not have a corresponding closing tag. Enabling a tag
into the document text will ensure that subsequent text is placed at the beginning of a new line.

For example, this approach can be used to create list-type structures without using special list markup tags. Or, for example, this tag cannot be used to display poems, etc.

Here is an example of using forced line feed (Fig. 1.8):

Using forced line feed

Over darkened Petrograd

November breathed the autumn chill.

Splashing with a noisy wave

To the edges of your slender fence,

Neva was tossing around like a sick person

Restless in my bed.

A.S. Pushkin. Bronze Horseman Rice. 1.8.
Tag

can be used to force a line feed

Unlike the paragraph tag
When using the tag

an empty string will not be generated.
Using a tag
requires caution - it is possible that the browser has already made a line break one or two words before it encountered your tag

. This happens if the reader's viewer window width is smaller than the same setting of the program with which you tested your document. In this case, it may turn out that only one word remains on a line in the middle of a paragraph, thereby ruining the beauty of the document layout.

Note
When using the tag

To break text around an image or table, you can set the CLEAR parameter to stop text wrapping. You can read about this in chapters 3 and 4. Tags

There are situations when you need to perform the opposite operation - disable line feed. There is a container tag for this . Text marked with this tag is guaranteed to fit on one line, regardless of its length. If the resulting line extends beyond the browser viewing window, then horizontal stripe

. This happens if the reader's viewer window width is smaller than the same setting of the program with which you tested your document. In this case, it may turn out that only one word remains on a line in the middle of a paragraph, thereby ruining the beauty of the document layout.

scroll. To ensure the continuity of text located in table cells, there is special parameter NOWRAP tag

. You can learn more about this in Chapter 4. Marking up text with a non-breaking line tag you can get very long strings. To avoid this, you can indicate to the reader's browser the location of a possible line feed, which will be performed only when necessary (the so-called "soft" line feed). This can be done by placing a tag in the right place in the text
(Word BReak), which is the same as the tag

. This happens if the reader's viewer window width is smaller than the same setting of the program with which you tested your document. In this case, it may turn out that only one word remains on a line in the middle of a paragraph, thereby ruining the beauty of the document layout.

Rice. 1.8. , does not need a closing tag. is not supported at all by the Netscape browser. Microsoft Browser Internet Explorer .

recognizes this tag only in tagged text Often required inside one Excel cells Wrap the text on a new line. That is, move the text along the lines inside one cell as indicated in the picture. If after entering the first part of the text you simply click on ENTER key

, then the cursor will be moved to the next line, but to a different cell, and we need a move in the same cell. This is very common task and it can be solved very simply - to move text to a new line inside one Excel cell, you need to click ALT+ENTER (press ALT key

, then without releasing it, press the ENTER key)

How to Wrap Text on a New Line in Excel Using a Formula

Sometimes you need to make a line break not just once, but using functions in Excel. Like in this example in the figure. We enter the first name, last name and patronymic and it is automatically collected in cell A6

In the window that opens, in the “Alignment” tab, you must check the box next to “Word wrap” as indicated in the picture, otherwise line wrapping in Excel will not be displayed correctly using formulas.

How to replace a hyphen in Excel with another character and back using a formula Can change the hyphen symbol to any other character , for example on a space, using text function

SUBSTITUTE in Excel

Let's take an example of what is in the picture above. So, in cell B1 we write the SUBSTITUTE function:

SUBSTITUTE(A1,CHAR(10), " ")
CHAR(10) is a line break (we looked at this a little higher in this article);
" " is a space because we are changing the line break to a space

If you need to do reverse operation— change the space to a hyphen (symbol), then the function will look like this:

SUBSTITUTE(A1; " ";CHAR(10))

Let me remind you that in order for line breaks to be reflected correctly, you must specify “Wrap across lines” in the cell properties, in the “Alignment” section.

How to change the hyphen to a space and back in Excel using SEARCH - REPLACE

There are times when formulas are inconvenient to use and replacements need to be made quickly. To do this, we will use Search and Replace. Select our text and press CTRL+H, the following window will appear.

If we need to change the line break to a space, then in the “Find” line we need to enter a line break, for this stand in the “Find” field, then press the ALT key, without releasing it, type 010 on the keyboard - this is a line break code, it will not be visible in this field.

After that, in the “Replace with” field, enter a space or any other character that you need to change to and click “Replace” or “Replace All”.

By the way, this is implemented more clearly in Word.

If you need to change the line break character to a space, then in the “Find” field you need to specify special code"Line break", which is indicated as ^l
In the “Replace with:” field, you just need to make a space and click on “Replace” or “Replace All”.

You can change not only line breaks, but also others Special symbols To get their corresponding code, you need to click on the “More >>”, “Special” button and select the code you need. I remind you that this function only exists in Word, these symbols will not work in Excel.

How to change line break to space or vice versa in Excel using VBA

Let's look at an example for selected cells. That is, we select the required cells and run the macro

1. Change spaces to hyphens in selected cells using VBA

Sub SpacesToHyphens()
For Each cell In Selection
cell.Value = Replace(cell.Value, Chr(32) , Chr(10) )
Next
End Sub

2. Change hyphens to spaces in selected cells using VBA

Sub WrapsToSpaces()
For Each cell In Selection
cell.Value = Replace(cell.Value, Chr(10) , Chr(32) )
Next
End Sub

The code is very simple: Chr (10) is a line break, Chr (32) is a space. If you need to change to any other symbol, then simply replace the code number corresponding to the required symbol.

Character codes for Excel

The picture below shows various symbols and their corresponding codes; several columns represent different fonts. To enlarge the image, click on the picture.