html - 如何自动将 HTML 表格单元格内的文本换行?

标签 html css xhtml html-table

我在 HTML 中遇到以下问题。

我的这个表只包含一个 thead:

<table border="1" class="table_legenda" width="100%">

    <thead>
    <tr>
        <th width="8.33%">Codice</th>
        <th width="2%">Stato</th>
        <th width="8.33%">Creazione<</th>
        <th width="8.33%">Registrazione</th>
        <th width="6.33%">Autore Cr</th>
        <th width="6.33%">Autore Con</th>
        <th width="6.33%">Autore Acq</th>
        <th width="8.33%">Totale Imponibile</th>
        <th width="24.66%">Fornitore</th>
        <th width="4.33%">RM riserva</th>
        <th width="8.33%">Errore</th>
        <th width="8.33%">Azione</th>
    </tr>
    </thead>
</table>

现在,如您所见,每个单元格都设置了一个百分比宽度并包含一个文本值。

我的问题是,如果单元格的文本值比单元格的可用空间长,它将自动隐藏。我希望文本自动换行。

我怎样才能获得这种行为?

谢谢

最佳答案

您需要将表格设置为具有 table-layout:fixed 并将第 th 元素设置为 overflow-wrap:break-word

.table_legenda {
  table-layout: fixed;
}
.table_legenda th {
  overflow-wrap: break-word;
}
<table border="1" class="table_legenda" width="100%">
  <thead>
    <tr>
      <th width="8.33%">Codice</th>
      <th width="2%">Stato</th>
      <th width="8.33%">Creazione</th>
      <th width="8.33%">Registrazione</th>
      <th width="6.33%">Autore Cr</th>
      <th width="6.33%">Autore Con</th>
      <th width="6.33%">Autore Acq</th>
      <th width="8.33%">Totale Imponibile</th>
      <th width="24.66%">Fornitore</th>
      <th width="4.33%">RM riserva</th>
      <th width="8.33%">Errore</th>
      <th width="8.33%">Azione</th>
    </tr>
  </thead>
</table>


演示在 http://jsfiddle.net/ma4bc7zj/

关于html - 如何自动将 HTML 表格单元格内的文本换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27530293/

相关文章:

javascript - 如何使用 JQuery 获取 HTML 表单数据并写入 XML?

javascript - 使用 JavaScript 过滤 HTML <optgroup>

javascript - document.click 以删除页面上的类行为不正常

html - 如何在没有@media 查询的情况下创建响应式 div 宽度

javascript - 为什么 hr 标签的 CSS 不会出现在 React 代码中?

CSS Noob 真的可以在布局和定位方面使用一些熟练的帮助

css - 如何与网页设计师合作?

html - 如何在输入不为空时仅使用 css 永久隐藏搜索图标

css - 在这种情况下如何覆盖字体大小(em)?

javascript - 如何将一个div加载到另一个div中