Zlog
首页时间轴关于
Zlog

一个探索技术、编程和构建 Web 的个人空间。

© 2026 Zlog

导航

首页时间轴关于

链接

首页/position

position

.top 元素距离其在正常文档流中的位置 top: 10px

Z

Zephyr110

2019年12月28日·1 分钟
|
frontendfrontend-css

1. 属性

  • static
“

默认值,没有定位,元素出现在正常的文档流中, top, right, bottom, left 不生效

  • relative
“

生成相对定位元素,相对于其在正常文档流中的位置定位,元素没有脱离文档流

  • absolute
“

生成绝对定位元素,相对于其最近属性不为static的父元素进行定位,元素脱离文档流

  • fixed
“

生成绝对定位元素,相对于浏览器窗口进行定位

  • sticky
“

粘性定位,该定位基于用户滚动的位置。它的行为就像 position:relative; 而当页面滚动超出目标区域时,它的表现就像 position:fixed;,它会固定在目标位置。

2. 示例

html
  <body>
    <div class="top">
      <div class="mid">
        <div class="foo"></div>
      </div>
    </div>
  </body>
css
* {
    margin: 0;
    padding: 0;
}
body {
  height: 1000px;
}
.top,
.mid,
.foo {
  width: 200px;
  height: 200px;
}
 
.top {
  background-color: #b61b1b;
  position: absolute;
  top: 10px;
}
 
.mid {
  background-color: #0eb13f;
  position: fixed;
  top: 30px;
}
 
.foo {
  background-color: #d4f080;
  position: absolute;
  top: 200px;
}
 

.top 元素距离其在正常文档流中的位置 top: 10px
.mid 元素相对窗口顶部 top: 30px 滑动窗口不会变
.foo 元素相对于.mid top: 200px,但受到 .mid 的 fixed属性 影响,自身也有了fixed行为

标签

frontendfrontend-css

评论

相关文章

Facade

外观模式:就是提供一个统一的接口去访问多个子系统的多个不同的接口,为子系统中的一组接口提供统一的高层接口。使得子系统更容易使用,不仅简化类中的接口,而且实现调用者和接口的解耦。

2021年12月26日·2 分钟

Singleton

单例模式:就是在整个运行时域,一个类只有一个实例对象。

2021年12月26日·2 分钟

Function Overloading

函数名相同,函数的参数列表不同(参数个数、参数类型),根据参数的不同之行不同的操作。

2021年12月19日·2 分钟

← 返回文章列表