如何强制表格单元格<td>包装?

Doc*_*day 142 html css html-table

继承人整个页面*可以在main.css文件中定义

/* 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;
}                
Run Code Online (Sandbox Code Playgroud)

继承整个页面:

<%@ 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>
Run Code Online (Sandbox Code Playgroud)

它每次提交时都会延伸.
此页面也在div内.我是否还需要设置div和表格的宽度?

jim*_*415 273

使用table-layout:fixed该表,并word-wrap:break-word在TD.

看这个例子:

<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>
Run Code Online (Sandbox Code Playgroud)

DEMO:

table {border-collapse:collapse; table-layout:fixed; width:310px;}
       table td {border:solid 1px #fab; width:100px; word-wrap:break-word;}
Run Code Online (Sandbox Code Playgroud)
       <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>
    
Run Code Online (Sandbox Code Playgroud)


Lav*_*wal 42

它对我有用.

<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; 

}
Run Code Online (Sandbox Code Playgroud)

而表属性是:

table { 
  table-layout: fixed;
  width: 100%
}
Run Code Online (Sandbox Code Playgroud)


小智 12

td {
overflow: hidden;
max-width: 400px;
word-wrap: break-word;
}
Run Code Online (Sandbox Code Playgroud)

  • 也许你可以解释代码,这样任何人都可以清楚 (6认同)

ash*_*awg 8

当我需要在单元格中显示“漂亮”的 JSON 时,这对我有用:

td { white-space:pre }
Run Code Online (Sandbox Code Playgroud)

有关该white-space物业的更多信息:

  • normal :该值指示用户代理折叠空白序列,并根据需要中断行以填充行框。

  • pre:此值可防止用户代理折叠空白序列。
    仅在保留的换行符处断行。

  • nowrap:此值会像 一样折叠空白normal,但会抑制文本中的换行符。

  • pre-wrap:此值可防止用户代理折叠空白序列。
    在保留的换行符处断行,并根据需要填充行框。

  • pre-line:该值指示用户代理折叠空白序列。
    在保留的换行符处断行,并根据需要填充行框。

(另外,请参阅源代码。)