一、Position介绍
position 属性规定元素的定位类型,这个属性定义建立元素布局所用的定位机制。任何元素都可以定位,不过绝对或固定元素会生成一个块级框,而不论该元素本身是什么类型。相对定位元素会相对于它在正常流中的默认位置偏移。
常用值有static、absolute、fixed、relative这四种。
注:文档流又称正常流,是默认情况下HTML元素排版布局过程中元素会自动按照自上而下或从左到右进行流式排放的一种顺序。
二、四种常用position样式属性值介绍
1、static
默认值。没有定位,元素出现在正常流中(忽略top,bottom,left,right或者z-index声明)。
示例如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>static</title>
<style type="text/css">
.out{
width:200px;
border: 1px solid #00ee00;
}
.in{
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div class="out">
<div class="in" style="background-color: red;"></div>
<div class="in" style="background-color: green;"></div>
<div class="in" style="background-color: yellow;"></div>
</div>
<br/>
<div class="out">
<div class="in" style="background-color: red;"></div>
<div class="in" style="background-color: green; position:static; top:20px; left:20px;/*top和left没起作用*/"></div>
<div class="in" style="background-color: yellow;"></div>
</div>
说明:position为static时,其所作用的标签没有脱离正常文档流
</body>
</html>
结果如下:
2、relative
生成相对定位的标签,因此,“left:20”会向标签的left位置添加20像素。
示例如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>relative</title>
<style type="text/css">
.out{
width:200px;
border: 1px solid #00ee00;
}
.in{
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div class="out">
<div class="in" style="background-color: red;"></div>
<div class="in" style="background-color: green; position:relative;"></div>
<div class="in" style="background-color: yellow;"></div>
</div>
说明:position样式属性的属性值为relative,但是没有指定top、right、left和bottom,这时其位置没有发生变化,即没有脱离正常文档流。
<br/>
<div class="out">
<div class="in" style="background-color: red;"></div>
<div class="in" style="background-color: green; position:relative; top:20px; left:20px;"></div>
<div class="in" style="background-color: yellow;"></div>
</div>
说明:对比可以发现position为relative时标签的移动是相对于原来位置而言的,尽管如此,依然没有脱离正常文档流。
</body>
</html>
结果如下:
3、absolute
生成绝对定位的标签,相对于标签本身第一个position为非static的父元素进行定位。标签通过“left”,“top”,“right”以及“bottom”样式属性进行定位。如果该标签所在的父标签均没有设置position为非static,则相对于浏览器窗口进行定位,但是此时元素会随着滚动条的滑动而滑动。
示例如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>absolute</title>
<style type="text/css">
.out{
width:200px;
border: 1px solid #00ee00;
}
.in{
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<br/>
<div class="out" style="position:relative;">
<div class="in" style="background-color: red;"></div>
<div class="in" style="background-color: green; position:absolute;"></div>
<div class="in" style="background-color: yellow;"></div>
</div>
说明:position样式属性的属性值为absolute会使其所作用的标签脱离正常文档流。
<br/>
<div class="out" style="position:relative;">
<div class="in" style="background-color: red;"></div>
<div class="in" style="background-color: green; position:absolute; top:20px; left:20px;"></div>
<div class="in" style="background-color: yellow;"></div>
</div>
说明:position为absolute的div是相对于其直接父标签div进行定位的,因为其直接父标签div的position为非static,而absolute修饰的元素是相对于除position为static以外的第一个父元素进行定位的。
</body>
</html>
结果如下:
4、fixed
生成绝对定位的标签,相对于浏览器窗口进行定位,此时元素不会随着滚动条的滑动而滑动。元素通过“left”,“top”,“right”以及“bottom”样式属性进行定位。
示例如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>fixed</title>
<style type="text/css">
.out{
width:200px;
border: 1px solid #00ee00;
}
.in{
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div class="out">
<div class="in" style="background-color: red;"></div>
<div class="in" style="background-color: green;"></div>
<div class="in" style="background-color: yellow;"></div>
</div>
<br/>
<div class="out">
<div class="in" style="background-color: red;"></div>
<div class="in" style="background-color: green; position:fixed;"></div>
<div class="in" style="background-color: yellow;"></div>
</div>
说明:position样式属性的属性值为fixed会使其所作用的标签脱离正常文档流。
<br/>
<div class="out">
<div class="in" style="background-color: red;"></div>
<div class="in" style="background-color: green; position:fixed; bottom:20px; left:20px;"></div>
<div class="in" style="background-color: yellow;"></div>
</div>
说明:position为fixed的div是相对于浏览器窗口进行定位的。
</body>
</html>
其结果如下:
三、注意
任何元素都可以定位,但但absolute和fixed元素会生成一个块级框,不论该元素本身是不是块级元素。relative元素会相当于它在正常流中的默认位置偏移。