html - 高度为 100% 的容器适合内容,未设置高度将高度设置为父级的 100%
问题描述
所以我有一个height
属性是css的案例。
根据[定义](https://www.w3schools.com/cssref/pr_dim_height.asp)一个容器height: 50%
将元素的高度设置为父元素高度的 50%
这里有 2 个代码片段,唯一的区别是 height 属性设置为 100% 或根本没有设置
设置高度属性: JSFIDDLE
.parent {
display: flex;
min-height: 300px;
background-color: grey;
padding: 30px;
}
.child {
position: relative;
background-color: red;
height: 100%;
}
.content {
margin: 20px;
height: 50px;
width: 100px;
background-color: blue;
}
<div class="parent">
<div class="child">
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>
</div>
</div>
没有设置高度属性(按预期工作):JSFIDDLE
.parent {
display: flex;
min-height: 300px;
background-color: grey;
padding: 30px;
}
.child {
position: relative;
background-color: red;
}
.content {
margin: 20px;
height: 50px;
width: 100px;
background-color: blue;
}
<div class="parent">
<div class="child">
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>
</div>
</div>
对我来说,这与 height 属性的定义相反,怎么会?我注意到,如果没有display: flex
父母,行为是不同的,但是height: 100%
什么也不做。
解决方案
推荐阅读
- python - 如何从路径字符串中获取文件夹名称并将其添加到熊猫数据框中的新列?
- mysql - 表 1 和表 2,我应该使用什么连接。另外,如果有人可以解释加入会很棒
- android - 如何使用 ParameterResolver 在 JUnit 5 中注入多个扩展值
- android - 如何在 Activity(基本 java 文件)中访问 Recycler View 的布局?
- python - Don't understand why my code giving me an IndexError
- openpyxl - 无法使用openpyxl在连接公式中的第一项周围插入引号
- sql - 计算变量出现的次数
- html - 添加边距时如何解决掉框问题
- r - 当列包含“NA”时如何使用 Plotly 修复不正确的颜色
- vue.js - 当 prop 对象属性的值发生变化时,具有 v-once 的组件仍会重新渲染