一、position属性

1、relative(相对定位)

  • 相对它原来的位置,通过指定偏移,到达新的位置。
  • 扔在标准流中,它对父级盒子和相邻的盒子都没有任何影响。

看下面的例子:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>相对定位</title>
<style type="text/css">
.box1,.box2,.box3{
width: 100px;
height: 100px;
}
.box1{
background-color: red;
}
.box2{
background-color:blue;
}
.box3{
background-color: yellow;
}
</style>
</head>
<body>
<div class="box"></div>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>

效果:

然后分别给第一个和第二个盒子添加定位:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>相对定位</title>
<style type="text/css">
.box1,.box2,.box3{
width: 100px;
height: 100px;
}
.box1{
background-color: red;
position: relative;
left: 100px;
}
.box2{
background-color:blue;
position: relative;
left: 100px;
top: 50px;
}
.box3{
background-color: yellow;
}
</style>
</head>
<body>
<div class="box"></div>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>

效果:

观察上面的截图会发现:第一个和第二个盒子分别相对于原来的位置进行了偏移,但是对父级盒子和相邻的盒子都没有影响。

2、absolute(绝对定位)

  • 相对已设定非static定位属性的父元素计算偏移量,脱离文档流。

看下面的例子:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>绝对定位</title>
<style type="text/css">
.box1,.box2,.box3{
width: 100px;
height: 100px;
}
.box1{
background-color: red;
position: relative;
left: 100px;
}
.box2{
background-color:blue;
position: absolute;
left: 100px;
top: 50px;
}
.box3{
background-color: yellow;
width: 120px;
}
</style>
</head>
<body>
<div class="box"></div>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>

效果:

观察上面的截图可以发现:absolute定位是脱离文档流的,是相对于父元素进行偏移。

3、fixed(相对浏览器固定定位,IE6不支持)

看下面的例子:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>fixed</title>
<style type="text/css">
.box1,.box2,.box3{
width: 100px;
height: 100px;
}
.box1{
background-color: red;
position: relative;
left: 100px;
}
.box2{
background-color:blue;
position: absolute;
left: 100px;
top: 50px;
}
.box3{
background-color: yellow;
width: 120px;
position: fixed;
left: 100px;
top: 200px;
}
</style>
</head>
<body>
<div class="box"></div>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br />
</body>
</html>

效果:

上下移动滚动条的时候你会发现,第三个盒子的位置不会随着滚动条的滚动而上下移动,相对于浏览器是固定的。

4、static(默认)

  • 偏移量设置
  • X轴(left、right属性)与Y轴(top、bottom属性)
  • 可取值:像素或百分比。

5、定位图解

6、Z-Index

Z-Index用来设置定位盒子的层级

  • 数字越大层级越高,越在上层。

例如:Z-Index:2;

注意:

  • 数字之后没有单位。
  • 数字可以设置为负值。

看下面的例子:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>绝对定位</title>
<style type="text/css">
.box1,.box2,.box3{
width: 100px;
height: 100px;
}
.box1{
background-color: red;
position: relative;
left: 100px;
}
.box2{
background-color:blue;
position: absolute;
left: 100px;
top: 50px;
}
.box3{
background-color: yellow;
width: 120px;
}
</style>
</head>
<body>
<div class="box"></div>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>

效果:

现在给box1添加层级:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>绝对定位</title>
<style type="text/css">
.box1,.box2,.box3{
width: 100px;
height: 100px;
}
.box1{
background-color: red;
position: relative;
left: 100px;
z-index: 1;
}
.box2{
background-color:blue;
position: absolute;
left: 100px;
top: 50px;
}
.box3{
background-color: yellow;
width: 120px;
}
</style>
</head>
<body>
<div class="box"></div>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>

效果:

这时box1就会在box2上面。

也可以给box2添加层级:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>绝对定位</title>
<style type="text/css">
.box1,.box2,.box3{
width: 100px;
height: 100px;
}
.box1{
background-color: red;
position: relative;
left: 100px;
/* z-index: 1; 添加层级 */
}
.box2{
background-color:blue;
position: absolute;
left: 100px;
top: 50px;
z-index: -2;/*添加负数的层级*/
}
.box3{
background-color: yellow;
width: 120px;
}
</style>
</head>
<body>
<div class="box"></div>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>

效果:

实例:

实现网页横幅的效果:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>定位基本应用</title>
<style type="text/css">
#adverImg{
width: 426px;
height: 130px;/*和图片的宽度和高度一致*/
position: relative;/*父元素添加相对定位*/
border: 1px solid red;
}
#number{
position: absolute;
right: 5px;
bottom: -10px;
}
/*li标签设置样式,使用后代选择器*/
#number li{
list-style: none;/*设置li标签样式:不显示前面的圆点*/
float: left; /*设置浮动:使li标签在一行显示*/
width: 20px;
height: 20px;
border: 1px solid #666666;/*设置边框*/
margin-left: 5px;/*设置向左的外边距,使每个li标签之间有空格*/
text-align: center;/*设置文字水平方向居中*/
line-height: 20px;/*设置文字垂直方向居中*/
/* color: white; */
cursor: pointer;/*设置鼠标移动到li标签时显示小手的形状*/
background-color: white;
}
</style>
</head>
<body>
<div id="adverImg">
<img src="data:images/adver-01.jpg" alt="商品促销" />
<ul id="number">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</div>
</body>
</html>

效果:

CSS(八):定位属性的更多相关文章

  1. css - Position定位属性与层级关系

    今天同事发现一个有意思的问题,关于position的层级关系的,他要不说我也没注意过 测试后果然有趣,有待深入研究: <!DOCTYPE html> <html> <he ...

  2. CSS Position 定位属性

    本篇文章主要介绍元素的Position属性,此属性可以设置元素在页面的定位方式. 目录 1. 介绍 position:介绍position的值以及辅助属性. 2. position 定位方式:介绍po ...

  3. CSS position(定位)属性

    关于CSS position,来自MDN的描述: CSS position属性用于指定一个元素在文档中的定位方式.top.right.bottom.left 属性则决定了该元素的最终位置. 然后来看看 ...

  4. CSS position定位属性

    css中的position属性是用于设置元素位置的定位方式 它有以下几种取值: static:默认定位方式,子容器在父容器中按照默认顺序进行摆放 absolute:绝对定位,元素不占据父容器空间,相当 ...

  5. CSS的定位属性实现text-shadow属性的文本下产生阴影效果

    只要先理解text-shadow的原理,就能用定位元素进行效果的模仿. text-shadow: h-shadiv v-shadov blur color h-shadv为文本水平移动的距离,正值相对 ...

  6. CSS属性:定位属性(图文详解)

    本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. CSS的定位属性有三种,分别是绝对定位.相对定位.固定定位. posit ...

  7. css中的定位属性position(转)

    css中的定位属性position   同样的也是上课的时候发现学生难以理解的一些问题拿出来记录一下,希望帮助初学者. 在css中定位属性position的运用在页面中是很常用的,特别是一些结合js来 ...

  8. css 08-CSS属性:定位属性

    08-CSS属性:定位属性 CSS的定位属性有三种,分别是绝对定位.相对定位.固定定位. position: absolute; <!-- 绝对定位 --> position: relat ...

  9. CSS定位属性Position详解

    CSS中最常用的布局类属性,一个是Float(CSS浮动属性Float详解),另一个就是CSS定位属性Position. 1. position:static 所有元素的默认定位都是:position ...

随机推荐

  1. robotframework + appium 获取android toast

    android toast 获取主要方式是在出现toast的时候查找元素:xpath=//*[contains(@text,'记同步')]  ,该xpath 表示为toast信息含有  "记 ...

  2. 安装windows后grub修复

    安装windows之后发现ubuntu进不去了,主要原因在于grub被windows干掉了. 原本希望通过使用u盘来进行修复,结果U盘不被识别. 于是通过easybcd启动ubunt live光盘.进 ...

  3. 用python做网页抓取与解析入门笔记[zz]

    (from http://chentingpc.me/article/?id=961) 事情的起因是,我做survey的时候搜到了这两本书:Computational Social Network A ...

  4. AR模型与数据平稳性之间的关系

    作者:桂. 时间:2017-12-19  21:39:08 链接:http://www.cnblogs.com/xingshansi/p/8068021.html 前言 前几天碰到一个序列分析的问题, ...

  5. [svc]linux正则及grep常用手法

    正则测试 可以用sublime等工具快速的检测正则是否合适 china : 匹配此行中任意位置有china字符的行 ^china : 匹配此以china开关的行 china$ : 匹配以china结尾 ...

  6. Fetch API 接口参考

    前言 Fetch API是新的ajax解决方案,用于解决古老的XHR对象不能实现的问题,Fetch API 提供了一个获取资源的接口(包括跨域请求),任何使用过 XMLHttpRequest 的人都能 ...

  7. 用NPOI创建Excel、合并单元格、设置单元格样式、边框的方法

    本篇文章小编为大家介绍,用NPOI创建Excel.合并单元格.设置单元格样式.边框的方法.需要的朋友参考下 今天在做项目中,遇到使用代码生成具有一定样式的Excel,找了很多资料,最后终于解决了,Ex ...

  8. 在vue-cli搭建的项目中在后台mock接口中支持req.body和req.cookies

    在<vue-cli搭建的项目中增加后台mock接口>中实现了后台mock,但是前端post的t数据都要在mock的后台接口中使用req的接收数据事件获取http协议body中的数据. re ...

  9. linux命令(51):set 指定行,直接替换并修改文件

    sed 命令: 指定行,从第一行到第一行: 把该行的ssd,换成cd: -i 表示的是替换并直接修改文件: sed  -i  '1,1s/ssd/cd/g' test_file 命令使用: sed - ...

  10. 【Bootloader】探究bootloader,分析u-boot源码

    Preface 之前也发表过关于<Bootloader启动过程分析>的文章,但是内容表达得比较抽象,大多是文字叙述,所以这里从系统和代码的角度来深入分析bootloader的启动过程. 工 ...