html - 伪元素可以充当块元素而不重叠吗?
问题描述
div
我有三个 50x50 的彩色块,内容为 R、G 和 B。如果它们是三个彼此相邻的元素,这些块是否可以像预期的那样垂直排列?
如果使用与红色块相同高度的边距底部是可能的,但前提是红色块不包含任何文本。这是因为文本的高度被添加到边距的高度。
有没有办法只用 , 和 来完成.red
这项.red::after
工作.blue
?
body {
width: 100%;
height: 80vh;
display: flex;
align-items: center;
justify-content: center;
}
.demo-container {
display: flex;
align-items: center;
justify-content: center;
padding: 24px;
border: solid 1px #d5d9dc;
}
.red {
background: pink;
display: block;
min-width: 50px;
height: 50px;
position: relative;
}
.red::after {
content: "G";
background-color: rgba(64, 255, 64, 0.5);
display: block;
min-width: 50px;
height: 50px;
position: relative;
}
.blue {
background-color: rgba(64, 64, 255, 0.5);
display: block;
min-width: 50px;
height: 50px;
position: relative;
}
<!DOCTYPE html>
<div class="demo-container">
<div class="container">
<div class="red">R</div>
<div class="blue">B</div>
</div>
<br/>
</div>
解决方案
您可以添加position: relative;height: 100px
到.red
元素。
然后添加:position: absolute; bottom: 0;
到.red:after
元素
body {
width: 100%;
height: 80vh;
display: flex;
align-items: center;
justify-content: center;
}
.demo-container {
display: flex;
align-items: center;
justify-content: center;
padding: 24px;
border: solid 1px #d5d9dc;
}
.red {
background: pink;
display: block;
min-width: 50px;
height: 100px;
position: relative;
}
.red::after {
content: "G";
background-color: rgba(64, 255, 64, 0.5);
display: block;
min-width: 50px;
height: 50px;
position: absolute;
bottom: 0
}
.blue {
background-color: rgba(64, 64, 255, 0.5);
display: block;
min-width: 50px;
height: 50px;
position: relative;
}
<!DOCTYPE html>
<div class="demo-container">
<div class="container">
<div class="red">R</div>
<div class="blue">B</div>
</div>
<br/>
</div>
推荐阅读
- uml - 非软件上下文的 UML 类图的替代方案
- progressive-web-apps - 如何使用独立浏览器分离 PWA 会话和 cookie?(PWA 作为私人标签)
- java - 在实体类中选择属性的日期格式
- python - 如何在 django 模板标签中设置全局可访问的上下文变量?
- javascript - 以下两种实现有什么区别?
- html - Word Press 在移动设备主页上的静态页面全高
- python - 批量查找 20,000 个域的 Whois - 获取超时
- javascript - 想从地址栏获取准确的域名吗?
- python - OpenCV:自适应阈值错误(错误 -215)
- java - 制作扩展 AppCompatDialogFragment 的自己的警报对话框