【问题标题】:How come a child's "white-space: pre;" changes its parent's table "width"? [duplicate]为什么孩子的“空白:pre;”更改其父表的“宽度”? [复制]
【发布时间】:2021-03-07 17:40:06
【问题描述】:

我有一个table 和width: 500px。在桌子里面,我有一个孩子有white-space: pre;。

但是,似乎这种组合使得表格的 500px width 被忽略了。

很难描述,所以请查看演示。

https://jsfiddle.net/1fs4zxr2/4/

table {
  width: 500px;
  background: blue;
}

.codeblock {
  background-color: #f3f3f3;
  overflow: auto;
  white-space: nowrap;
  display: block;
  max-width: 72%;
  width: 100%;
  white-space: pre;
}
<table>
  <tr>
    <td>
      <div class="codeblock">
        "/oauth2/token?grant_type=refresh_token&refresh_token=" + APP_REFRESH_TOKEN + "&client_id=" + APP_CLIENT_ID + "&client_secret=" + APP_CLIENT_SECRET,
        </div>
    </td>
  </tr>
</table>

无论如何我可以让codeblock 尊重表格的 500 像素宽度吗?

【问题讨论】:

  • 你需要表格布局:固定;在桌子上
  • 哇哦,这行得通!谢谢!

标签: html css html-table


【解决方案1】:

为单元格的max-width 使用绝对值(即px)(顺便说一句:width: 100% 和max-width: 72% 是矛盾的)。我在下面使用了 max-width 360px,它是容器 500px 的 72%(你之前的 max-width 值):

table {
  width: 500px;
  background: blue;
}

.codeblock {
  background-color: #f3f3f3;
  overflow: auto;
  white-space: nowrap;
  display: block;
  max-width: 360px;
  white-space: pre;
}
<table>
  <tr>
    <td>

      <div class="codeblock">
        "/oauth2/token?grant_type=refresh_token&refresh_token=" + APP_REFRESH_TOKEN + "&client_id=" + APP_CLIENT_ID + "&client_secret=" + APP_CLIENT_SECRET,
        </div>
    </td>
  </tr>
</table>

【讨论】:

  • 您好,谢谢您的回答。但是,“代码块”应该是“宽度:100%;”因此无论 av 父级,它都可以普遍使用。我不想在“代码块”上硬编码像素宽度。
  • 关于“不管父母如何”:在你写的问题中“我有一个宽度为 500px 的表格”......你仍然可以另外添加 width: 100% - 试试......跨度>
猜你喜欢
  • 1970-01-01
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多