Table of Contents
3.1 white-space: normal
3.2 white-space: nowrap
3.3 white-space: pre
3.4 white-space: pre-wrap
3.5 white-space: pre-line
Home Web Front-end CSS Tutorial How to handle spaces in CSS (example)

How to handle spaces in CSS (example)

Aug 06, 2018 pm 05:18 PM

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>
Copy after login
<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.

<p>The browser output is as follows.

hello world
Copy after login
Copy after login
<p>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.

<p>If you want the spaces to be output as they are, you can use the <pre class="brush:php;toolbar:false">&lt;/code&gt; tag. &lt;/p&gt;&lt;div class=&quot;code&quot; style=&quot;position:relative; padding:0px; margin:0px;&quot;&gt;&lt;pre class='brush:php;toolbar:false;'&gt;&lt;pre class=&quot;brush:php;toolbar:false&quot;&gt;◡◡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>
Copy after login
<p>2. Space characters

<p>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).

<p>The browser will automatically convert these symbols into ordinary space keys.

<p>hello
world</p>
Copy after login
<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
Copy after login
Copy after login
<p>So, line breaks within the text are invalid (unless the text is placed within the <pre class="brush:php;toolbar:false">&lt;/code&gt; tag). &lt;/p&gt;&lt;div class=&quot;code&quot; style=&quot;position:relative; padding:0px; margin:0px;&quot;&gt;&lt;pre class='brush:php;toolbar:false;'&gt;&lt;p&gt;hello&lt;br&gt;world&lt;/p&gt;</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.

3.1 white-space: normal

<p>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>
Copy after login
<p>In the above code, there are two spaces in front of the text, a long word and a newline character inside.

<p>Then, the container <p>specifies a relatively small width. For easier identification, the background color is set to red.

p {
  width: 100px;
  background: red;
}
Copy after login
<p>The display effect is as shown below.

<p>

<p>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

<p>white-spaceWhen the attribute is nowrap, line breaks will not occur due to exceeding the width of the container.

p {
  white-space: nowrap;
}
Copy after login
<p>The display effect is as shown below.

<p>

<p>All text is displayed as one line without line breaks.

3.3 white-space: pre

<p>white-spaceWhen the attribute is pre, follow
</code> Label processing. </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>p {
  white-space: pre;
}
Copy after login
<p>The display effect is as shown below.

<p>

<p>The above result is exactly the same as the original text, all spaces and newlines are preserved.

3.4 white-space: pre-wrap

<p>white-spaceWhen 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;
}
Copy after login
<p>The display effect is as shown below.

<p>

<p>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

<p>white-spaceWhen 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;
}
Copy after login
<p>The display effect is as shown below.

<p>

<p>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.

<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 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

How to select a child element with the first class name item through CSS? How to select a child element with the first class name item through CSS? Apr 05, 2025 pm 11:24 PM

When the number of elements is not fixed, how to select the first child element of the specified class name through CSS. When processing HTML structure, you often encounter different elements...

See all articles