Text Wrapping

Frontend SELF EN
Level 15 , Lesson 3
Available

8.1 The text-overflow Property

Text wrapping and controlling its display are crucial aspects of web design, especially when dealing with long text lines or containers with limited size. The text-overflow, white-space and word-wrap (now known as overflow-wrap) properties let you control how text behaves in such scenarios.

The text-overflow property manages how text that extends beyond the boundaries of its container is displayed. It is used in combination with the white-space and overflow properties to create text clipping and ellipsis effects.

Values:

  • clip: clips the text that extends beyond the container boundaries
  • ellipsis: adds an ellipsis (...) at the end of clipped text

Example Usage:

CSS
    
      .container {
        width: 200px;
        white-space: nowrap;
        overflow: hidden;
        border: 1px solid #000;
      }

      .clip {
        text-overflow: clip;
      }

      .ellipsis {
        text-overflow: ellipsis;
      }
    
  
HTML
    
      <body>
        <div class="container clip">This text will be clipped at the container's border.</div>
        <div class="container ellipsis">This text will be clipped and replaced with an ellipsis.</div>
      </body>
    
  

8.2 The white-space Property

The white-space property controls the handling of whitespace and text wrapping. It allows you to specify how spaces, tabs, and line breaks are processed in the text.

Values:

  • normal: spaces and line breaks are handled normally (default)
  • nowrap: text is displayed on a single line without wrapping
  • pre: spaces and line breaks are preserved, like in an HTML <pre> tag
  • pre-wrap: spaces and line breaks are preserved, but text may wrap if necessary
  • pre-line: line breaks are preserved, but spaces are handled as usual
  • break-spaces: spaces and line breaks are preserved, and long spaces may cause text to wrap

Example Usage:

CSS
    
      .normal {
        white-space: normal;
      }

      .nowrap {
        white-space: nowrap;
      }

      .pre {
        white-space: pre;
      }

      .pre-wrap {
        white-space: pre-wrap;
      }
    
  
HTML
    
      <body>
        <div class="normal">This text will wrap to the next line if there isn't enough space.</div>
        <div class="nowrap">This text will not wrap to the next line and will display on a single line.</div>
        <div class="pre">
          This text     will preserve

          all spaces and line breaks.
        </div>

        <div class="pre-wrap">
          This text will wrap to the next line but will also preserve

          all spaces and line breaks.
        </div>
      </body>
    
  

8.3 The overflow-wrap Property

The overflow-wrap property (formerly known as word-wrap) controls the breaking of words that extend beyond the container boundaries. It helps prevent container overflow by long words that can't be naturally broken up.

Values

  • normal: word breaks occur according to standard rules
  • break-word: word breaks occur even if it means splitting a word onto a new line

Example Usage:

CSS
    
      .container {
        width: 200px;
        border: 1px solid #000;
      }

      .normal {
        overflow-wrap: normal;
      }

      .break-word {
        overflow-wrap: break-word;
      }
    
  
HTML
    
      <body>
        <div class="container normal">This text will wrap to the next line if there isn't enough space.</div>
        <div class="container break-word">ThislongwordWILLbreakontoanewlineifthereisn'tenoughspace.</div>
      </body>
    
  

Code Explanation:

  • overflow-wrap: normal;: word breaks occur according to standard rules, no splitting of words
  • overflow-wrap: break-word;: long words will be split and wrapped onto the next line as necessary

8.4 Combining Properties

The text-overflow, white-space, and word-wrap properties are often used together to achieve the desired text display effect in containers with limited size.

Example Usage:

CSS
    
      .container {
        width: 200px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        overflow-wrap: break-word;
        border: 1px solid #000;
      }
    
  
HTML
    
      <body>
        <div class="container">This text will be clipped and replaced with an ellipsis if there's not enough space in the container.</div>
      </body>
    
  

Code Explanation:

  • white-space: nowrap;: text is displayed on a single line, no wrapping
  • overflow: hidden;: hides the text that extends beyond the container boundaries
  • text-overflow: ellipsis;: adds an ellipsis at the end of clipped text
  • overflow-wrap: break-word;: breaks long words if there isn't enough space
1
Task
Frontend SELF EN, level 15, lesson 3
Locked
Text Trimming
Text Trimming
1
Task
Frontend SELF EN, level 15, lesson 3
Locked
Text Wrapping
Text Wrapping
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION