[转载]CSS教程:实例讲解定位Position
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>
[转载]CSS教程:实例讲解定位Position的更多相关文章
- CSS(8)---通俗讲解定位(position)
CSS(8)---通俗讲解定位(position) CSS有三种基本的定位机制: 普通流.浮动.定位.前面两个之前已经讲过,详见博客: 1.CSS(5)---通俗讲解盒子模型 2.CSS(6)---通 ...
- CSS 浮动(float)与定位(position)
一.浮动 1.三个属性:left.right.none. 2.特点:容易造成父项塌陷,故在父项需要清除浮动 3.父项塌陷现象 4.父项塌陷解决方案(建议使用):清除浮动 .parent:after{ ...
- angular.js 教程 -- 实例讲解
angular.js AngularJS [1] 诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.Ang ...
- Vue.js 教程 -- 实例讲解
一. Vue.js是什么 Vue.js是一套构建用户界面(view)的MVVM框架.Vue.js的核心库只关注视图层,并且非常容易学习,非常容易与其他库或已有的项目整合. 1.1 Vue.js的目的 ...
- [转载]CSS教程--字体与文本属性
b>font-family功能:用于改变HTML标志或元素的字体,你可设置一个可用字体清单.浏览器由前向后选用字体.语法:{font-family:字体1,字体2, ... ,字体n} font ...
- [转载]CSS元素的定位position
CSS元素的定位position 属性position 值 描述 absolute 生成绝对定位的元素,相对于 static 定位以外的第一 ...
- HTML+CSS教程(六)浮动-float+定位-position+居中问题
一.浮动(float)1.文档流:是指盒子按照 html 标签编写的顺序依次从上到下,从左到右排列,块元素占一行,行内元素在一行之内从左到右排列,先写的先排列,后写的排在后面,每个盒子都占据自己的位置 ...
- div+css定位position详解
div+css定位position详解 1.div+css中的定位position 最主要的两个属性:属性 absolute(绝对定位) relative(相对定位),有他们才造就了div+css布局 ...
- CSS.04 -- 浮动float、overflow、定位position、CSS初始化
标准流:行内/行内块元素横向有序排列 : 块元素纵向有序排列. 浮动:Float 语法:float:left/right : 设置浮动的元素,脱离标准流 浮动的框可以向左或向右移动,直到它的外边缘碰 ...
随机推荐
- 【基础知识】.Net基础加强07 天
一. 自定义泛型 1. 泛型的目的:提高代码的复用性. 2. T:一般叫做“类型参数”,把数据类型作为参数传递 .一般用T类表示或者以大写T开头的比如:TKey,TValue.TOutput.TRes ...
- Arcgis for Javascript 出现“init.js->TypeError: f is not a function”
环境 采用离线JS包,版本为v3.8 问题描述 在为map添加了 app.map.on("pan-start", this.showHandBeignPan()); 在拖动地图的时 ...
- SqlServer=>MySql
原文:http://www.cnblogs.com/andrew-blog/archive/2011/12/03/SQL_mss2sql.html#3242272 工具:http://files.cn ...
- Salesforce 生成测试可用 Id
在写 Test Class 的时候,有时候需要一批有 Id 的数据或者把 Id 作为参数等情况,在数据关系比较复杂的情况下去造真实数据有些麻烦,于是找到这样一个可以生成 Id 的方法可以用来辅助测试! ...
- Hibernate关联映射(转载)
原文:http://www.cnblogs.com/huxi/archive/2009/12/15/1624988.html 以简单的两个类为例: User(int id, String name) ...
- Linux中ctrl-c, ctrl-z, ctrl-d 区别
在Linux中: ctrl-c: ( kill foreground process ) 发送 SIGINT 信号给前台进程组中的所有进程,强制终止程序的执行: ctrl-z: ( suspend ...
- 学习Git
参考廖雪峰老师的个人网站:http://www.liaoxuefeng.com 版本库初始化 通过git init命令将这个命令变为git可以管理的仓库 添加文件 用命令git add file告诉G ...
- 比较用decodeFileDescriptor和decodeFile的区别
从本地中读取图片,可以用decodeFileDescriptor和decodeFile,至于哪一种方式的耗内存情况作了一次简单对比,可能一次选取6张图片数量过少,貌似区别不大,decodeFileDe ...
- 【1】CommonCode快速代码集
阅读目录 CommonCode是什么? CommonCode包括哪些内容? 版本信息 回到顶部 CommonCode是什么? 简单的说,CommonCode是作者在经历各种"试错&quo ...
- C#中的函数式编程
在函数式编程中,可以把函数看作数据.函数也可以作为参数,函数还可以返回函数.比如,LINQ就是基于函数式编程的. 两个例子引出函数式编程 语句式编程可能这样写: string result; ) { ...