简单的方案:

  <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>
td {
    min-width: 175px;
}
</style>

因此每个表格单元格的最小宽度为175px。您可以更改该值以使其适合您的目的。

你可以在这里找到更多关于min-width的信息:https://developer.mozilla.org/en-US/docs/Web/CSS/min-width?retiredLocale=de

其他回答

如果在表上使用<table class="table">, Bootstrap的表类将为表添加100%的宽度。您需要将宽度更改为自动。

此外,如果表的第一行是标题行,则可能需要将宽度添加到th而不是td。

对于引导4.0:

在Bootstrap 4.0.0中,你不能可靠地使用col-*类(适用于Firefox,但不适用于Chrome)。 你需要使用OhadR的答案:

<tr>
  <th style="width: 16.66%">Col 1</th>
  <th style="width: 25%">Col 2</th>
  <th style="width: 50%">Col 4</th>
  <th style="width:  8.33%">Col 5</th>
</tr>

对于Bootstrap 3.0:

用twitter引导3使用:class="col-md-*"其中*是宽度的列数。

<tr class="something">
    <td class="col-md-2">A</td>
    <td class="col-md-3">B</td>
    <td class="col-md-6">C</td>
    <td class="col-md-1">D</td>
</tr>

对于Bootstrap 2.0:

与twitter引导2使用:class="span*",其中*是宽度的列数。

<tr class="something">
    <td class="span2">A</td>
    <td class="span3">B</td>
    <td class="span6">C</td>
    <td class="span1">D</td>
</tr>

**如果你有<th>元素设置宽度,而不是<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> ...

您可以创建一个colgroup并将这些类应用到col标记,而不是将col-md-*类应用到行中的每个td。

    <table class="table table-striped">
        <colgroup>
            <col class="col-md-4">
            <col class="col-md-7">
        </colgroup>
        <tbody>
        <tr>
            <td>Title</td>
            <td>Long Value</td>
        </tr>
        </tbody>
    </table>

演示

在引导表4.x中

如果您在初始化参数中创建表,而不是使用HTML。

你可以在columns属性中指定width参数:

$("table").bootstrapTable({
    columns: [
        { field: "title", title: "title", width: "100px" }
    ]
});