首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用“表-布局:固定;”控制表格单元格(td)的宽度。

用“表-布局:固定;”控制表格单元格(td)的宽度。
EN

Stack Overflow用户
提问于 2019-01-15 12:23:47
回答 1查看 23关注 0票数 0

在下面的代码中,我尝试使用以下方法控制列的宽度:

代码语言:javascript
复制
table-layout: fixed;

但这是行不通的;

代码语言:javascript
复制
[class^="col--"] { 
float: left;
}

.col-7 {
  width: 35%;
}

.col-4 {
  width: 20%;
}

.col-3 {
  width: 15%;
}

.col-2 {
  width: 10%;
}


.at { 
background-color:green;
color: white;
width: calc(100% - 3rem);
}

table { 
  margin: 1.5rem 0 0;
  border-collapse: collapse;
  table-layout: fixed;
  width: 100%;
}

td { 
  overflow: hidden;
  padding: .5rem 0;
  float: none;
}
代码语言:javascript
复制
<div class="at">
  <table>
    <thead>
      <tr>
        <th>Row 1</th>
        <th>Row 2</th>
        <th>Row 3</th>
        <th>Row 5</th>
        <th>Row 6</th>
      </tr>
      </thead>
      <tbody>     
        <tr>
          <td class="col-7">Lorem ipsum albatros</td>
          <td class="col-4">#fdsafdsa</td>
          <td class="col-4">Jan. 15, 2019</td>
          <td class="col-2">Pending Action</td>
          <td class="col--3">Lorem ipsum Delete</td>
       </tr>
      </tbody>
   </table>
</div>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-15 13:05:21

我的理解是,列的宽度将由表中的第一行决定--在本例中,是在thead...if中--您将类放在那里,它可以工作。

代码语言:javascript
复制
[class^="col-"] {
  text-align: right;
  border: 1px solid red;
}

.col-7 {
  width: 35%;
}

.col-4 {
  width: 20%;
}

.col-3 {
  width: 15%;
}

.col-2 {
  width: 10%;
}

.at {
  background-color: green;
  color: white;
  width: calc(100% - 3rem);
}

table {
  margin: 1.5rem 0 0;
  border-collapse: collapse;
  table-layout: fixed;
  width: 100%;
}

td {
  overflow: hidden;
  padding: .5rem 0;
  float: none;
}
代码语言:javascript
复制
<div class="at">
  <table>
    <thead>
      <tr>
        <th class="col-7">Row 1</th>
        <th class="col-4">Row 2</th>
        <th class="col-4">Row 3</th>
        <th class="col-2">Row 5</th>
        <th class="col-3">Row 6</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="col-x">Lorem ipsum albatros</td>
        <td class="col-x">#fdsafdsa</td>
        <td class="col-x">Jan. 15, 2019</td>
        <td class="col-x">Pending Action</td>
        <td class="col-x">Lorem ipsum Delete</td>
      </tr>
    </tbody>
  </table>
</div>

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54198800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档