css定位

1. 基本属性

属性
inherit 规定应该从父元素继承 position 属性的值
static 默认值,没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)
relative 生成相对定位的元素,相对于元素本身正常位置进行定位,因此,left:20px 会向元素的 left 位置添加20px
absolute 生成绝对定位的元素,相对于static定位以外的第一个祖先元素(offset parent)进行定位,元素的位置通过 left, top, right 以及bottom属性进行规定
fixed 生成绝对定位的元素,相对于浏览器窗口进行定位。元素的位置通过 left, top, right 及 bottom 属性进行规定
sticky CSS3新属性,表现类似position:relative和position:fixed的合体,在目标区域在屏幕中可见时,它的行为就像position:relative; 而当页面滚动超出目标区域时,它的表现就像position:fixed,它会固定在目标位置

注意:sticky兼容性特别不好,不推荐使用.效果可以用javascript来实现.

2.普通流与相对定位

CSS有三种基本的定位机制:普通流,相对定位和绝对定位

  • 普通流是默认定位方式,在普通流中元素框的位置由元素在html中的位置决定,元素position属性为static或继承来的static时就会按照普通流定位,这也是我们最常见的方式
  • 相对定位比较简单,对应position属性的relative值,如果对一个元素进行相对定位,它将出现在他所在的位置上,然后可以通过设置垂直或水平位置,让这个元素相对于它自己移动,在使用相对定位时,无论元素是否移动,元素在文档流中占据原来空间,只是表现出来的位置会改变

普通流

<div style="border: solid 1px #0e0; width:200px;">
<div style="height: 100px; width: 100px; background-color: Red;"> </div>
<div style="height: 100px; width: 100px; background-color: Green;"> </div>
<div style="height: 100px; width: 100px; background-color: Red;"> </div>
</div>

![Paste_Image.png](http://upload-images.jianshu.io/upload_images/4585153-0fe9fdb2d6ac7a27.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

**相对定位**
>```
 <div style="border: solid 1px #0e0; width:200px;">
      <div style="height: 100px; width: 100px; background-color: Red;"> </div>
      <div style="height: 100px; width: 100px; background-color: Green; position:relative; top:20px; left:20px;"></div>
      <div style="height: 100px; width: 100px; background-color: Red;"></div>
  </div>
Paste_Image.png

3.绝对定位与固定定位

  • 相对定位可以看作特殊的普通流定位,元素位置是相对于它在普通流中位置发生变化,而绝对定位使元素的位置与文档流无关,也不占据文档流空间,普通流中的元素布局就像绝对定位元素不存在一样

  • 绝对定位的元素的位置是相对于距离最近的非static祖先元素
    位置决定的。如果元素没有已定位的祖先元素,那么他的位置就相对于初始包含块html来定位demo

  • 因为绝对定位与文档流无关,所以绝对定位的元素可以覆盖页面上的其他元素,可以通过z-index
    属性控制叠放顺序,z-index越高,元素位置越靠上。

absolute绝对定位

<div style="border: solid 1px #0e0; width:200px; position:relative;">
<div style="height: 100px; width: 100px; background-color: Red;"></div>
<div style="height: 100px; width: 100px; background-color: Green; position:absolute; top:20px; left:20px;"></div>
<div style="height: 100px; width: 100px; background-color: Yellow;"></div>
</div>

![Paste_Image.png](http://upload-images.jianshu.io/upload_images/4585153-20321a76c2e5add8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

**fixed固定定位**
固定定位是绝对定位的一种,固定定位的元素也不包含在普通文档流中,差异是固定元素的包含块儿是视口(viewport)
>```
 <div style="border: solid 1px #0e0; width:200px;">
      <div style="height: 100px; width: 100px; background-color: Red;"></div>
      <div style="height: 100px; width: 100px; background-color: Green; position:fixed; bottom:20px; left:20px;"></div>
      <div style="height: 100px; width: 100px; background-color: Yellow;"></div>
  </div>
Paste_Image.png

绝对定位宽度
绝对定位宽度是收缩的,如果想撑满父容器,可以设置 width: 100%

<div style="position: absolute; background: red">hello 饥人谷</div>

绝对定位和 BFC
绝对定位能形成 BFC 可用来清除浮动 可用来阻止外边距合并

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • CSS 定位 CSS有三种基本的定位机制:普通流,浮动,绝对定位(absolute, fixed):普通流是默认定...
    _空空阅读 11,097评论 0 15
  • 在CSS中关于定位的内容是:position:relative | absolute | static | fix...
    feelinghappy618阅读 3,846评论 0 0
  • 当在这一个页面上实现布局和定位有几种不同的技术。使用哪种技术,很大程序上取决于内容和目标页面,因为有很多技术比别人...
    lulu_c阅读 4,827评论 0 5
  • http://www.jianshu.com/p/4a1f36e9ca32参考链接 1.谁受浮动的影响,就让谁清除...
    温柔你要送嘻嘻阅读 1,342评论 0 0
  • 定位 定位概况 CSS 有三种基本的定位机制:普通流、浮动和绝对定位。除非专门指定,否则所有框都在普通流中定位。 ...
    xiaolizhenzhen阅读 2,910评论 0 0

友情链接更多精彩内容