position

position我称它为三部曲css

一部曲;position;顾名思义就是定位它呢有5个属性值html

css3

描述浏览器

Absolute  post

生成绝对定位的元素,相对于 static 定位之外的第一个父元素进行定位。htm

元素的位置经过 "left", "top", "right" 以及 "bottom" 属性进行规定。继承

fixed    文档

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

元素的位置经过 "left", "top", "right" 以及 "bottom" 属性进行规定。io

relative  

生成相对定位的元素,相对于其正常位置进行定位。

所以,"left:20px" 会向元素的 LEFT 位置添加 20 像素。

Static   

默认值。没有定位,元素出如今正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。

Inherit

继承父元素

 二部曲;说下postion的用法absolute是相对于有相对定位的父元素定位,若是没有它会逐级查找直达body,下面的盒子是最上面的。它呢是会脱离文档流的!

fixed是绝对定位生成相对于窗口定位;

三部曲;是定位的层级关系关键属性 z-index ;

z-index;这个属性只能在 postion属性下使用默认是 0 或 auto 参数能够是正整数 和负整数 0!

元素的z-index的值越大它的层级就越高 下面我拿案例来讲明

<html> <head> <meta charset="UTF-8"> <title></title> </head> <style type="text/css"> /*首先干掉默认值*/ *{ margin: 0; padding: 0; list-style: none; text-decoration: none; } /*再给每一个盒子宽高 边框 和绝对定位*/ div{ width: 200px; height: 200px; border: 1px solid red; position: absolute; } /*而后给每一个盒子不一样的背景颜色 和 left top 的值我用的是css3的伪类选择器*/ div:nth-of-type(1){ background: blue; z-index: 200; } div:nth-of-type(2){ background: green; left: 22px; z-index: 10; } div:nth-of-type(3){ top: 22px; background: red; z-index: -200; } </style> <body> <div></div> <!-- 做者:1693405836@qq.com 时间:2016-09-07 描述:首先写出三个盒子而后全给position --> <div></div> <div></div> </body></html>

相关文章
相关标签/搜索