css - 位置 : absolute causes overflow on the page

标签 css

简而言之,问题是:codepen.io/anon/pen/OBRoNW 当我将页面缩小到蓝色矩形+绿色矩形的大小时,底部的矩形将页面向下推,右侧的矩形将页面向下推页面向右移动,以便出现滚动条。当涉及到position:absolute元素时,如何防止推送?另外,如果可能的话,我该如何做相反的事情并将插入应用于左上角的元素?

问题本身(这还没有完全理解,所以我在顶部添加了更简单的问题): 当我使用绝对位置时,元素在页面主体中仍然占用空间。 例如:

  <div class="content">
    <div class="decoration">
    </div>
  </div>


<style>

.content{
  height:300px;
  width:300px;
  background:blue;
  position:relative;
}
.decoration{
  height:10px;
  width:10px;
  background:green;
  position:absolute;
  bottom:-5px;    
}

</style>

如果我将窗口大小调整到滚动条出现的位置,那么我将 .decoration 类设置为 bottom:0; 滚动条就会消失。 我期望的行为是页面在计算页面大小时忽略位置绝对元素,因为它们不应该占用任何空间。 因此,当窗口缩小时,它应该只显示一半的装饰。 这是一个错误吗?对此有已知的解决方案吗?

编辑:它似乎仅发生在底部定位

最佳答案

DOM 流忽略绝对定位的元素。整个页面占用的空间与 DOM 流不同。

换句话说:浏览器将尝试使访问者能够查看视口(viewport)之外的内容,即使它是绝对定位或固定的。

每当您将任何内容放置在视口(viewport)的右侧或底部“之外”时,您都应该期待滚动条。将内容放置在页面左侧之外不会导致滚动条,并且是下拉导航等内容的常用技巧。

您的原始示例:

.content {
  height: 300px;
  width: 300px;
  background: blue;
  position: relative;
}

.decoration {
  height: 10px;
  width: 10px;
  background: green;
  position: absolute;
  bottom: -10px;
}
<div class="content">
  <div class="decoration">
  </div>
</div>

左侧绝对定位示例:

.content {
  height: 300px;
  width: 300px;
  background: blue;
  position: relative;
}

.decoration {
  height: 10px;
  width: 10px;
  background: green;
  position: absolute;
  left: -999em;
}
<div class="content">
  <div class="decoration">
  </div>
</div>

关于css - 位置 : absolute causes overflow on the page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52683153/

相关文章:

javascript - 幻灯片图片消失

css - 萨斯/ bool 玛错误 : Error: File to import not found or unreadable: utilities/mixins. 萨斯

javascript - 表格内的下拉菜单 (CSS/HTML/Java)

css - 表中的链接不起作用

html - Chrome 在低 DPI 和高 DPI 显示器上以不同方式重绘滚动条上的 div

CSS 图标,我可以将一个类转换为另一个类吗

javascript - 使用 jQuery 或 Javascript 更改 svg 速率

jQuery 在每次输入提交时将列表项更紧密地附加在一起

html - ASP.NET MVC和VS2013结合使用什么技术构建多平台响应式网页设计(RWD)

php - 我在 PHP 中使用 CSS 边框属性,但它不起作用