html - 如何在CSS中将表格中的不同列设置为不同的宽度?

标签 html css

有一个包含 3 列的表。 现在我想设置第一列 100px 宽度,第二列 200px,最后一列 300px。
为所有 td 设置一个 id 来指示它是哪些列是很尴尬的。
有没有更明智的做法?

最佳答案

假设您的表格具有标准标记,您可以使用 nth-childnth-of-type 来定位每行中的特定单元格。如果您的表有更多列,您可以将任何数字替换为 nth-child

/* nth-child(1) = the first td in each tr */
td:nth-child(1) {
  width: 100px;
  background: #ddd;
  }

/* the second */
td:nth-child(2) {
  width: 200px;
  background: #ccc;
}

/* the third */
td:nth-child(3) {
  width: 300px;
  background: #bbb;
 }
<table>
  <tbody>
    <tr>
      <td>1.1</td>
      <td>1.2</td>
      <td>1.3</td>
      </tr>
    <tr>
      <td>2.1</td>
      <td>2.2</td>
      <td>2.3</td>
     </tr>
    </tbody>
  </table>

关于html - 如何在CSS中将表格中的不同列设置为不同的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40134586/

相关文章:

html - Foundation 6 中的 CSS 提升 Angular

Javascript : Get word after specific word?

JavaScript [ Canvas ] "Slow down framerate?"

CSS 样式不适用于已经存在的组件/主体标签

javascript - 在过渡期间更改 webkit 过渡速度

css - 为什么动画 css 翻译会导致黑色背景上的 webkit 闪烁?

javascript - 在 html/js 中旋转嵌入的 svg 对象

javascript - 页脚的动态绝对位置,缺少像素 - 无限滚动

c# - 如何将级联样式表加载到 C# 对象中?

javascript - 在所选类别之间切换