The `white-space` property in CSS

The `white-space` CSS property is used to control how white space inside an element is handled. It determines whether spaces, tabs, line breaks, and other whitespace characters are preserved or collapsed in the rendered output.


The `white-space` property accepts the following values:


1. `normal`: This is the default value. Sequences of whitespace characters are collapsed into a single space. Line breaks are treated as spaces or collapsed.

   

2. `nowrap`: Sequences of whitespace are collapsed into a single space. Text will never wrap to the next line. Line breaks are ignored.


3. `pre`: Whitespace is preserved exactly as written in the HTML source code. Text will only wrap on line breaks and `<br>` elements.


4. `pre-wrap`: Whitespace is preserved, and text will wrap to the next line if it exceeds the width of the container.


5. `pre-line`: Sequences of whitespace are collapsed into a single space. Text will wrap to the next line if it exceeds the width of the container.


Here's an example illustrating the usage of the `white-space` property:


```css

/* Apply different white-space values to paragraphs */

p.normal {

    white-space: normal;

}


p.nowrap {

    white-space: nowrap;

}


p.pre {

    white-space: pre;

}


p.pre-wrap {

    white-space: pre-wrap;

}


p.pre-line {

    white-space: pre-line;

}

```


```html

<!-- Example paragraphs with different white-space values -->

<p class="normal">This is a paragraph with normal white-space handling. Sequences of whitespace are collapsed.</p>


<p class="nowrap">This is a paragraph with nowrap white-space handling. Text will not wrap to the next line.</p>


<p class="pre">This is a paragraph with pre white-space handling. Whitespace is preserved exactly as written in the HTML source code.</p>


<p class="pre-wrap">This is a paragraph with pre-wrap white-space handling. Whitespace is preserved, and text wraps to the next line if needed.</p>


<p class="pre-line">This is a paragraph with pre-line white-space handling. Sequences of whitespace are collapsed, and text wraps to the next line if needed.</p>

```


In this example, each paragraph has a different `white-space` value applied to it, demonstrating how whitespace handling affects the rendering of text. 

Comments

Popular posts from this blog

Quotation marks to wrap an element in HTML

The Basic Structure of a Full-Stack Web App

Unlocking Web Design: A Guide to Mastering CSS Layout Modes