Simple space and special character & nbsp; only one thing is similar: they mean the separation of words, tags, attributes and other markup elements. However, the use of the usual space is relevant only to increase the readability of the page content and for the convenience of working with it in a text editor.
For the browser and its JavaScript language, the number of spaces between words, tags and other elements does not matter.
Applying spaces
Space is the most needed character. Although it is "empty", but very useful. Even in the good old days, when such characters as “line feed” and “carriage return” were important, any number of spaces anywhere in the code was used only to increase its readability. But the empty space itself was "very much appreciated."
To this day, no compiler (interpreter) has learned how to correctly manipulate words, keywords and syntactic constructions, to distinguish program code from commentary. Without noble tokens like Pascal (pascal) ": =" and Peel (PL / 1) ";", the currency-oriented innovation "Pehape" (PHP) - "$" in the variable name cost, except that "Fortran", which each operator should be in a separate line.
Fundamentally said means that the natural ability to separate words and phrases, to distinguish phrases from sentences, and in the latter to capture the complete meaning of artificial languages still do not.
But a simple space can be in any quantity on any line, and it has a required digital code. Space is not an empty place in the code or in the value of a variable. This is a very important symbol. Therefore, nbsp also matters. What is it? Now consider.
Ideal system: what is not, but so necessary
Many areas of application for writing and displaying content require strict adherence to design rules. A simple example is lists.
Invalid list:
- __First line;
- _ second line;
- third line.
The correct list is:
- First line;
- second line;
- third line.
Here the symbol "_" denotes a simple space, which may be indicated by mistake or may be formed due to alignment of the text of the list item on both sides of the paragraph.
It is important that the content of the list item should immediately follow the number. In particular, at the very beginning of its existence, the MS Word text editor suggested using an inextricable space (ctrl + space bar) to prevent a gap between line elements.
An inextricable space stuck together two adjacent words, and it was not necessary that both of them be such. And in the case of lists, when the item number is not included in the paragraph, gluing is performed to the beginning of the line.
In those days when MS Word was already enjoying steady success, the hypertext language was just being formed, but the presence of special characters in it was supposed from the very beginning. Another thing & nbsp - what it is and why it is needed, it was not immediately clear.
Use in Headers
The correct title is not torn, and the words in it are not carried over. It is centered or aligned on one side or another. Using an inextricable space is difficult.
In hypertext language, it will look like this:
7. & nbsp; 5. & nbsp; FreeBSD Installation Guide
On the HTML page in the browser, the header number will look like this: "7. 5." and all the headings thus formed will be exactly the same.
There is a point worth paying attention to if the title uses text and a picture. The browser displays everything sequentially, exactly as determined by the incoming stream. The img tag is located at the very beginning of the line, but in order for the text to be placed immediately after the picture, you need to consider its width.
Use in page content
Absolute text positioning in rare cases can help. Typically, headers and text go in the same thread, but with different tags, for example h2 and p.
To shift the text to the right of the picture, it’s very convenient to use several & nbsp ;.
In the following example, it is necessary to display the icons of the types of downloaded files and write the corresponding extensions next to the text.
Encoding is done in PHP using nbsp. What is it and how is it used? Now it becomes clear. Similarly, the non-breaking space character is used in JavaScript.
Application & nbsp; in rows
When processing strings (both on the server and inside the browser), an inextricable space is not used - it is too expensive to use as many as six "& nbsp;" instead of one significant character "". However, when the line is sent to the output, to the browser stream, you must encode all the space characters into the appropriate number of inextricable spaces.
The output in the echo or print_r sequence of more than one space will not have the desired effect and the content will “crawl”. For example (PHP):
- echo "2 ____ Hello!"; // here "_" stands for the character "".
- echo "2 & nbsp; Hello!"
Will have a different effect if the tag has justify alignment. In the first case, the number 2 will be printed from the left edge of the block in which the conclusion was made, and the text "Hello!" - from the right.
NBSP - what is it and how to apply it? Important information
Meanwhile, the problem is not spaces at all. There are actually a lot of non-printable characters, and they are not always used correctly. It is worth recalling how MS Word users draw up documents. When you need to write the word "director" on the left, and his last name on the right, the vast majority sculpt several tabs in a row - quickly and efficiently.
Instead of correctly setting the value of the tab character and alignment on it, the user clicks the tab several times and gets the desired effect.
A similar situation in the HTML markup language. Many developers do not use special characters correctly, do not apply the necessary styles as they should be, and the result: "Hello, World, I was wrong the third time ..." - will be stretched across the entire width of the block into which this line was withdrawn.
If you recode the string to "Hello & nbsp; Peace, & nbsp; I & nbsp; 3rd & nbsp; times & nbsp; wrong ..." - the result will be exactly what it should be.
Knowing and using special characters is a very good idea. Especially if you have a desire to create effective and high-quality pages in which the content is located in a high-quality and accurate manner.