White-space | CSS-Tricks (2024)

DigitalOcean provides cloud products for every stage of your journey. Get started with $200 in free credit!

The CSS white-space property controls how text is handled on the element it is applied to. Let’s say you have HTML exactly like this:

<div> A bunch of words you see.</div>

You’ve styled the div to have a set width of 100px. At a reasonable font size, that’s too much text to fit in 100px. Without doing anything, the default white-space value is normal, and the text will wrap. See the example below or follow along at home with the demo.

div { /* This is the default, you don't need to explicitly declare it unless overriding another declaration */ white-space: normal; }
White-space | CSS-Tricks (1)

If you want to prevent the text from wrapping, you can apply white-space: nowrap;

White-space | CSS-Tricks (2)

Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code). When the text renders in the browser, those line breaks appear as if they are stripped out. Also stripped out are the extra spaces on the line before the first letter. If we want to force the browser to display those line breaks and extra white space characters we can use white-space: pre;

White-space | CSS-Tricks (3)

It’s called pre because the behavior is that as if you had wrapped the text in <pre></pre> tags (which by default handle white space and line breaks that way). White space is honored exactly as it is in the HTML and the text does not wrap until a line break is present in the code. This is particularly useful when literally displaying code, which benefits aesthetically from some formatting (and sometimes is absolutely crucial, as in white-space-dependent languages!)

Perhaps you like how pre honors white space and breaks, but you need the text to wrap instead of potentially break out of its parent container. That’s what white-space: pre-wrap; is for:

White-space | CSS-Tricks (4)

Finally, white-space: pre-line; will break lines where they break in code, but extra white space is still stripped.

White-space | CSS-Tricks (5)

Interestingly, the final line break is not honored. As per the CSS 2.1 spec: “Lines are broken at preserved newline characters, and as necessary to fill line boxes.” so perhaps that makes sense.

Here is a table to understand the behaviors of all the different values:

New linesSpaces and tabsText wrapping
normalCollapseCollapseWrap
prePreservePreserveNo wrap
nowrapCollapseCollapseNo wrap
pre-wrapPreservePreserveWrap
pre-linePreserveCollapseWrap

In CSS3, the white-space property is literally going to follow that chart and map the properties to text-space-collapse and text-wrap accordingly.

More information

Browser support

IEEdgeFirefoxChromeSafariOpera
8+AllAllAllAllAll
Android ChromeAndroid FirefoxAndroid BrowseriOS SafariOpera Mobile
AllAllAllAllAll

Related

White-space | CSS-Tricks (2024)
Top Articles
How to Get a Runner's Body without Running a Single Mile ...
How-To: Using the Faker Gem to Seed your Rails Database
Umbc Baseball Camp
NYT Mini Crossword today: puzzle answers for Tuesday, September 17 | Digital Trends
Pieology Nutrition Calculator Mobile
Craigslist Vans
Phcs Medishare Provider Portal
877-668-5260 | 18776685260 - Robocaller Warning!
15 Types of Pancake Recipes from Across the Globe | EUROSPAR NI
Wal-Mart 140 Supercenter Products
Roblox Character Added
The Many Faces of the Craigslist Killer
Over70Dating Login
Conduent Connect Feps Login
Assets | HIVO Support
Colts seventh rotation of thin secondary raises concerns on roster evaluation
U/Apprenhensive_You8924
Five Day National Weather Forecast
Grasons Estate Sales Tucson
Spoilers: Impact 1000 Taping Results For 9/14/2023 - PWMania - Wrestling News
U Arizona Phonebook
China’s UberEats - Meituan Dianping, Abandons Bike Sharing And Ride Hailing - Digital Crew
Bjerrum difference plots - Big Chemical Encyclopedia
Munis Self Service Brockton
What Individuals Need to Know When Raising Money for a Charitable Cause
Finding Safety Data Sheets
Acurafinancialservices Com Home Page
Costco Jobs San Diego
UCLA Study Abroad | International Education Office
As families searched, a Texas medical school cut up their loved ones
Big Boobs Indian Photos
Till The End Of The Moon Ep 13 Eng Sub
Revelry Room Seattle
Bridgestone Tire Dealer Near Me
Nurtsug
The Menu Showtimes Near Amc Classic Pekin 14
15 Downer Way, Crosswicks, NJ 08515 - MLS NJBL2072416 - Coldwell Banker
Whas Golf Card
The best Verizon phones for 2024
Jewish Federation Of Greater Rochester
Pp503063
Shane Gillis’s Fall and Rise
O'reilly's El Dorado Kansas
Miami Vice turns 40: A look back at the iconic series
Tunica Inmate Roster Release
Tableaux, mobilier et objets d'art
60 Days From May 31
Chubbs Canton Il
Sam's Club Gas Price Sioux City
Star Sessions Snapcamz
Underground Weather Tropical
Estes4Me Payroll
Latest Posts
Article information

Author: Mr. See Jast

Last Updated:

Views: 6274

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.