一、默认情况
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
margin: 10px;
padding: 5px;
font-size: 12px;
line-height: 25px;
}
#father{
border: 1px solid #C850C0;
padding: 0px;
}
#first{
background-color: #a0a0a0;
border: 1px dashed #78c841;
}
#second{
background-color: #FFCC70;
border: 1px dashed #2a3bc8;
}
#third{
background-color: aqua;
border: 1px dashed #c84614;
}
</style>
</head>
<body>
<div id="father">
<div id="first">第一个盒子</div>
<div id="second">第二个盒子</div>
<div id="third">第三个盒子</div>
</div>
</body>
</html>
效果:
二、相对定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- 相对定位
相对于自己原来的位置进行偏移
-->
<style>
body{
padding: 0px;
}
div{
margin: 10px;
padding: 5px;
font-size: 12px;
line-height: 25px;
}
#father{
border: 1px solid #C850C0;
padding: 0px;
}
#first{
background-color: #a0a0a0;
border: 1px dashed #78c841;
position: relative;
top: -20px;/*相对定位,上下左右*/
left: 20px;
}
#second{
background-color: #FFCC70;
border: 1px dashed #2a3bc8;
}
#third{
background-color: aqua;
border: 1px dashed #c84614;
position: relative;
bottom: -10px;
right: 20px;
}
</style>
</head>
<body>
<div id="father">
<div id="first">第一个盒子</div>
<div id="second">第二个盒子</div>
<div id="third">第三个盒子</div>
</div>
</body>
</html>
效果:
相对定位:position:relative
相对于原来的位置,进行指定的偏移,对于相对定位来说,他仍然在标准文档流中,原来的位置会被保留。
1、top:上
2、left:左
3、bottom:下
4、right:右
案例:
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#box{
width: 300px;
height: 300px;
padding: 10px;
border: 2px solid red;
}
a{
width: 100px;
height: 100px;
text-decoration: none;
background-color: #C850C0;
line-height: 100px;
text-align: center;
color: white;
display: block;
}
a:hover{
background-color: blue;
}
.a2 , .a4{
position: relative;
left: 200px;
top: -100px;
}
.a5{
position: relative;
left: 100px;
top: -300px;
}
</style>
</head>
<body>
<div id="box">
<a href="#" class="a1">链接1</a>
<a href="#" class="a2">链接2</a>
<a href="#" class="a3">链接3</a>
<a href="#" class="a4">链接4</a>
<a href="#" class="a5">链接5</a>
</div>
</body>
</html>
效果:
三、绝对定位
定位:
1、没有父级元素定位的前提下,相对于浏览器定位
2、假设父级元素存在定位,我们通常会相对于父级元素进行偏移
3、在父级元素范围内移动
相对于父级或浏览器的位置,进行指定的偏移,绝对定位的话,它不在标准文档流中,原来的位置不会被保留。
案例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
margin: 10px;
padding: 5px;
font-size: 12px;
line-height: 25px;
}
#father{
border: 1px solid #C850C0;
padding: 0px;
position: relative;
}
#first{
background-color: #a0a0a0;
border: 1px dashed #78c841;
}
#second{
background-color: #FFCC70;
border: 1px dashed #2a3bc8;
position: absolute;
left: 100px;
}
#third{
background-color: aqua;
border: 1px dashed #c84614;
}
</style>
</head>
<body>
<div id="father">
<div id="first">第一个盒子</div>
<div id="second">第二个盒子</div>
<div id="third">第三个盒子</div>
</div>
</body>
</html>
效果:
四、固定定位fixed
案例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
height: 1000px;
}
div:nth-of-type(1){/*绝对定位,相对于浏览器*/
width: 100px;
height: 100px;
background: red;
position: absolute;
right: 0;
bottom: 0;
}
div:nth-of-type(2){/*fixed,固定定位*/
width: 50px;
height: 50px;
background: yellow;
position: fixed;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<div>div1</div>
<div>div2</div>
</body>
</html>
效果:
五、z-index
相当于PS中图层的概念,最低是0,最高无限制。
案例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
img{
width: 400px;
height: 300px;
}
#content{
width: 400px;
padding: 0;
margin: 0;
overflow: hidden;
font-size: 12px;
line-height: 25px;
border: 1px solid black;
}
ul , li {
padding: 0;
margin: 0;
list-style: none;
}
/* 父级元素相对定位*/
#content ul{
position: relative;
}
.tipText , .tigBg{
position: absolute;
width: 400px;
height: 25px;
top: 275px;
}
.tipText{
color: white;
z-index: 999;
}
.tigBg{
background: black;
opacity: 0.5; /*背景透明度*/
}
</style>
</head>
<body>
<div id="content">
<ul>
<li><img src="images/a.jpeg" alt=""></li>
<li class="tipText">繁华都市,使人驻足</li>
<li class="tigBg"></li>
<li>时间:2100-06-05</li>
<li>地点:大千世界</li>
</ul>
</div>
</body>
</html>
效果: