【问题标题】:What is the difference between width:auto and max-width:fit-content?width:auto 和 max-width:fit-content 有什么区别?
【发布时间】:2020-11-11 23:17:15
【问题描述】:

我有一个 div

max-width: fit-content; width: auto;

已应用,现在当我删除任何属性时,我看不到任何更改。所以无法弄清楚这两个属性之间的具体区别是什么。我尝试阅读 fit-content 但无法理解。

【问题讨论】:

  • width:auto 没用,因为默认值已经是 auto。对于最大宽度,我们需要更多上下文(HTML/CSS),因为它取决于每种情况
  • 但这并没有使它毫无用处。见stackoverflow.com/questions/30704073/…
  • @ChloeDev 这个问题与width:auto有什么关系?

标签: css


【解决方案1】:

如果您使用display:block 元素,使用max-width: fit-content 会有所不同。

在带有前缀的 Firefox 上工作。 Caniuse

.a {
  max-width: -moz-fit-content;
  max-width: fit-content;
  width: auto;
  background: pink;
}

.b {
  /* max-width: fit-content; */
  width: auto;
  background: yellow;
}
<div class="a">Lorem ipsum dolor </div>
<div class="b">Lorem ipsum dolor </div>

<!-- there is no difference if we are using inline or inline-block elements -->
<span class="a">Lorem ipsum dolor </span>
<span class="b">Lorem ipsum dolor </span>

【讨论】:

  • width:auto 和 max-width:fix-content 有什么区别? max-width ...然后现在,当我删除任何属性时,我看不到任何更改 任何更改。所以我不确定这是如何回答问题的。
  • @TemaniAfif 我不明白“我没有看到任何变化”。当您删除 max-width: fit-content; 时,您会看到不同之处。
  • 再次阅读问题,OP 在同时使用它们时没有看到任何变化,并询问为什么。你实际上展示了相反的结果,所以你没有回答他为什么两者行为相同的问题。
  • 您仍然表现出 差异 .. 无论如何,如果我们没有更多的 HTML/CSS,则无法回答该问题,因为没有通用答案,它基于其他属性。
  • @VXp firefox 支持添加
猜你喜欢
  • 2015-08-22
  • 2012-09-02
  • 2021-10-25
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
  • 2017-11-28
  • 2020-12-12
  • 1970-01-01
相关资源
最近更新 更多