Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
CSS white-space Property
The CSS white-space property controls how white-space characters (spaces, tabs, line breaks) inside an element are handled. It determines whether white-space is preserved, collapsed, or wrapped.
Syntax
selector {
white-space: value;
}
Possible Values
| Value | Description |
|---|---|
normal |
Default. White-space is collapsed and text wraps normally |
nowrap |
White-space is collapsed but text does not wrap |
pre |
White-space is preserved and text does not wrap |
pre-wrap |
White-space is preserved but text wraps when necessary |
pre-line |
White-space is collapsed except for line breaks |
Example: Comparing white-space Values
The following example demonstrates different white-space property values −
<!DOCTYPE html>
<html>
<head>
<style>
.container {
width: 300px;
border: 1px solid #ccc;
margin: 10px 0;
padding: 10px;
}
.normal {
white-space: normal;
}
.nowrap {
white-space: nowrap;
}
.pre {
white-space: pre;
}
.pre-wrap {
white-space: pre-wrap;
}
</style>
</head>
<body>
<div class="container normal">
<strong>normal:</strong> This text has multiple spaces and will wrap normally.
</div>
<div class="container nowrap">
<strong>nowrap:</strong> This text has multiple spaces and will not wrap.
</div>
<div class="container pre">
<strong>pre:</strong> This text preserves
spaces and line breaks.
</div>
<div class="container pre-wrap">
<strong>pre-wrap:</strong> This text preserves
spaces but wraps when the container width is exceeded.
</div>
</body>
</html>
Four bordered boxes appear showing different white-space behaviors: - normal: Multiple spaces collapse to single spaces, text wraps - nowrap: Spaces collapse, text extends beyond container without wrapping - pre: All spaces and line breaks are preserved exactly as typed - pre-wrap: Spaces preserved but text wraps when container width is reached
Conclusion
The white-space property gives you precise control over how text spacing and wrapping behave. Use pre for code blocks, nowrap for single-line text, and normal for regular content.
Advertisements
