简体   繁体   English

绝对定位清晰的页脚

[英]Absolute positioning clear footer

I have 2 absolutely positioned div s inside a relative container, I plan on using JavaScript to toggle visibility 我在相对容器中有2个绝对定位的div ,我打算使用JavaScript来切换可见性

 .container { position:relative; } .section1 { position:absolute; top:0; left:0; right:0; bottom:0; } .section2 { position:absolute; top:0; left:0; right:0; bottom:0; } .section1 .div1 { background:green; } .section1 .div2 { background:purple; } .section1 .div3 { background:brown; } .section1 .div4 { background:grey; } .section2 .div1 { background:pink; } .section2 .div2 { background:gold; } .section2 .div3 { background:blue; } .section2 .div4 { background:orange; } .footer { background:lightblue; min-height:100vh; } 
 <div class="container"> <div class="section1"> <div class="div1"> This is Item 1 </div> <div class="div2"> This is Item 2 </div> <div class="div3"> This is Item 3 </div> <div class="div4"> This is Item 4 </div> </div> <div class="section2"> <div class="div1"> This is Item 1 </div> <div class="div2"> This is Item 2 </div> <div class="div3"> This is Item 3 </div> <div class="div4"> This is Item 4 </div> </div> </div> <div class="footer"> Footer </div> 

They are working correctly, but my footer is not. 他们工作正常,但我的页脚不是。 Do I need to add a clear? 我需要添加清楚吗?

Your footer has min-height 100vh . 你的页脚min-height 100vh maybe you meant that for container? 也许你的意思是容器? Try removing that, and move 100vh to container class. 尝试删除它,并将100vh移动到容器类。

I would look at applying a position property to your footer and removing the overall height thats been set against it. 我会考虑将一个位置属性应用到页脚并删除对其设置的总高度。

Assuming you want your footer at the bottom, you can add the following CSS to it to help position it: 假设您希望页脚位于底部,您可以将以下CSS添加到其中以帮助定位:

.footer {
  background: lightblue;
  position:absolute;
  bottom:0;
  width:100%;
}

 body{ margin:0; } .container { position: relative; margin:0;padding:0; } .section1 { position: absolute; top: 0; left: 0; width:100%; } .section2 { position: absolute; top: 0; left: 0; width:100%; } .section1 .div1 { background: green; } .section1 .div2 { background: purple; } .section1 .div3 { background: brown; } .section1 .div4 { background: grey; } .section2 .div1 { background: pink; } .section2 .div2 { background: gold; } .section2 .div3 { background: blue; } .section2 .div4 { background: orange; } .footer { background: lightblue; position:absolute; bottom:0; width:100%; } 
 <div class="container"> <div class="section1"> <div class="div1"> This is Item 1 </div> <div class="div2"> This is Item 2 </div> <div class="div3"> This is Item 3 </div> <div class="div4"> This is Item 4 </div> </div> <div class="section2"> <div class="div1"> This is Item 1 </div> <div class="div2"> This is Item 2 </div> <div class="div3"> This is Item 3 </div> <div class="div4"> This is Item 4 </div> </div> </div> <div class="footer"> Footer </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM