网站首页 > 知识剖析 正文
绝对定位
position:
absolute
特性:
1、脱离文档流,定位元素占据的位置会释放
2、原点计算:如果该元素做了定位,那么就去找它做了定位的父元素(找最近的)作为原点基准,如果父元素都没做定位,则以浏览器(0,0)作为原点基准。
3、对内嵌元素定位后,设置宽高属性就会有效果
应用场景:
一般情况下,绝对定位用在下拉菜单、焦点图轮播、弹出数字气泡、特别花边等场景
相对定位
position:
relative
1、不脱离文档流,定位元素占据的位置不会被释放/
2、原点计算:以父级元素作为原点基准,若没有父级元素则以浏览器(0,0)为基准。
一般的应用:父相子绝
3、父元素为相对定位,子元素为绝对定位,文档元素不会受影响。
4、父元素提供相对定位后,子元素以父元素为基准来进行定位。
应用场景:
相对定位一般情况下很少自己单独使用,都是配合绝对定位使用,为绝对定位创造定位父级而又不设置偏移量
固定定位
position:
fixed
1、脱离了文档流
2、原点计算:以浏览器(0,0)作为原点基准,尽管父元素做了定位也不会影响它的原点
基准。
应用场景:
一般在网页中被用在窗口左右两边的固定广告、返回顶部图标、吸顶导航栏等
注意:使用定位后会激活如下5个属性
left | right | top | bottom | z-index
z-index
改变定位后的叠放顺序
取值范围:-1~9999
其他:
设置网页元素的透明度
opacity: 0~1;
filter: opacity(0.2) | contrast(0.2)
绝对定位(absolute)代码案例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>绝对定位</title>
<style type="text/css">
/*绝对定位:
*1、脱离文档流,做了定位后它占据的位置会释放。
*2、原点计算:如果该元素做了定位,那么就去找它做了定位的父元素(最近)作为原点基准,若果父元素
* 都没做定位,则以浏览器(0,0)作为原点基准。
*3、对内嵌元素做了定位后,它的宽度高度属性就会有效。
*/
*{
padding: 0px;
margin: 0px;
}
.box-father{
width: 500px;
height: 500px;
margin-left: 500px;
background-color: yellow;
position: absolute;
}
.son{
width: 400px;
height: 400px;
margin-left: 20px;
background-color: black;
position: absolute;
}
.box{
width: 300px;
height: 300px;
background-color: blue;
/*绝对定位*/
position: absolute;
/*激活4个属性*/
left: 150px;
/*right: ;*/
top: 100px;
/*bottom: ;*/
}
.box2{
width: 400px;
height: 400px;
background-color: red;
}
span{
width: 200px;
height: 200px;
background-color: green;
/* position: absolute;*/
float: left;
}
</style>
</head>
<body>
<div class="zx"> <!-- 祖先 :定位-->
<div class="box-father"> <!-- 爷爷 :定位-->
<div class="son"> <!-- 儿子:定位-->
<div class="box"> <!-- 孙子:如果孙子做了定位,它就去找接近它定位最近的父元素来做为基准 -->
</div>
</div>
</div>
</div>
<div class="box2">
</div>
<span>我是span</span>
</body>
</html>
相对定位(relative)代码案例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>相对定位</title>
<style type="text/css">
/*相对定位:
*1、没有脱离文档流,元素定位了占据的空间不会被释放
*2、原点计算:根据父元素的位置来做基准,如果没有父元素则以浏览器(0,0)作为基准。
* 父相子绝:
* 1、文档元素不会受影响
* 2、父元素提供相对定位后,子元素就以父元素为基准来做绝对定位。
*/
*{
padding: 0px;
margin: 0px;
}
.box-father{
width: 500px;
height: 500px;
margin-left: 100px;
background-color: yellow;
/*相对定位*/
/*position: relative;
left: 100px;*/
}
.box{
width: 200px;
height: 200px;
background-color: blue;
position: absolute;
right: 0px;
bottom:0px;
}
.box-one{
width: 400px;
height: 400px;
background-color: red;
}
/*.box2{
width: 400px;
height: 400px;
background-color: red;
}*/
</style>
</head>
<body>
<div class="box-father">
<div class="box-one">
</div>
<div class="box">
</div>
</div>
<!--<div class="box2">
</div>-->
</body>
</html>
固定定位(fixed)代码案例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>固定定位</title>
<style type="text/css">
/*固定定位:
*1、脱离了文档流
*2、原点计算:按浏览器(0,0)来作为基准
*/
*{
padding: 0px;
margin: 0px;
}
body{
height: 1800px;
background-image: url(img/logo.png);
}
.box-father{
width: 500px;
height: 500px;
background-color: yellow;
position: relative;
}
.box{
width: 200px;
height: 200px;
background-color: blue;
/*固定定位*/
position: fixed;
right: 0px;
bottom: 0px;
}
</style>
</head>
<body>
<div class="box-father">
<div class="box">
</div>
</div>
</body>
</html>
- 上一篇: 终于搞懂了 CSS 中的百分比是基于什么工作的了
- 下一篇: 总有你喜欢的一款 总有你喜欢的一款 美女
猜你喜欢
- 2024-11-12 原来隐藏一个DOM元素可以有这么多种方式,最后一种你肯定不知道
- 2024-11-12 你知道什么是BFC么 你知道什么是bfc么英语
- 2024-11-12 为什么我写的z-index不生效? z index无效
- 2024-11-12 开发人员在编写 HTML 和 CSS 时最常犯的六大错误
- 2024-11-12 css中如何让div水平居中(上) 怎么让div水平居中
- 2024-11-12 CSS 12个趣味小技巧大公开 | 原力计划
- 2024-11-12 谈谈工作中常用的设计模式 工作设计模型
- 2024-11-12 CSS 父元素中的绝对定位 父元素设置相对定位
- 2024-11-12 菜鸟学习记:第二十五天 菜鸟学习记
- 2024-11-12 响应式网页中的高度设计,你认真的吗?
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)