好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

理解css中的position五个属性_html/css_WEB-ITnose

在实际开发页面布局时,运用position,对定位的块级元素的嵌套的效果总是不太理解,这里做了几个测试

一般的在w3c中我们可以很容易的获取定义:

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

fixed :生成绝对定位的元素,相对于浏览器窗口进行定位。元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

inherit :规定应该从父元素继承 position 属性的值。

absolute : 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

relative : 生成相对定位的元素,相对于其正常位置进行定位。因此,"left:20" 会向元素的 LEFT 位置添加 20 像素。

总的来说 :

static 呢,就是正常的文档流顺序,默认的,相当于没有定位!

fixed 呢, 就是相对于浏览器窗口,就是你滚动条怎么滚动,他还是那个位置,就想是 “粘” 在窗口上了!

   inherit 呢, 就是从父元素继承 position 属性的值,

absolute 呢,是脱离文档流的原来的位置是不继续占据了,如果他的父级元素中有已经定位了的不管是absolute的还是relative,它都会相对于他的父级元素来定位,如果他的父级元素中没有定位了的那么它就是相对于body来定位的。也就是说absolute的绝对是有参照物的!

relative 呢,是不会脱离文档流的原来的位置也就继续占据了,它是只相对于自身原来的位置来定位的!

  前三个是很容易理解的,对于absolute和relative的结合使用,做了几个测试

测试(absolute和relative)

  1.单独的absolute和relative

  2.relative中的relative,absolute中的relative

  3.absolute中的absolute,relative中的absolute

  

   	 position -- absolute -- relative   		.test-a{		position: absolute;		top:20px;		left:60px;		width:200px;		height: 100px;		background: red;	}	.test{		width:400px;		height: 100px;		background: green;	}	.test-r{		position:relative;		top:50px;		left:130px;		background: yellow;		width:160px;		height: 180px;	}	.test-rr{	    position: relative;	    top: 20px;	    left: 100px;	    width: 600px;	    height: 300px;	    background: blue;	}	.test-aa{		position: absolute;	    top: 24px;	    left: 34px;	    background: orange;	}	.test-aaa{		position: absolute;	    top: 24px;	    left: 34px;	    width:400px;	    height:200px;	    background: #18E457;	}	.test-aaaa{		position: absolute;	    top: 124px;	    left: 134px;	    width:400px;	    height:200px;	    background: yellow;	}	.test-rrr{		position: relative;	    top: 24px;	    left: 34px;	    width:400px;	    height:200px;	    background: yellow;	}	.test-rrr{		position: relative;	    top: 124px;	    left: 134px;	    width:400px;	    height:200px;	    background: red;	}	.test-r-a{		position: absolute;	    top: 124px;	    left: 134px;	    width:800px;	    height:800px;	    background: yellow;	}	.test-a-r{		position: relative;	    top: 124px;	    left: 134px;	    width:700px;	    height:700px;	    background: red;	}  	

absolute

查看更多关于理解css中的position五个属性_html/css_WEB-ITnose的详细内容...

  阅读:37次