Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS white-space Property

Posted in CSS Tutorial
Updated on Jul 14, 2024
By Mari Selvan
👁️ 25 - Views
⏳ 4 mins
💬 1 Comment
CSS white-space Property

Photo Credit to CodeToFun

🙋 Introduction

The white-space property in CSS is used to control how white space inside an element is handled. This includes spaces, tabs, and newline characters.

By using the white-space property, you can manage how text is displayed, whether it's preserving spaces, wrapping lines, or collapsing whitespace.

💡 Syntax

The syntax for the white-space property is simple and can be applied to any element.

Syntax
Copied
Copy To Clipboard
element {
  white-space: value;
}

Here, value can be one of several predefined keywords that dictate how white space is treated.

🎛️ Default Value

The default value of the white-space property is normal. This means that sequences of white space will collapse into a single space, and lines will wrap as necessary to fit within the block container.

🏠 Property Values

ValueDescription
normalCollapses whitespace and breaks lines as needed.
nowrapCollapses whitespace but prevents line breaks.
prePreserves whitespace and line breaks.
pre-wrapPreserves whitespace but still wraps lines when necessary.
pre-lineCollapses whitespace but preserves line breaks.
break-spacesPreserves whitespace, including spaces at the end of lines, and only breaks at allowed break points.

📄 Example

In this example, we'll demonstrate how different values of the white-space property affect text display.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS white-space Example</title>
  <style>
    .normal {
      white-space: normal;
    }
    .nowrap {
      white-space: nowrap;
    }
    .pre {
      white-space: pre;
    }
    .pre-wrap {
      white-space: pre-wrap;
    }
    .pre-line {
      white-space: pre-line;
    }
  </style>
</head>
<body>
  <h1>White-space Property Examples</h1>
  <div class="normal">
    This is an example     of white-space: normal.   
    This text will wrap normally and    extra spaces will be collapsed.
  </div>
  <br>
  <div class="nowrap">
    This is an example of white-space: nowrap. This text will not wrap to the next line.
  </div>
  <br>
  <div class="pre">
    This is an example     of white-space: pre.
    This text will preserve        spaces and line breaks.
  </div>
  <br>
  <div class="pre-wrap">
    This is an example     of white-space: pre-wrap.
    This text will preserve        spaces and line breaks, but also wrap when necessary.
  </div>
  <br>
  <div class="pre-line">
    This is an example     of white-space: pre-line.
    This text will collapse spaces but preserve line breaks.
  </div>
</body>
</html>

🖥️ Browser Compatibility

The white-space property is well-supported in all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. This ensures that your text formatting will be consistent across different platforms.

🎉 Conclusion

The white-space property is an essential tool for web developers when it comes to managing text display.

Whether you need to preserve formatting or ensure that text wraps correctly, the white-space property provides the flexibility to handle whitespace in a way that suits your design needs. Experiment with the different values to see how they can enhance the readability and layout of your content.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy