html - 无法匹配 <div> 高度

标签 html css

我有一个父级设置为 height:0padding-bottom:100% 以保持正方形纵横比。在 parent 内部,我想要另一个占 parent 高度 80% 的 div。

<div class="parent" style="width:100%; height:0; padding-bottom:100%">
   <div class="child" style="width:100%; height: 80%"></div>
</div>

我知道 80% 的高度是相对于其父项的高度,并且将父项的高度设置为 0,子项的高度会受到影响是合乎逻辑的。是否有任何解决方法可以在保持父级的纵横比的同时保持操纵子级高度的能力?

最佳答案

你可以绝对定位 child :

<div class="parent" style="width:100%; height:0; padding-bottom:100%">
    <div class="child" style="position: absolute; left: 0; top: 10%; right: 0; bottom: 10%"></div>
</div>

关于html - 无法匹配 <div> 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54774537/

相关文章:

html - 导航栏不改变颜色

html - 第一个 child 和最后一个 child 在不同的容器中

html - 将鼠标悬停在文本上以淡化 CSS 中的菜单

javascript - 使用 JQuery 迭代 HTML 表(工作时间)的问题

html - 定义海报属性和预加载后视频消失

php - 如何从 Bootstrap Modal 表单获取值?

jquery - 提高 jQuery 拖动动画的性能

css - 包装不居中

jquery - 在文本区域的更改事件上使用 Canvas 上的 fabric.js 添加文本

html - Heroku - CSS 不在单个页面上加载,而是在其他任何地方加载