灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:7591回复:0

css教程之绝对定位使用详解

楼主#
更多 发布于:2014-02-09 12:18

一.基本概念:

如果说相对定位没有脱离文档流,相对于对象本身进行偏移有点拖泥带水的话,那么绝对定位绝对是快刀斩乱麻,因为绝对定位可以使一个对象脱离正常的文档流,好像是漂浮在正常文档流的上空,并相对于包含此对象的元素进行定位,当然这个定位相对元素在不同的情况下也有所不同。

二.如何将一个元素设置为绝对定位:

为对象添加如下属性即可将对象设置为绝对定位:

复制代码代码如下:

position:absolute;

或者

复制代码代码如下:

position:fixed

三.定位参考对象:

可以使用top属性和left属性设置绝对定位对象的偏移量。

绝对定位虽然脱离了文档流,但是也需要有定位的参考对象,不过在不同的情况下参考对象也是不同。

1.如果没有设置top或者left属性值,那么相应方位的定位参考对象就是此对象的一级父元素,代码实例如下:

复制代码代码如下:

<!DOCTYPE html>

<html>

<head>

<meta charset=" utf-8">

<title>CSS的绝对定位-蚂蚁部落</title>

<style type="text/css">

body

{

margin:20px;

}

#grandfather

{

width:330px;

height:300px;

background-color:#F90;

}

#father

{

width:200px;

height:200px;

background-color:green;

margin-left:50px;

}

#children

{

width:100px;

height:100px;

background-color:red;

float:right;

}

#inner

{

width:50px;

height:50px;

background-color:blue;

position:absolute;

top:10px;

}

</style>

</head>

<body>

<div id="grandfather">

<div id="father">

<div id="children">

<div id="inner"></div>

</div>

</div>

</div>

</body>

</html>

以上代码中,由于inner元素采用绝对定位,并且没有设置left属性值,所以在水平方位的定位参考对象就是inner元素的一级父元素children。当然如果没有设置top属性值,那么垂直方位的定位参考对象也是children。

2.如果设置了left或者top属性值情况:

1).如果祖先元素中有采用定位的,那么此对象的相应方位的定位参考对象就是此祖先元素,如果祖先元素没有采用定位的,那么相应方位的上的定位参考对象就是浏览器客户区,代码实例如下:

实例一:

复制代码代码如下:

<!DOCTYPE html>

<html>

<head>

<meta charset=" utf-8">

<title>CSS的绝对定位-蚂蚁部落</title>

<style type="text/css">

body

{

margin:20px;

}

#grandfather

{

width:330px;

height:300px;

background-color:#F90;

}

#father

{

width:200px;

height:200px;

background-color:green;

margin-left:50px;

position:relative;

}

#children

{

width:100px;

height:100px;

background-color:red;

float:right;

}

#inner

{

width:50px;

height:50px;

background-color:blue;

position:absolute;

left:10px;

top:10px;

}

</style>

</head>

<body>

<div id="grandfather">

<div id="father">

<div id="children">

<div id="inner"></div>

</div>

</div>

</div>

</body>

</html>

以上代码,inner元素采用绝对定位,并且它的祖先元素father采用相对定位,那么它的定位参考对象就是father。

实例二:

复制代码代码如下:

<!DOCTYPE html>

<html>

<head>

<meta charset=" utf-8">

<title>CSS的绝对定位-蚂蚁部落</title>

<style type="text/css">

body

{

margin:20px;

}

#grandfather

{

width:330px;

height:300px;

background-color:#F90;

}

#father

{

width:200px;

height:200px;

background-color:green;

margin-left:50px;

}

#children

{

width:100px;

height:100px;

background-color:red;

float:right;

}

#inner

{

width:50px;

height:50px;

background-color:blue;

position:absolute;

top:10px;

}

</style>

</head>

<body>

<div id="grandfather">

<div id="father">

<div id="children">

<div id="inner"></div>

</div>

</div>

</div>

</body>

</html>

以上代码中,inner元素采用绝对定位,并且它的祖先元素没有采用定位的,那么垂直方位的定位参考对象就是窗口,由于没有设置left属性值,那么水平方位的定位参考对象就是它的一级父元素children。

四.绝对定位元素脱离文档流:

在开头已经提到过,绝对定位能够使元素脱离文档流,那么它周边文档流中的元素就能够占据此元素没有脱离文档流时的位置。

代码实例如下:

复制代码代码如下:

<!DOCTYPE html>

<html>

<head>

<meta charset=" utf-8">

<title>CSS的绝对定位-蚂蚁部落</title>

<style type="text/css">

div

{

text-align:center;

line-height:100px;

}

.father

{

width:400px;

height:400px;

background-color:green;

margin:50px;

}

.first

{

width:100px;

height:100px;

background-color:red;

position:absolute;

}

.second

{

width:120px;

height:120px;

background-color:blue;

}

</style>

</head>

<body>

<div class="father">

<div class="first">first</div>

<div class="second">second</div>

</div>

</body>

</html>

在以上代码中,由于first元素脱离文档流,所以second元素能够占据原来first元素本来应该占据的位置。


喜欢0 评分0
游客

返回顶部