Tutorial

CSS white-space Property

Published on September 3, 2020
author

Alligator.io

CSS white-space Property

white-space is a CSS property that helps control how whitespace and line breaks within an element’s text are treated.

The white-space property can take these values:

  • normal: The default value. Multiple whitespaces are collapsed into one. The text wraps to the next line when needed.
  • nowrap: Multiple whitespaces are collapsed into one, but the text doesn’t wrap to the next line. We’ve already discussed how to use the nowrap value to prevent line breaks.
  • pre: Same results as using the <pre> where all the whitespaces will be kept as is and the text only wraps when line breaks are in the content.
  • pre-line: Multiple whitespaces are collapsed into one, the text breaks to the next line when needed or with line breaks in the content.
  • pre-wrap: Similar to pre, but the text also wraps when needed.

white-space: normal

Medusafish banded killifish convict blenny saury threadsail beluga sturgeon. Indian mul mora cisco masu salmon, roosterfish requiem shark longnose lancetfish bluefish red snapper Sacramento splittail giant danio.

white-space: nowrap

Medusafish banded killifish convict blenny saury threadsail beluga sturgeon. Indian mul mora cisco masu salmon, roosterfish requiem shark longnose lancetfish bluefish red snapper Sacramento splittail giant danio.

white-space: pre

Here I manually included line breaks and extra spaces. Notice the extra line break at the beginning. That’s because in the markup the text start on the line after the <p> element.

Medusafish banded killifish convict blenny saury threadsail beluga sturgeon. Indian mul mora cisco masu salmon, roosterfish requiem shark longnose lancetfish bluefish red

snapper Sacramento splittail giant danio.

pre-line

Here the text breaks when needed, but I also manually broke the last few words. I included the same extra whitespaces, but they are now collapsed.

Medusafish banded killifish convict blenny saury threadsail beluga sturgeon. Indian mul mora cisco masu salmon, roosterfish requiem shark longnose lancetfish bluefish red snapper Sacramento splittail giant danio.

pre-wrap

Now the extra whitespaces aren’t collapsed.

Medusafish banded killifish convict blenny saury threadsail beluga sturgeon. Indian mul mora cisco masu salmon, roosterfish requiem shark longnose lancetfish bluefish red snapper Sacramento splittail giant danio.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Alligator.io

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.