html - 位置 :absolute causes horizontal scrollbar

标签 html css

绝对定位(侧面黄色广告)div 在调整窗口大小时(尺寸减小)超过它们时会导致不需要的水平滚动条。只有当窗口小于主 #container 时滚动条才会出现,并且这些广告 div 不应影响布局。他们是否被覆盖并不重要。

HTML:

<div id='topbar'>
    <div id='menu'> <a href='#'>Link1</a>
 <a href='#'>Link2</a>
 <a href='#'>Link3</a>
 <a href='#'>Link4</a>

    </div>
</div>
<div id='container'>
    <div id='pushfix'></div>
    <div id='ad_container'>
        <div id='ad1'>ad</div>
        <div id='ad2'>ad</div>
    </div>
Lorem ipsum placeholder text
</div>

CSS:

body, html {
    height:100%;
}
body {
    margin:0;
}
#topbar {
    width:100%;
    background-color:#DCDCDC;
    height:40px;
    position:absolute;
}
#menu {
    width:250px;
    background-color:#B3B3B3;
    margin:0 auto;
    height:100%;
    text-align:center;
    line-height:40px;
}
#menu a {
    color:#fff;
}
#container {
    height:100%;
    background-color:#808080;
    width:240px;
    padding:0 5px;
    margin:0 auto;
}
#pushfix {
    height:40px;
}
#ad_container {
    position:relative;
    width:240px;
}
#ad_container div {
    width:100px;
    background-color:yellow;
    height:300px;
    position:absolute;
}
#ad1 {
    left:-105px;
}
#ad2 {
    right:-105px;
}

Exact layout replica: http://jsfiddle.net/8UkQA/

最佳答案

出于某种原因,扩展到 body 边界之外的绝对定位元素似乎会导致出现滚动条。您可以通过简单地将 body 标记内的所有内容包装在一个相对定位的 div 中来解决这个问题,该 div 样式为 overflow: hidden;。超出此容器边界的绝对定位内容不会导致窗口出现滚动条。

这是一个工作示例:http://jsfiddle.net/8UkQA/1/

关于html - 位置 :absolute causes horizontal scrollbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19308257/

相关文章:

html - 如何将固定位置的 flex 容器覆盖在另一个 flex 容器上?

javascript - 使用 jquery 将文本更改为 h1?

html - 如何根据其base64编码的背景数据比例为<img>设置宽度和高度?

css - 多少 CSS 是太多的 CSS?

javascript - 想要使用 cssSelector 聚焦元素。我能怎么做?

javascript - 在 HTML5 中创建框架

html - 使 div 占用所有剩余空间

html - 存储限制(在 IndexedDB 或 localStorage 上)是否适用于在计算机外运行的本地应用程序?

html - Bootstrap 导航栏图标栏不起作用

html - 如何在 Bootstrap-vue 或 Bootstrap 4 的输入组左侧添加 Font Awesome 图标?