How to force table cell <td> content to wrap?

HtmlCssHtml Table

Html Problem Overview


Heres the entire page

  • wrappable is defined in a main.css file

    /* Wrappable cell

    • Add this class to make sure the text in a cell will wrap.
    • By default, data_table tds do not wrap. */ td.wrappable, table.data_table td.wrappable { white-space: normal; }

Heres the entire page:

<%@ include file="../../include/pre-header.html" %>
<form id="commentForm" name="commentForm" action="" method="post">



    <ctl:vertScroll height="300" headerStyleClass="data_table_scroll" bodyStyleClass="data_table_scroll" enabled="${user.scrollTables}">
        <ctl:sortableTblHdrSetup topTotal="false" href="show.whatif_edit_entry?   entryId=${entry.entryId}" />
        <table class="data_table vert_scroll_table">



            </tr>
            <tr>
                <ctl:sortableTblHdr styleClass="center" title="Comments" property="comment" type="top">Comments</ctl:sortableTblHdr>
            </tr>


            <c:forEach var="comments" items="${entry.comments}">


                <tr id="id${comments.id}">
                    <td id="comments-${comments.id}" class="wrappable" style="width:400px;">${comments.comment}</td>
                </tr>



            </c:forEach>
            <c:if test="${lock.locked || form.entryId < 0 }">
                <%-- This is the row for adding a new comment. --%>
                    <tr id="commentRow">
                        <td><input type="text" id="comment" name="comment" size="50" maxlength="250" onkeypress="javascript:return noenter();" />
                            <a href="javascript:addComment();"><img src="../images/icon_add.gif" border="0" alt="Add"/></a>
                        </td>

                    </tr>
            </c:if>

        </table>

    </ctl:vertScroll>
</form>

It just stretches every time I submit.
This page is within a div also. Do I need to set the width of the div and the table also?

Html Solutions


Solution 1 - Html

Use table-layout:fixed in the table and word-wrap:break-word in the td.

See this example:

<html>
<head>
   <style>
   table {border-collapse:collapse; table-layout:fixed; width:310px;}
   table td {border:solid 1px #fab; width:100px; word-wrap:break-word;}
   </style>
</head>

<body>
   <table>
      <tr>
         <td>1</td>
         <td>Lorem Ipsum</td>
         <td>Lorem Ipsum is simply dummy text of the printing and typesetting industry. </td>
      </tr>
      <tr>
         <td>2</td>
         <td>LoremIpsumhasbeentheindustry'sstandarddummytexteversincethe1500s,whenanunknown</td>
         <td>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</td>
      </tr>
      <tr>
         <td>3</td>
         <td></td>
         <td>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna...</td>
      </tr>
   </table>
</body>
</html>

DEMO:

table {border-collapse:collapse; table-layout:fixed; width:310px;}
       table td {border:solid 1px #fab; width:100px; word-wrap:break-word;}

       <table>
          <tr>
             <td>1</td>
             <td>Lorem Ipsum</td>
             <td>Lorem Ipsum is simply dummy text of the printing and typesetting industry. </td>
          </tr>
          <tr>
             <td>2</td>
             <td>LoremIpsumhasbeentheindustry'sstandarddummytexteversincethe1500s,whenanunknown</td>
             <td>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</td>
          </tr>
          <tr>
             <td>3</td>
             <td></td>
             <td>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna...</td>
          </tr>
       </table>
    

Solution 2 - Html

Its works for me.

<style type="text/css">
   
 td {

    /* css-3 */
    white-space: -o-pre-wrap; 
    word-wrap: break-word;
    white-space: pre-wrap; 
    white-space: -moz-pre-wrap; 
    white-space: -pre-wrap; 

}

And table attribute is:

table { 
  table-layout: fixed;
  width: 100%
}

Solution 3 - Html

td {
overflow: hidden;
max-width: 400px;
word-wrap: break-word;
}

Solution 4 - Html

This worked for me when I needed to display "pretty" JSON in a cell:

td { white-space:pre }

More about the white-space property:

> - normal : This value directs user agents to collapse sequences of white space, and break lines as necessary to fill line boxes.

> - pre : This value prevents user agents from collapsing sequences of white space.
Lines are only broken at preserved newline characters.

> - nowrap : This value collapses white space as for normal, but suppresses line breaks within text.

> - pre-wrap : This value prevents user agents from collapsing sequences of white space.
Lines are broken at preserved newline characters, and as necessary to fill line boxes.

> - pre-line : This value directs user agents to collapse sequences of white space.
Lines are broken at preserved newline characters, and as necessary to fill line boxes.

(Also, see more at the source.)

Solution 5 - Html

If you are using Bootstrap responsive table, just want to set the maximum width for one particular column and make text wrapping, making the the style of this column as following also works

max-width:someValue;
word-wrap:break-word

Solution 6 - Html

Just add: word-break: break-word; to you table class.

Solution 7 - Html

I solve it putting a "p" tag inside of my "td" tag like this:

<td><p class="">This is my loooooooong paragraph</p></td>

Then add this properties to the class, using max-width to define how wide you want your field to be

.p-wrap {
  max-width: 400px;
  word-wrap: break-word;
  white-space: pre-wrap;
  font-size: 12px;
}

Solution 8 - Html

If you want fix the column you should set width. For example:

<td style="width:100px;">some data</td>

Solution 9 - Html

.wrappable { 
      overflow: hidden; 
      max-width: 400px; 
      word-wrap: break-word; 
}

I have tested with the binary data and its working perfectly here.

Solution 10 - Html

This is another way of tackling the problem if you have long strings (like file path names) and you only want to break the strings on certain characters (like slashes). You can insert Unicode Zero Width Space characters just before (or after) the slashes in the HTML.

Solution 11 - Html

if you want to wrap the data in td then you can use the below code

td{
    width:60%;
    word-break: break-word;
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDoc HolidayView Question on Stackoverflow
Solution 1 - Htmljim31415View Answer on Stackoverflow
Solution 2 - HtmlLavekush AgrawalView Answer on Stackoverflow
Solution 3 - HtmlJafstarView Answer on Stackoverflow
Solution 4 - HtmlashleedawgView Answer on Stackoverflow
Solution 5 - HtmlFeng HanView Answer on Stackoverflow
Solution 6 - HtmlSajjad AljileeziView Answer on Stackoverflow
Solution 7 - HtmlFigx TechnologiesView Answer on Stackoverflow
Solution 8 - HtmlEugeneView Answer on Stackoverflow
Solution 9 - HtmljaipView Answer on Stackoverflow
Solution 10 - HtmlMark LakataView Answer on Stackoverflow
Solution 11 - HtmlPooja NandnikarView Answer on Stackoverflow