The content of this article is about how to process spaces in CSS (examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
<p>1. Space rules <p>Spaces in HTML code are usually ignored by browsers. <p><p>◡◡hello◡◡world◡◡</p>
◡
is used here to represent spaces. <p>The browser output is as follows. hello world
<pre class="brush:php;toolbar:false"></code> tag. </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:html;toolbar:false"><pre class="brush:php;toolbar:false">◡◡hello◡◡world◡◡</pre></pre><div class="contentsignin">Copy after login</div></div><p>An alternative is to use HTML entities <code>
to represent spaces instead. <p> hello world </p>
\t
) and newline characters (\r
and \n
). <p>The browser will automatically convert these symbols into ordinary space keys. <p>hello world</p>
hello world
<pre class="brush:php;toolbar:false"></code> tag). </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:html;toolbar:false"><p>hello<br>world</p></pre><div class="contentsignin">Copy after login</div></div><p>The above code uses the <code><br>
tag to explicitly indicate line breaks. <p>3. The white-space attribute of CSS<p>The space processing in HTML language is basically direct filtering. Such processing is too crude and completely ignores the fact that the whitespace inside the original text may be meaningful. <p>CSS provides a white-space
attribute, which can provide a more precise way to handle spaces. This attribute has six values in total. In addition to a common inherit
(inheriting the parent element), the remaining five values are introduced below. The default value of the white-space
attribute is normal
, indicating that the browser handles spaces in a normal manner. <p>◡◡hellohellohello◡hello world </p>
<p>
specifies a relatively small width. For easier identification, the background color is set to red. p { width: 100px; background: red; }
white-space
When the attribute is nowrap
, line breaks will not occur due to exceeding the width of the container. p { white-space: nowrap; }
white-space
When the attribute is pre
, follow </code> Label processing. </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:html;toolbar:false">p {
white-space: pre;
}
Copy after login
white-space
When the attribute is pre-wrap
, basically follow the < The pre>
tag is processed in the same way. The only difference is that when the width of the container is exceeded, a line break will occur. p { white-space: pre-wrap; }
white-space
When the attribute is pre-line
, it means to retain the newline character. Except for the newline character, which will be output as it is, everything else is consistent with the white-space:normal
rules. p { white-space: pre-line; }
normal
. This is useful for poetry type texts.
<p> Recommended related articles:
<p>div tag: implementation of horizontal centering and vertical centering (with code)
<p>Code for string conversion using the text-transform attribute in CSSThe above is the detailed content of How to handle spaces in CSS (example). For more information, please follow other related articles on the PHP Chinese website!