您的位置:首页 > Web前端 > CSS

[转载]CSS教程:实例讲解定位Position

2014-10-31 13:32 841 查看

http://www.missyuan.com/thread-395406-1-1.html

1. position:static

所有元素的默认定位都是:position:static,这意味着元素没有被定位,而且在文档中出现在它应该在的位置。

一般来说,不用指定 position:static,除非想要覆盖之前设置的定位。

#div-1 {
position:static;
}




2. position:relative

如果设定 position:relative,就可以使用 top,bottom,left 和 right 来相对于元素在文档中应该出现的位置来移动这个元素。【意思是元素实际上依然占据文档中的原有位置,只是视觉上相对于它在文档中的原有位置移动了】

#div-1 {
position:relative;
top:20px;
left:-40px;
}




3. position:absolute

当指定 position:absolute 时,元素就脱离了文档【即在文档中已经不占据位置了】,可以准确的按照设置的 top,bottom,left 和 right 来定位了。

#div-1a {
position:absolute;
top:0;
right:0;
width:200px;
}




4. position:relative + position:absolute

如果我们给 div-1 设置 relative 定位,那么 div-1 内的所有元素都会相对 div-1 定位。如果给 div-1a 设置 absolute 定位,就可以把 div-1a 移动到 div-1 的右上方。

#div-1 {
position:relative;
}
#div-1a { position:absolute; top:0; right:0; width:200px; }




5. 两栏绝对定位

现在就可以使用相对定位和绝对定位来做一个两栏布局了。

#div-1 {
position:relative;
}
#div-1a { position:absolute; top:0; right:0; width:200px; }
#div-1b {
position:absolute;
top:0;
left:0;
width:200px;
}




6. 两栏绝对定位定高

一种方案是给元素设定固定高度。但这种方案对大多数设计来说不太适合,因为一般我们不知道元素中会有多少文本,或者将要使用的精确的字号。

#div-1 {
position:relative;
height:250px;
}
#div-1a { position:absolute; top:0; right:0; width:200px; }
#div-1b {
position:absolute;
top:0;
left:0;
width:200px;
}




7. 浮动

对于可变高度的列来说,绝对定位就不起作用了,以下是另外一个方案。

我们可以浮动一个元素,使它移动到左边/右边,并且是文本环绕着它。这主要用于图像,但这里我们把它用于一个复杂的布局任务(因为这是我们唯一的工具)。

#div-1a {
float:left;
width:200px;
}




8. 浮动列

如果我们把一个元素向左浮动,并且把第二个元素也向左浮动,they will push up against each other。

#div-1a {
float:left;
width:150px;
}
#div-1b {
float:left;
width:150px;
}




9. 清除浮动列

在浮动元素之后,我们可以清除浮动来使其他元素正确定位。

#div-1a {
float:left;
width:190px;
}
#div-1b {
float:left;
width:190px;
}
#div-1c {
clear:both;
}




虽然我一直用浮动布局,但是掌握好 position 也是必须的,其实也没那么难的。。。



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<STYLE TYPE="text/css">
body {
font-family: tahoma, helvetica, arial, sans-serif;
font-size: 12px;
padding: 0;

text-align:center;
background-color:#000000;

}
#Divbody {
width:1000px;
height:900px;
margin:auto;
}
#div-before {
width:100%;
height:20px;
background-color:#FF0000;
}
#div-after {
width:100%;
height:20px;
background-color:#FD0000;
}
#div-1 {
width:100%;
height:500px;
background-color:#999999;
position:relative;

}
#div-1a {
width:200px;
background-color:#3300FF;
float:left;

}
#div-1b {
width:300px;
height:250px;
background-color:#66FF00;

}
#div-1c {
width:100px;
height:250px;
background-color:#00FFFF;
margin:auto;

}
</STYLE>
</head>

<body>
<div id=Divbody>
<div id=div-before>#div-before</div>
<div id=div-1>
<div id=div-1a>Based on a true story, the Chinese ballet which made its premiere in 1964Based on a true story, the Chinese ballet which made its premiere in 1964Based on a true story, the Chinese ballet which made its premiere in 1964 depicts the liberation of a peasant girl on Hainan Island. The classic ballet will be performed by the Chinese Central Ballet Troupe in Guangzhou in June.
</div>
<div id=div-1b>Based on a true story, the Chinese ballet which made its premiere in 1964 depicts the liberation of a peasant girl on Hainan Island. The classic ballet will be performed by the Chinese Central Ballet Troupe in Gu.
</div>
<div id=div-1c>Based on a true story, the Chinese ballet which made its premiere in 1964 depicts the liberation of a peas.d. </div> </div> <div id=div-after>#div-after</div></div></body></html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: