position定位
2016-11-01 17:35
169 查看
position有三种定位方式:静态定位,相对定位和绝对定位
它的属性值有四种:static,relative,absolute,fixe
position:relative;
相对定位的特点:相对于自身原有位置进行偏移,随即拥有偏移属性和z-index属性,处于标准文档流当中
}.box3{ background:blue;height:100px;}</style></head><body><div class="box1">box1</div><div class="box2">box2</div><div class="box3">box3</div></body></html>
相对定位的效果如下图所示:
position:absolute;
绝对定位的特点:建立了以包含块为基准的定位;完全脱离了标准文档流;随即拥有偏移属性和z-index属性
(1)未设置偏移量:
它会保持在初始位置,效果有点像浮动,会脱离标准文档流,并且在没有设置宽度的情况下,宽度会根据填充内容的宽度而改变
(2)有偏移量
1.没有已经定位的祖先元素,会以<html>为偏移的参照基准
2.有已经定位的祖先元素,会以离它最近的祖先元素为偏移的参照基准
它的属性值有四种:static,relative,absolute,fixe
position:relative;
相对定位的特点:相对于自身原有位置进行偏移,随即拥有偏移属性和z-index属性,处于标准文档流当中
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <style> .box1{ background:red;height:100px; } .box2{ background:yellow;height:200px; <pre name="code" class="html"> position:relative; top:50px; left:50px;
}.box3{ background:blue;height:100px;}</style></head><body><div class="box1">box1</div><div class="box2">box2</div><div class="box3">box3</div></body></html>
相对定位的效果如下图所示:
position:absolute;
绝对定位的特点:建立了以包含块为基准的定位;完全脱离了标准文档流;随即拥有偏移属性和z-index属性
(1)未设置偏移量:
<head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <style> .box1{ background:red;height:100px; } .box2{ background:yellow;height:200px; position:absolute; } .box3{ background:blue;height:300px; } </style> </head> <body> <div class="box1">box1</div> <div class="box2">box2</div> <div class="box3">box3</div> </body>
它会保持在初始位置,效果有点像浮动,会脱离标准文档流,并且在没有设置宽度的情况下,宽度会根据填充内容的宽度而改变
(2)有偏移量
1.没有已经定位的祖先元素,会以<html>为偏移的参照基准
<head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <style> .box1{ background:red;height:100px; } .box2{ background:yellow;height:200px; position:absolute; top:50px; left:20px; } .box3{ background:blue;height:300px; } </style> </head> <body> <div class="box1">box1</div> <div class="box"> <div class="box2">box2</div> </div> <div class="box3">box3</div> </body>
2.有已经定位的祖先元素,会以离它最近的祖先元素为偏移的参照基准
<head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <style> .box1{ background:red;height:100px; } .box2{ background:yellow;height:200px; position:absolute; top:50px; left:20px; } .box3{ background:blue;height:300px; } .box{ position:relative; height:100px; background:pink; } </style> </head> <body> <div class="box1">box1</div> <div class="box"> <div class="box2">box2</div> </div> <div class="box3">box3</div> </body>
相关文章推荐
- CSS中的position定位
- CSS中position定位的个熟悉示例介绍
- css position(定位)
- css中position的两种定位(absolute、relative)
- 浅析CSS——元素重叠及position定位的z-index顺序
- ie6下position:absolute绝对定位内容不显示
- 解决IE6浏览器下position:fixed固定定位问题
- 当页面有position:fixed的悬浮菜单时,如何调整锚定位的时候不被悬浮菜单挡住?
- css之定位(position)
- CSS中的定位属性(position)
- css position定位详解
- CSS常见问题及解决(五) position定位中的absolute
- jQuery offset,position,offsetParent,scrollLeft,scrollTop html控件定位 css position
- 层的固定定位.跨浏览器的实践:position:fixed
- CSS教程:position 绝对定位的问题
- position定位和添加阴影
- 跨浏览器的CSS固定定位{position:fixed}
- css中position定位的一些要点
- 解决 IE6 position:fixed 固定定位问题(div固定,不随着滚动条滚动)
- CSS布局浮动(float)和定位(position)属性的区别和使用