讲给Android程序员看的前端系列教程(40集免费视频教程+源码)
版权声明
- 本文原创作者:谷哥的小弟
- 作者博客地址:http://blog.csdn.net/lfdfhl
定位(position)的简介
在HTML中,利用定位(position)可较为精确地定义元素显示的位置,这个位置可以是相对于其本身出现的位置,也可以是相对于其上级元素的位置,还可以是相对于其他元素的位置,亦可为相对于浏览器视窗左上角的位置。
定位(position)的语法
position:relative | absolute | static | fixed ;
top | right | bottom | left:Mpx ;
小结如下:
-
定位(position)常用类型:static、absolute、relative、fixed
-
定位(position)通常与top、right、bottom、left属性联合使用
-
定位(position)的使用可能导致元素脱离标准流
现在分别介绍四种常用的定位(position)类型。
static
position:static是CSS中默认的定位类型,在该类型下元素以标准流的显示方式,其位置不受top、bottom、left、right的影响。
请看如下示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>static定位</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
div {
width: 510px;
height: 200px;
border: 1px solid red;
background-color: greenyellow;
position: static;
left: 50px;
top: 80px;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
运行后,效果如下图所示:
在该示例中,在标签选择器div里设置了position:static并且为left和top属性设置了具体的值,但是我们发现:<div></div>的位置没有发生改变。
relative
相对定位position:relative用于设置元素位置,即相对于元素本身原来的位置进行调整。
相对定位特性:
- 相对于自身原来在标准流中位置而移动
- 移动后继续占有原来在标准流中的位置
- 元素设置position:relative后该元素不会脱离标准流
我们先来看一个不设置定位的例子:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>无定位</title>
<style>
.fatherDiv {
width: 400px;
height: 500px;
background-color: pink;
}
.blueSonDiv{
width: 200px;
height: 200px;
background-color: blue;
}
.greenyellowSonDiv{
width: 200px;
height: 200px;
background-color: greenyellow;
}
</style>
</head>
<body>
<div class="fatherDiv">
<div class="blueSonDiv"></div>
<div class="greenyellowSonDiv"></div>
</div>
</body>
</html>
结果如下:
接下来,我们为blueSonDiv设置相对定位:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>相对定位</title>
<style>
.fatherDiv {
width: 400px;
height: 500px;
background-color: pink;
}
.blueSonDiv{
width: 200px;
height: 200px;
background-color: blue;
/* 相对定位 */
position: relative;
top: 30px;
left: 30px;
}
.greenyellowSonDiv{
width: 200px;
height: 200px;
background-color: greenyellow;
}
</style>
</head>
<body>
<div class="fatherDiv">
<div class="blueSonDiv"></div>
<div class="greenyellowSonDiv"></div>
</div>
</body>
</html>
运行后,效果如下图所示:
absolute
绝对定位position:absolute是元素参照带有定位的父级元素实现位置移动。
绝对定位特性:
- 如果父元素和爷爷元素等上级元素都没有定位,则子元素以浏览器左上角为参照进行定位。
- 如果父元素或爷爷元素等上级元素有定位,则子元素依据最近的已经定位(绝对、固定或相对定位)的父元素或爷爷元素进行定位。
- 子元素设置绝对定位后将脱离原来的标准流,在原标准流中亦不再保留其原来的位置
绝对定位示例1
子元素采用绝对定位而父元素无定位。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>绝对定位</title>
<style>
/* 父元素无定位 */
.fatherDiv {
width: 400px;
height: 500px;
background-color: pink;
}
.blueSonDiv{
width: 200px;
height: 200px;
background-color: blue;
/* 子元素绝对定位 */
position: absolute;
top: 30px;
left: 30px;
}
.greenyellowSonDiv{
width: 200px;
height: 200px;
background-color: greenyellow;
}
</style>
</head>
<body>
<div class="fatherDiv">
<div class="blueSonDiv"></div>
<div class="greenyellowSonDiv"></div>
</div>
</body>
</html>
运行后,效果如下图所示:
从结果中我们可以看到:
- 1、子元素blueSonDiv以屏幕的左上角为参照进行定位
- 2、子元素blueSonDiv脱离了标准流
绝对定位示例2
子元素采用绝对定位而父元素采用相对定位(子绝父相)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>绝对定位</title>
<style>
/* 父元素使用相对定位 */
.fatherDiv {
width: 400px;
height: 500px;
background-color: pink;
position: relative;
}
.blueSonDiv{
width: 200px;
height: 200px;
background-color: blue;
/* 子元素绝对定位 */
position: absolute;
top: 30px;
left: 30px;
}
.greenyellowSonDiv{
width: 200px;
height: 200px;
background-color: greenyellow;
}
</style>
</head>
<body>
<div class="fatherDiv">
<div class="blueSonDiv"></div>
<div class="greenyellowSonDiv"></div>
</div>
</body>
</html>
运行后,效果如下图所示:
从结果中我们可以看到:
- 1、子元素参照父元素位置移动
- 2、子元素脱离了标准流
fixed
position:fixed表示固定定位,即相对浏览器窗口定位。采用该定位后,不论页面如何滚动,该盒子显示的位置不变并完全脱离标准流。
请看如下示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>fixed定位</title>
<style type="text/css">
img {
width: 20%;
/* 固定定位 */
position: fixed;
left: 300px;
top: 200px;
}
.blueDiv{
width: 200px;
height: 200px;
background-color: blue;
}
.greenyellowDiv{
width: 200px;
height: 200px;
background-color: greenyellow;
}
</style>
</head>
<body>
<img src="img/girlfriend.jpg"/>
<div class="blueDiv"></div>
<div class="greenyellowDiv"></div>
<div class="blueDiv"></div>
<div class="greenyellowDiv"></div>
<div class="blueDiv"></div>
<div class="greenyellowDiv"></div>
</body>
</html>
运行后,效果如下图所示:
在该示例中,在标签选择器img中设置了position:fixed;属性。从效果图中可以看到:图片一直固定在某个位置,当我们拖动滚动条时它的位置仍然保持不变
规避脱标流
有时使用float和position来实现某种效果,但是这很可能导致某些元素脱离标准流(脱标);所以,应尽可能地避免脱标,可遵守以下原则:
- 网页布局过程中尽量使用标准流布局
- 标准流不能解决的问题优先考虑使用浮动
- 浮动不能解决的问题再考虑使用定位
- 在子元素中使用margin-left:auto 将其置于父元素的最右边
- 在子元素中使用margin-right:auto将其置于父元素的最左边
- 在子元素中使用margin 0 auto;使其在父元素内水平居中显示