html - 相对定位的元素不占用其子元素的高度
问题描述
我正在使用带有以下html和scss的css构建一个小进度条(它只是scss,因为我正在使用嵌套)
html:
<div class="first">
<div class="first__progress-bar-container"/>
<div class="first__progress-bar"/>
<div class="first__content-section">
<div>content</div>
<div>content</div>
<div>content</div>
</div>
</div>
<div class="second">more content</div>
scss:
.first {
position: relative;
height: 10px;
width: 1000px;
&__progress-bar-container {
position: absolute;
background: #eee;
height: 10px;
width: 100%;
z-index: 1
}
&__progress-bar {
position: absolute;
z-index: 2;
height: 10px;
background: linear-gradient(to right, red, blue);
border-radius: 0 5px 5px 0;
width: 50%;
}
}
也可以使用这个codepen
如果我在浏览器中看到结果,文本"more content"
就位于所有.content-section
内容之上。我预计这.second
会出现在它之后.content-section
而不是在它之上。
为什么子元素不.content-section
被评估为父元素的高度.first
?
后续问题是,我怎样才能让它"more content"
出现在第一部分内容之后而不是在它之上?
解决方案
我已经稍微更改了标记,假设您想将进度条放在进度条容器中。如果进度条位于进度条容器内,则不再需要相对定位和绝对定位。从 .first 中删除了高度,以便 .first 的大小是内容的大小。
SCSS:
.first {
width: 1000px;
background-color:grey;
&__progress-bar-container {
background: #eee;
height: 10px;
width: 100%;
}
&__progress-bar {
height: 10px;
background-image: linear-gradient(to right, red, blue);
border-radius: 0 5px 5px 0;
width: 50%;
}
}
HTML:
<div class="first">
<div class="first__progress-bar-container">
<div class="first__progress-bar"></div>
</div>
<div class="content-section">
<div>content</div>
<div>content</div>
<div>content</div>
</div>
</div>
<div class="second"> more content</div>
推荐阅读
- c++ - C ++如何告诉开发人员实现某事。设计模板时(如 Java 接口)
- reactjs - 如何在 onClick 函数中将繁忙的光标添加到 reactstrap 模态按钮。该功能是一个承诺
- node.js - 如何在 express/node.js 应用程序中使用 mailgun/mailchimp/etc 发送电子邮件
- jquery - 使用 CSS 或 Jquery 在 div 中删除时间文本的最佳方法是什么
- java - 如何通过 Hibernate 中的两个属性删除实体?
- javascript - 如何使用 Web Audio API 创建 .wav 文件?
- php - PHP 在删除自动加载器时 Composer 无法完成这项工作
- html - 我怎样才能建立一个任何访问者都可以编辑的字段的网站?
- arrays - 在 Swift 中添加到数组
- javascript - 单击时,类中使用的 line-through 不执行任何操作