How to prevent text in a table cell from wrapping using CSS?

Given a table which contains the table head and body section. The task is to prevent the text in a table cell from wrapping using CSS. To achieve this we use white-space property of CSS. This property forces the contents of th to display in one line. There are many property values exists to the white-space function.

Syntax:

white-space: normal|nowrap|pre|pre-wrap|pre-line;

Example 1: This example uses white-space property to prevent cell wrapping using CSS.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
   <head>
      <meta charset="utf-8">
        
      <title>white-space property</title>
        
      <style>
         th {
            white-space: nowrap;
         }
      </style>
   </head>
     
   <body>
      <table border="1" collap>
         <thead>
            <tr>
               <th>First Heading of GeeksforGeeks</th>
               <th>Second Heading of GeeksforGeeks</th>
               <th>Third Heading of GeeksforGeeks</th>
               <th>Fourth Heading of GeeksforGeeks</th>
            </tr>
         </thead>
           
         <tbody>
            <tr>
               <td>Value</td>
               <td>Value</td>
               <td>Value</td>
               <td>Value</td>
            </tr>
         </tbody>
      </table>
   </body>
</html>

chevron_right


Output:
Before applying white-space property:

After applying white-space property:

Example 2: This example uses inline white-space property.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
   <head>
      <meta charset="utf-8">
        
      <title>white-space property</title>
   </head>
     
   <body>
      <table border="1" collap>
         <thead>
            <tr>
               <th style="white-space: nowrap;">
                   First Heading of GeeksforGeeks
               </th>
                 
               <th>Second Heading of GeeksforGeeks</th>
                 
               <th style="white-space: nowrap;">
                   Third Heading of GeeksforGeeks
               </th>
                 
               <th>Fourth Heading of GeeksforGeeks</th>
            </tr>
         </thead>
           
         <tbody>
            <tr>
               <td>Value</td>
               <td>Value</td>
               <td>Value</td>
               <td>Value</td>
            </tr>
         </tbody>
      </table>
   </body>
</html>

chevron_right


Output:
Before applying white-space property:

After applying white-space property:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.