Margin bottom在%100 div上不起作用



我有以下html代码:

<html>
 <body style="margin:0px; padding:0px;">
   <div id="outer" style="height:100%;">
     <div id="header" style="height:40px; background:blue;">Header</div>
     <div id="main" style="height:100%; margin-bottom:-40px; background:red; overflow:auto;">
        <p style="height:1000px">Main</p>
     </div>
   </div>
 </body>
</html>

我只希望垂直滚动显示在主div内的内容超过可视区域时,似乎主div上的margin-bottom不起作用。

有人能帮我解决这个问题吗?

实际上,你似乎在解决错误的问题。如果你只是想去掉body本身的滚动条,将body的样式设置为overflow:hidden

<html>
  <body style="margin:0px; padding:0px;overflow:hidden;">
   <div id="outer" style="height:100%;">
     <div id="header" style="height:40px; background:blue;">Header</div>
     <div id="main" style="height:100%; margin-bottom:-40px; background:red; overflow:auto;">
        <p style="height:1000px">Main</p>
     </div>
   </div>
 </body>
</html>

这样就可以解决边距问题了,然后你所要做的就是保持尺寸合适。

最新更新