1.子绝父相
注意:子元素设置绝对定位,父元素可以设置相对定位、绝对定位、固定定位, 这三种情况,参照元素都是父元素**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.wrap {
width: 300px;
height: 300px;
background-color: pink;
/* position: relative; */
/* position: absolute; */
position: fixed;
}
.box1 {
width: 100px;
height: 100px;
background-color: tomato;
position: absolute;
top: 100px;
right: 50px;
}
</style>
</head>
<body>
<!--
这三种情况,子元素设置绝对定位,参照元素都是父元素
1.子绝父相(一般都用这个)
子元素设置绝对定位,父元素设置相对定位
2.子绝父绝
子元素设置绝对定位,父元素设置绝对定位
3.子绝父固
子元素设置绝对定位,父元素设置固定定位
-->
<div class="wrap">
<div class="box1">box1</div>
</div>
</body>
</html>二、CSS 透明
透明度属性
1、rgba(r,g,b,a)颜色模式
兼容性:IE6、7、8下不兼容,IE9+支持
使用: background-color属性、color属性、border-color属性等中设置透明度
语法 rgba(r,g,b,a) r-red 取值范围0-255之间 g-green 取值范围0-255之间 b-blue 取值范围0-255之间 a-alpha 取值范围0-1之间表示半透明 1表示完全不透明 0表示完全透明
超出范围的值将被截至最近的极限值
2、opacity属性 透明度
兼容性:IE6、7、8下不兼容,IE9+支持
使用 将内容(及所有后代)、背景一起透明,自身透明,子元素也透明
取值 范围0-1之间表示半透明 0表示完全透明 1表示完全不透明
3、filter属性
兼容性:仅仅支持IE6、7、8、9,在IE10及以上被废除
使用: IE浏览器专属
语法 filter:Alpha(opacity=n) n的取值在0-100之间表示半透明,0表示完全透明,100表示完全不透明
想用filter实现父元素背景透明,子元素不透明:
父元素加静态定位,子元素加相对定位,能阻止透明度的传递
.box盒子有透明度
.box{
background: red;
opacity: .4;/* IE9+ */
filter: Alpha(opacity=40);/* IE6|7|8|9兼容 */
}
.box{
width:300px;
height: 300px;
background:red;
filter:Alpha(opacity = 30);
position: static;
}
.box span{
background:blue;
font-size: 40px;
position: relative;
}
<div class="box">
<span>东方闪电</span>
</div>
三、CSS块级格式化
概念
BFC(Block formatting context)直译为"块级格式化上下文"。它是一个独立的渲染区域,只有Block-level box(块级标签)参与, 它规定了内部的Block-level Box(块级标签)如何布局,并且与这个区域外部毫不相干。