html - CSS Flexbox - 将所有相对于父元素的全高子元素居中
问题描述
我需要将所有子元素与父元素的全高对齐。
一切都很好,直到我使用align-items: center; 属性,我对齐元素,但它不覆盖 100%,如果我使用align-self:stretch; 覆盖 100% 但不垂直对齐元素,对此有一些解决方案。
.abouts {
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: -o-flex;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
align-self: stretch;
}
.abouts .item {
flex: 1;
margin: 5px;
padding: 15px;
background-color: rgba(0,0,0,0.1);
}
<div class="abouts">
<!-- ITEM -->
<div class="item">
<h3>Vision</h3>
<p>Content here details, more content height...</p>
</div>
<!--/ ITEM -->
<!-- ITEM -->
<div class="item">
<h3>Vision</h3>
<p>Content here details...</p>
</div>
<!--/ ITEM -->
<!-- ITEM -->
<div class="item">
<h3>Vision</h3>
<p>Content here details, more content for test equal height all elements more content for test equal height all elements...</p>
</div>
<!--/ ITEM -->
</div>
解决方案
让.abouts
flex 父级align-items: stretch
用来让.item
子级采用父级的高度。然后(你可以)让.item
孩子们自己成为灵活的父母。然后,您必须调整文本边距(因为 flex 格式中没有边距折叠)
.abouts .item {
flex: 1;
margin: 5px;
padding: 15px;
background-color: rgba(0, 0, 0, 0.1);
display: flex;
flex-direction: column;
justify-content: center;
}
.abouts {
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: -o-flex;
display: flex;
justify-content: center;
align-items: stretch;
text-align: center;
align-self: stretch;
}
.abouts .item {
flex: 1;
margin: 5px;
padding: 15px;
background-color: rgba(0, 0, 0, 0.1);
display: flex;
flex-direction: column;
justify-content: center;
}
<div class="abouts">
<!-- ITEM -->
<div class="item">
<h3>Vision</h3>
<p>Content here details, more content height...</p>
</div>
<!--/ ITEM -->
<!-- ITEM -->
<div class="item">
<h3>Vision</h3>
<p>Content here details...</p>
</div>
<!--/ ITEM -->
<!-- ITEM -->
<div class="item">
<h3>Vision</h3>
<p>Content here details, more content for test equal height all elements more content for test equal height all elements...</p>
</div>
<!--/ ITEM -->
</div>
推荐阅读
- typescript - 错误 TS2352:“会话 | 类型的转换” null' 输入 '{ x: string; y:字符串;}' 可能是一个错误
- haskell - 为什么 Element 在 ClassyPrelude 中使用 Foldable(不是 MonoFoldable)会出现类型错误?
- python - 自动设置注册表值(executionpolicy),以便我可以通过 Python 运行 powershell 脚本
- azure - Microsoft 托管代理上的 Az powershell 模块可用性
- html - CSS 样式不影响子元素(按钮)
- powershell - 只能进行 10 次试运行?
- c# - JTable 不显示排序或分页
- android - 使用 MediaCodec、Media Extractor 和 Media Muxer 在 Android 上进行视频修剪
- r - 导入多个 Excel 工作簿,每个工作簿都包含 R 中的多个工作表
- symfony4 - 使用 VichUploaderBundle 通过 API 上传文件