【问题标题】:Is there a way to use use the width of self in a CSS calc rule? [duplicate]有没有办法在 CSS 计算规则中使用 self 的宽度? [复制]
【发布时间】:2022-02-04 19:51:49
【问题描述】:

我已经阅读了Maintain the aspect ratio of a div with CSS(不是重复的,请进一步阅读)和其他方法,但在这里我要问一些更具体的问题:

有没有办法在 CSS 计算规则中使用当前元素(自身)的宽度?

如果没有,是否可以在height: calc(...) 中使用 元素的width

类似:

#container { 
    display: flex; 
    width: 500px; 
    flex-direction: row; 
}
#a { 
    background-color: red; 
    width: 50%;
}
#b { 
    background-color: blue; 
    width: 50%;
    height: calc(100% of self current width); /* <------- pseudo code here - does this exist? */
}
<div id="container">
<div id="a">
Hello
</div>
<div id="b">
World
</div>
</div>

【问题讨论】:

  • 你可以在这里找到一些可以帮助你的东西stackoverflow.com/questions/5445491/…
  • 阅读副本的第二个答案
  • @TemaniAfif 它在这里没有回答这个问题,你读过我的第一段吗?这是一个关于calc的具体问题。
  • 你不能用 calc() 来做,因此建议的解决方案
  • @TemaniAfif 那么,这不是重复的(因为我的问题实际上是关于使用calc 进行操作),答案是“不,这是不可能的”。请重新打开,问题具体有这个要求,并提到了链接问题(不是重复的)。

标签: css css-calc


【解决方案1】:

您可以使用padding-top: 100%; 使 width:height = 1:1。

参考:https://www.w3schools.com/howto/howto_css_aspect_ratio.asp

【讨论】:

  • 确实很有趣。但是calc 有更好的解决方案吗?
猜你喜欢
  • 2021-10-17
  • 1970-01-01
  • 2011-07-31
  • 2014-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-26
  • 1970-01-01
相关资源
最近更新 更多