CSS Properties
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.
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
Value | Description |
---|---|
normal | Collapses whitespace and breaks lines as needed. |
nowrap | Collapses whitespace but prevents line breaks. |
pre | Preserves whitespace and line breaks. |
pre-wrap | Preserves whitespace but still wraps lines when necessary. |
pre-line | Collapses whitespace but preserves line breaks. |
break-spaces | Preserves 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.
<!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:
Author
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
If you have any doubts regarding this article (CSS white-space Property), please comment here. I will help you immediately.