html - 高度为 100% 时如何阻止弹性项目溢出父项
问题描述
如果您查看示例代码段,我正在尝试#feature_header_container
(以红色)扩展以填充剩余的高度,header
但是因为我在里面有其他内容header
,<p>
如图所示,当我设置它height: 100%
时#feature_header_container
它会溢出header
但我希望它只扩大 的剩余高度header
,我该怎么做?
现在,#feature_header_container
(红色)在外部溢出header
并被<p>
内部的第一个元素向下推header
。
*
{
font-family: Arial;
box-sizing: border-box;
}
html, body
{
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
header
{
display: block;
height: 100%;
min-height: 100vh;
background-color: #21D4FD;
background-image: linear-gradient(19deg, #21D4FD 0%, #B721FF 100%);
-webkit-box-shadow: 0px 10px 14px -6px rgba(0,0,0,0.57);
box-shadow: 0px 10px 14px -6px rgba(0,0,0,0.57);
border-radius: 0 0 80px 80px;
padding-left: 10%;
padding-right: 10%;
}
#p_test
{
display: inline-block;
}
#feature_header_container {
display: flex;
height: 100%;
margin-top: 20px;
background-color: red;
align-items: center;
}
#feature_container {
display: inline-flex;
flex-grow: 1;
justify-content: center;
background-color: blue;
color: white;
}
.feature_box {
background-color: pink;
}
<html>
<body>
<header>
<p id="p_test">
Test Test Test
</p>
<div id="feature_header_container">
<div id="feature_container">
<div class="feature_box">
<p>Test</p>
</div>
<div class="feature_box">
<p>Test</p>
</div>
<div class="feature_box">
<p>Test</p>
</div>
</div>
</div>
</header>
<main>
<p>TesttestTesttestTesttestTesttestTesttest</p>
</main>
</body>
</html>
解决方案
感谢遇到这个小提琴:https ://jsfiddle.net/MadLittleMods/LmYay/
我设法解决了我的问题并有一个工作示例:https ://jsfiddle.net/p459qvfk/2/
工作示例复制过来:
* {
font-family: Arial;
box-sizing: border-box;
}
html,
body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
header {
display: flex;
flex-direction: column;
justify-content: flex-start;
/* align items in Main Axis */
align-items: stretch;
/* align items in Cross Axis */
align-content: stretch;
/* Extra space in Cross Axis */
min-height: 100vh;
background-color: #21D4FD;
background-image: linear-gradient(19deg, #21D4FD 0%, #B721FF 100%);
-webkit-box-shadow: 0px 10px 14px -6px rgba(0, 0, 0, 0.57);
box-shadow: 0px 10px 14px -6px rgba(0, 0, 0, 0.57);
border-radius: 0 0 80px 80px;
padding-left: 10%;
padding-right: 10%;
}
#p_test {
display: inline-block;
}
#feature_header_container {
display: flex;
flex: 1;
background-color: red;
align-items: center;
}
#feature_container {
display: inline-flex;
flex: 1;
justify-content: center;
background-color: blue;
color: white;
}
.feature_box {
background-color: pink;
}
<html>
<body>
<header>
<p id="p_test">
Test Test Test
</p>
<p id="p_test">
Test Test Test
</p>
<div id="feature_header_container">
<div id="feature_container">
<div class="feature_box">
<p>Test</p>
</div>
<div class="feature_box">
<p>Test</p>
</div>
<div class="feature_box">
<p>Test</p>
</div>
</div>
</div>
</header>
<main>
<p>TesttestTesttestTesttestTesttestTesttest</p>
</main>
</body>
</html>
推荐阅读
- web-services - Prestashop 1.6 Webservices:如何获取产品的附件?
- sublimetext3 - Sublime Text 3 粘性拆分
- firebase - 如何将 Firebase 地图转换为飞镖地图?
- azure - 无法导入自动解释器
- c++ - 即使为其相关的 dll 正确生成了 lib 文件,CMake 生成的 MSVC 项目也找不到符号
- javascript - 引导程序 4 - javascript 主文件
- java - 使用 inputMap 过滤列表(List1)中的数据(键和值可能因每个请求而异)
- google-apps-script - 谷歌应用脚本日期解析出错
- rest - 用于搜索的 RESTful URL 样式指南
- angular - Angular 8 - 从拦截器内导航后的代码