简单的方案:

  <tr class="something">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>D</td>
  </tr>

我需要为<td>设置一个固定宽度。我试过了:

tr.something {
  td {
    width: 90px;
  }
}

Also

td.something {
  width: 90px;
}

for

<td class="something">B</td>

甚至

<td style="width: 90px;">B</td>

但是<td>的宽度是一样的。


当前回答

这个组合解决方案为我工作,我想要等宽的列

<style type="text/css">

    table {
        table-layout: fixed;
        word-wrap: break-word;
    }

        table th, table td {
            overflow: hidden;
        }

</style>

结果:-

其他回答

<div class="row" id="divcashgap" style="display:none"> <div class="col-md-12"> <div class="table-responsive"> <table class="table table-default" id="gvcashgapp"> <thead> <tr> <th class="1">BranchCode</th> <th class="2"><a>TC</a></th> <th class="3">VourNo</th> <th class="4" style="min-width:120px;">VourDate</th> <th class="5" style="min-width:120px;">RCPT Date</th> <th class="6">RCPT No</th> <th class="7"><a>PIS No</a></th> <th class="8" style="min-width:160px;">RCPT Ammount</th> <th class="9">Agging</th> <th class="10" style="min-width:160px;">DesPosition Days</th> <th class="11" style="min-width:150px;">Bank Credit Date</th> <th class="12">Comments</th> <th class="13" style="min-width:150px;">BAC Comment</th> <th class="14">BAC Ramark</th> <th class="15" style="min-width:150px;">RAC Comment</th> <th class="16">RAC Ramark</th> <th class="17" style="min-width:120px;">Update Status</th> </tr> </thead> <tbody id="tbdCashGapp"></tbody> </table> </div> </div> </div>

我已经为这个问题纠结了一段时间了,所以以防万一有人和我犯同样愚蠢的错误…… 在<td>中,我应用了带有white-space:pre style的元素。这使得我所有的table/tr/td技巧都被丢弃了。当我删除该样式时,突然间我所有的文本都在td中被很好地格式化了。

所以,总是检查主容器(如table或td),但也总是检查你是否在更深的地方取消你的beautifull代码:)

在Bootstrap 4中使用d-flex代替row表示“tr”

问题是“row”类比父容器占用更大的宽度,这就产生了问题。

<table class=“table”> <tbody> <tr class=“d-flex”> <td class=“col-sm-8”>你好</td> <td class=“col-sm-4”>World</td> </tr> <tr class=“d-flex”> <td class=“col-sm-8”>8 列</td> <td class=“col-sm-4”>4 列</td> </tr> </tbody> </table>

对于Bootstrap 4,你可以简单地使用类帮助器:

<table class="table">
  <thead>
    <tr>
      <td class="w-25">Col 1</td>
      <td class="w-25">Col 2</td>
      <td class="w-25">Col 3</td>
      <td class="w-25">Col 4</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      ...

在引导4你可以使用row和col-*在表,我使用它如下

<table class="table">
    <thead>
        <tr class="row">
            <th class="col-sm-3">3 row</th>
            <th class="col-sm-4">4 row</th>
            <th class="col-sm-5">5 row</th>
        </tr>
    </thead>
    <tbody>
        <tr class="row">
            <td class="col-sm-3">some text</td>
            <td class="col-sm-4">some text</td>
            <td class="col-sm-5">some text</td>
        </tr>
    </tbody>
</table>