html - 使用 z-index 时的流体测量
问题描述
我当前的尝试基于此线程 https://intellipaat.com/community/22509/how-to-overlay-one-div-over-another-div
但我的问题是,当我像在另一个线程中使用固定测量值时,虽然我可以让两层的宽度和高度完全相同,但我无法让顶层的高度满足底部的高度当我将宽度切换到包装器 div 的 95% 时的图层。
这是我的固定测量代码:
.banner {
margin: 0 auto;
width: 500px;
height: 500px;
position: relative;
}
.backplane, .frontplane {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
.backplane {
border: 10px solid black;
}
.frontplane {
border: 10px solid blue;
border-radius: 50px;
background-opacity: 0;
text-align: center;
z-index: 10;
}
h2 {
font-size: 3em;
line-height: 0;
text-align: center;
}
这是用于 95% 宽度包装器的流体测量的 CSS。如果我向 .backplane, .frontplane {} 类添加 100% 的高度,则前面板的视图会变得混乱。
.banner {
margin: 0 auto;
width: 95%;
position: relative;
}
.backplane, .frontplane {
width: 100%;
position: absolute;
top: 0;
left: 0;
}
.backplane {
border: 10px solid black;
}
.frontplane {
border: 10px solid blue;
border-radius: 50px;
background-opacity: 0;
text-align: center;
z-index: 10;
}
h2 {
font-size: 3em;
line-height: 0;
text-align: center;
}
这是我的 HTML
<div class="banner">
<div class="backplane" id="back">
<img src="background.jpg" width="100%" alt=" ">
</div>
<div class="frontplane" id="front">
<img src="mars.jpg" width="30%" margin="0 auto" alt="Mars Logo">
<h2>Hello World!</h2>
</div>
</div>
我想也许可以使用 JavaScript 来获取流体背板的高度并将其分配给前板,如下所示:
document.getElementById("front").height() = document.getElementById("back").height();
但这似乎无济于事。
这是第一个具有固定测量值的示例的屏幕截图
这是第二个流体测量示例的屏幕截图。两层的宽度都正确,但高度不正确。蓝色边框显示高度在哪里。它需要匹配黑色边框。
有人可以建议一种使公会高度正常工作的方法吗?那将不胜感激。
解决方案
为了相对设置元素的尺寸(x%)。父级 (.banner) 用于此目的。由于您position: absolute;
在正面和背面进行设置,因此没有更多内容可以防止父级折叠。基本上你正在阅读一个空 div 的高度。
通过将背面保留为默认位置,.banner
具有防止其折叠的内容。
.banner {
margin: 0 auto;
width: 95%;
position: relative;
}
/* .backplane, */
.frontplane {
/* width: 100%; */
position: absolute;
}
.backplane {
border: 10px solid black;
}
.frontplane {
border: 10px solid blue;
border-radius: 50px;
/* background-opacity: 0; /* this does not exist use: background-color: transparent; */
text-align: center;
z-index: 10;
/* tie to perimeter */
top: 0;
right: 0;
left: 0;
bottom: 0
}
h2 {
font-size: 3em;
line-height: 0;
text-align: center;
}
<div class="banner">
<div class="backplane" id="back">
<img src="https://dummyimage.com/600x400/DDD/EEE&text=back" width="100%" alt=" ">
</div>
<div class="frontplane" id="front">
<img src="https://dummyimage.com/300x200/CCC/777&text=front" width="30%" margin="0 auto" alt="Mars Logo">
<h2>Hello World!</h2>
</div>
</div>
推荐阅读
- firebase - 在循环中通过文档 ID 获取一堆文档是否有效?
- css - 我如何检查规范化的 css 是否在 angularjs 中工作?
- optimization - 使用 CVXPY 进行市场中性优化
- r - 在不合并的情况下提取值并找到最小值
- php - VSCode - PHP单元测试?
- wordpress - 抱歉,无法购买此产品。电子商务
- php - sqlsrv_query 不返回任何内容
- apache - 为什么 Apache QPID 代理队列声明参数“x-qpid-dlq-enabled”不能正常工作?
- sql - 在 DB2 中将时间转换为 24 小时制
- aws-fargate - 使用 CDK 使用正确的端口注册 Fargate 服务 ELB 目标?