html - 宽度:自动不适用于内部表格

标签 html css html-table width

我想将父级 (#timer) 的宽度设置为自动,但不幸的是,内部表格会影响此父级的宽度并将其更改为 100% 宽度:(。

这是我的代码:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
}

html, body {
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  -webkit-user-select: none;
  -moz-user-select: -moz-none;
  -ms-user-select: none;
  user-select: none;
}

.time {
  margin: auto;
  border: 1px solid blue;
}

#clock {
  font-family: 'Lato', sans-serif;
  font-weight: 300;
  font-size: 100px;

  border: 1px solid red;

}

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

td {
  width: calc(100% / 3);
  text-align: center;
}

table, td {
  border: 1px solid #000;
}
<div class="time">
  <h1 id="clock">00 : 00 : 00</h1>
  <table>
    <tr>
      <td>hours</td>
      <td>minutes</td>
      <td>seconds</td>
    </tr>
  </table>
</div>

感谢您的帮助和解释:)。

最好的问候

最佳答案

对表格使用 min-width 而不是 width:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  user-select: none;
}

.time {
  margin: auto;
  border: 1px solid blue;
}

#clock {
  font-family: 'Lato', sans-serif;
  font-weight: 300;
  font-size: 100px;

  border: 1px solid red;

}

table {
  min-width: 100%;
  table-layout: fixed;
}

td {
  width: calc(100% / 3);
  text-align: center;
}

table, td {
  border: 1px solid #000;
}
<div class="time">
  <h1 id="clock">00 : 00 : 00</h1>
  <table>
    <tr>
      <td>hours</td>
      <td>minutes</td>
      <td>seconds</td>
    </tr>
  </table>
</div>

关于html - 宽度:自动不适用于内部表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56303308/

相关文章:

javascript - 静态多语言 javascript 重定向不断重新加载页面

javascript - 更新 Google AppS 脚本中的全局变量

javascript - 将 JSON 对象拆分为多个 <td>

javascript - 如何阻止 FullCalendar.js 在移动设备上垂直压缩?

html - 具有相同 TD 宽度的多个表

html - 在 CSS 制作表格中制作 colspans

javascript - 将 HTML 放置在预览 Div 中而不继承 CSS

javascript - jQuery 无法在输入中添加类

html - 如何在保持文本对齐的同时删除两个元素之间的空白?

html - 如何在 Bootstrap 中设置两行之间的边距?