【发布时间】:2020-01-18 23:02:33
【问题描述】:
我目前正在开发一个网站,我希望图像是其父级高度的 25%。
我查看了关于 SO 的以下答案以及其他网站上的一些其他类似答案:
- CSS square based on height
- Css div responsive square dependent on height
- https://css-tricks.com/aspect-ratio-boxes/
上述答案的问题是它要么使用视口单位(vh),要么是基于宽度的纵横比。就我而言,我想将一个元素设置为其父高度的 25%。我试过下面的代码,但它只是将它的宽度设置为 100%。
main {
--main-height: calc(100vh - var(--nav-height) - var(--body-top-padding));
min-height: var(--main-height);
}
section {
border: 1px solid red;
width: 100%;
min-height: calc(var(--main-height) / 2);
}
#profile > img {
border: 1px solid black;
height: 25%;
padding-left: 100%;
}
这里,#profile 是一个 ID 为 profile 的部分。
编辑
这就是我想要做的事情:
#profile > img {
border: 1px solid black;
--width: calc(0.25 * var(--main-height));
min-height: var(--width);
width: var(--width);
}
有没有更好的方法不使用 CSS 属性和计算?
【问题讨论】:
-
您可以发布您案例的完整代码吗?
#profile的风格是什么? -
@Nitheesh 更新了信息。
标签: html css aspect-ratio