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.
1. Space rules
Spaces in HTML code are usually ignored by browsers.
<p>◡◡hello◡◡world◡◡</p>
The above is a line of HTML code, with two spaces each in the front, inside and back of the text. For ease of identification, the semicircular symbol ◡
is used here to represent spaces.
The browser output is as follows.
hello world
As you can see, the spaces at the front and back of the text will be ignored, and the consecutive spaces inside will only be counted as one. This is the basic rule for browsers to handle spaces.
If you want the spaces to be output as they are, you can use the <pre class="brush:php;toolbar:false"></pre>
tag.
<pre class="brush:php;toolbar:false">◡◡hello◡◡world◡◡
An alternative is to use HTML entities
to represent spaces instead.
<p> hello world </p>
2. Space characters
HTML’s rules for processing spaces apply to a variety of characters. In addition to the normal space bar, it also includes tab characters (\t
) and newline characters (\r
and \n
).
The browser will automatically convert these symbols into ordinary space keys.
<p>hello world</p>
In the above code, the text contains a newline character, which is regarded as a space by the browser, and the output result is as follows.
hello world
So, line breaks within the text are invalid (unless the text is placed within the <pre class="brush:php;toolbar:false"></pre>
tag).
<p>hello<br>world</p>
The above code uses the <br>
tag to explicitly indicate line breaks.
3. The white-space attribute of CSS
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.
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.
3.1 white-space: normal
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>
In the above code, there are two spaces in front of the text, a long word and a newline character inside.
Then, the container <p></p>
specifies a relatively small width. For easier identification, the background color is set to red.
p { width: 100px; background: red; }
The display effect is as shown below.
As you can see, the spaces at the beginning of the text are ignored. Because the container is too narrow, the first word overflows the container and wraps one space after it. Line breaks within the text are automatically converted to spaces.
3.2 white-space: nowrap
white-space
When the attribute is nowrap
, line breaks will not occur due to exceeding the width of the container.
p { white-space: nowrap; }
The display effect is as shown below.
All text is displayed as one line without line breaks.
3.3 white-space: pre
white-space
When the attribute is pre
, follow <pre class="brush:php;toolbar:false"></pre>
Label processing.
p { white-space: pre; }
The display effect is as shown below.
The above result is exactly the same as the original text, all spaces and newlines are preserved.
3.4 white-space: pre-wrap
white-space
When the attribute is pre-wrap
, basically follow the 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; }
The display effect is as shown below.
The spaces at the beginning of the text, internal spaces and newlines are all retained, and line breaks occur beyond the container.
3.5 white-space: pre-line
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; }
The display effect is as shown below.
Except that the line breaks inside the text are not converted into spaces, the others are consistent with the processing rules of normal
. This is useful for poetry type texts.
Recommended related articles:
div tag: implementation of horizontal centering and vertical centering (with code)
Code for string conversion using the text-transform attribute in CSS
The 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!

What it looks like to troubleshoot one of those impossible issues that turns out to be something totally else you never thought of.

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

I know, I know: there are a ton of content management system options available, and while I've tested several, none have really been the one, y'know? Weird pricing models, difficult customization, some even end up becoming a whole &

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
