定位
定位方式有:static、fixed、relative、absolute
Static 静态定位(默认值)
无定位,元素正常出现了流中,不受left、right、top、bottom影响
(1)
<style type="text/css">
div{
width: 200px;
height: 200px;
background-color: aqua;
position: static;
}
</style>
</head>
<body>
<div></div>
静态定位的演示
</body>
(2)
#div1{
width: 100px;
height: 100px;
background-color: aqua;
position: fixed;
left: 30px;
top: 20px;
}
#div2{
width: 100px;
height: 100px;
background-color: darkorange;
}
</style>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
</body>
从结果可以看出,fix定位会将元素从流中“摘”出来单独进行定位取决于left、top
重新定位之后可能会出现重叠,通过z-index可以调节其顺序,但是定位z-index无效
(3)
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style type="text/css">
#div1{
width: 100px;
height: 100px;
background-color: rgba(255,0,0,1);
}
#div2{
width: 100px;
height: 100px;
background-color: greenyellow;
position: relative;
top: 30px;
left: 30px;
}
#div3{
width: 100px;
height: 100px;
background-color: blueviolet;
}
</style>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</body>
从结果可以看出,相对定位是从原有的位置进行位移,但并不影响后续位置
(4)
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style type="text/css">
#div1{
width: 100px;
height: 100px;
background-color: rgba(255,0,0,1);
}
#div2{
width: 100px;
height: 100px;
background-color: greenyellow;
position: fixed;
top: 30px;
left: 30px;
}
#div3{
width: 100px;
height: 100px;
background-color: blueviolet;
}
</style>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</body>
从结果可以看出:绝对定位的元素将从流中被“摘”出来,依靠left属性进行定位。
与fixed类似,但是参照物不同
Fixed 参照根元素(html)
Absolute 参照父容器