html - layout=fixed 时无法更改列宽和边距

标签 html css bootstrap-4

我尝试在特定列上设置列宽,但它不起作用。我的 table 是 layout=fixedwidth=100% .我设置了max-width=300px对于其中一列但没有任何变化,所有列均匀分布在表中。我的表格没有响应固定列宽。

CSS:


    table {
        table-layout: fixed;
        border-collapse: collapse;
        width: 100%;
    }

HTML:

<div class="container-fluid">
   <table class="table table-hover ml-3 mr-3">
      <tr class="headers bg-primary text-white headers">
         <th>
             @Html.DisplayNameFor(model => model.Name)
         </th>
         <th>
             @Html.DisplayNameFor(model => model.Account)
         </th>
         <th>
             @Html.DisplayNameFor(model => model.Day)
         </th>
      </tr>

   <tbody id="myTable" class="align-middle">

      <tr>
         <td style="word-wrap: break-word; white-space:normal">
             @Html.DisplayFor(modelItem => item.Name)
         </td>
         <td style="max-width: 300px">  <!-- here I set column width  -->
             @Html.DisplayFor(modelItem => item.Account)
         </td>
         <td>
             @Html.DisplayFor(modelItem => item.Day)
         </td>
      </tr>

    </tbody>
   </table>
</div>

添加style="max-width: 300px"<th>也无济于事。 还有 ml-3mr-3效果不好,我只在左边添加了 margin 。此外,水平滚动条出现在表格的底部。

这是它的样子:

enter image description here

最佳答案

max-width属性只适用于 block 元素,所以需要应用display:block:

<td style="max-width: 300px; display: block; margin: auto;">
 XYZ
</td>

table {
        table-layout: fixed;
        border-collapse: collapse;
        width: 100%;
    }
    
    tbody > tr > td {
    vertical-align: middle;
    text-align: center;

}
<div class="container-fluid">
   <table class="table table-hover ml-3 mr-3">
      <tr class="headers bg-primary text-white headers">
         <th>
             Name
         </th>
         <th>
             Account
         </th>
         <th>
             Day
         </th>
      </tr>

   <tbody id="myTable" class="align-middle">

      <tr>
         <td style="word-wrap: break-word; white-space:normal">
             XYZ
         </td>
         <td style="max-width: 300px; display: block; margin: auto;"> <!-- here I set column width  -->
             XYZ
         </td>
         <td>
             XYZ
         </td>
      </tr>

    </tbody>
   </table>
</div>

关于html - layout=fixed 时无法更改列宽和边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65049386/

相关文章:

javascript - PHP 显示隐藏的 Div

javascript - jQuery load() 函数更改输入文本的高度

jquery - 搜索栏在 Bootstrap 崩溃时超出下拉菜单

reactjs - 我可以在 Next.js 中使用 React Bootstrap 吗?

javascript - 为目标为 ="_blank"的 URL 显示带有固定消息的弹出窗口

javascript - 使用 HTML onClick 将 PHP 变量作为参数传递

javascript - jQuery - 使用表单外的选项卡进行表单验证

css - 我可以通过@font-face 在 CSS 中合并字体系列以获得更多字体变体吗?

css - Bootstrap 4,待定表单验证状态

html - Bootstrap 4 下拉菜单,固定位置在导航栏折叠外(移动/小屏幕)