[英]How do I place divs on top of other divs?
I am trying to place my leftbody and rightbody over top of my MidBody but it doesnt seem to work.我试图将我的 leftbody 和 rightbody 放在 MidBody 的顶部,但它似乎不起作用。 I thought placing the Midbody to relative and the left and right bodies to absolute with a z-index would help but it doesnt.我认为使用 z-index 将 Midbody 置于相对位置,将左右主体置于绝对位置会有所帮助,但事实并非如此。 So i am clueless right now.所以我现在一窍不通。 Any help would be greatly appreciated.任何帮助将不胜感激。
.Header { background-color: #CCCCCC; width: calc(100%-16px); height: 100px; border-radius: 5px; } .MidBody { background-color: #141414; width: calc(100%-16px); height: 850px; margin-top: 3px; border-radius: 5px; position: relative; } .footer { background-color: #CCCCCC; width: calc(100%-16px); height: 50px; margin-top: 5px; border-radius: 5px; } #leftbody { background-color: #F1F1F1; width: calc(50%-16px); height: 425px; float: left; margin-left: 3px; position: absolute; z-index: 1; } #rightbody { background-color: #F1F1F1; width: calc(50%-16px); height: 425px; float: right; position: absolute; z-index: 1; }
<div class="Header"></div> <div class="MidBody"> <div id="leftbody"></div> <div id="rightbody"></div> </div> <div class="footer"></div>
I changed我变了
float:left; -> left:0;
float:right; -> right:0;
and和
width:calc(50%-16px); -> width:50%;
Final css :最终的CSS:
.Header { background-color:#CCCCCC; width: calc(100%-16px); height: 100px; border-radius: 5px; } .MidBody { background-color:#141414; width: calc(100%-16px); height: 850px; margin-top:3px; border-radius: 5px; position: relative; } .footer { background-color:#CCCCCC; width:calc(100%-16px); height: 50px; margin-top: 5px; border-radius: 5px; } #leftbody { background-color:#F1F1F1; width:50%; height:425px; left:0; margin-left: 3px; position: absolute; z-index: 9999; } #rightbody { background-color:#F1F1F1; width:50%; height:425px; right:0; position: absolute; z-index: 9999; }
<div class="Header"></div> <div class="MidBody"> <div id="leftbody"></div> <div id="rightbody"></div> </div> <div class="footer"></div>
Use position: fixed;
使用position: fixed;
instead of position: absolute;
而不是position: absolute;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.