简单的方案:

  <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>的宽度是一样的。


当前回答

这些对我来说都不工作,并且在数据表上有许多cols,使%或sm类等于12个元素布局。

我正在使用Angular 5和Bootstrap 4的数据表,并且在表中有很多col。我的解决方案是在TH中添加一个具有特定宽度的DIV元素。例如,对于cols“Person Name”和“Event date”,我需要一个特定的宽度,然后在col标题中放一个div,整个col宽度然后调整为从TH上的div指定的宽度:

<table datatable [dtOptions]="dtOptions" *ngIf="listData" class="table table-hover table-sm table-bordered text-center display" width="100%">
    <thead class="thead-dark">
        <tr>
            <th scope="col">ID </th>
            <th scope="col">Value</th>
            <th scope="col"><div style="width: 600px;">Person Name</div></th>
            <th scope="col"><div style="width: 800px;">Event date</div></th> ...

其他回答

如果没有页面html或其他CSS的上下文,就很难判断。你的CSS规则不影响td元素可能有无数个原因。

您是否尝试过更具体的CSS选择器,例如

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

这可以避免您的CSS被来自引导CSS的更具体的规则所覆盖。

(顺便说一下,在你的内联CSS样本样式属性,你拼错了宽度-这可以解释为什么尝试失败!)

这些对我来说都不工作,并且在数据表上有许多cols,使%或sm类等于12个元素布局。

我正在使用Angular 5和Bootstrap 4的数据表,并且在表中有很多col。我的解决方案是在TH中添加一个具有特定宽度的DIV元素。例如,对于cols“Person Name”和“Event date”,我需要一个特定的宽度,然后在col标题中放一个div,整个col宽度然后调整为从TH上的div指定的宽度:

<table datatable [dtOptions]="dtOptions" *ngIf="listData" class="table table-hover table-sm table-bordered text-center display" width="100%">
    <thead class="thead-dark">
        <tr>
            <th scope="col">ID </th>
            <th scope="col">Value</th>
            <th scope="col"><div style="width: 600px;">Person Name</div></th>
            <th scope="col"><div style="width: 800px;">Event date</div></th> ...

好的,我刚刚找出了问题所在-在引导设置为默认值宽度选择元素,因此,解决方案是:

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

其他的都对我没用。

<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 style=“min-width:120px;”>B</td>