学习目标:
掌握css的定位和层级显示
学习内容:
1、 固定定位 2、 绝对定位 3、 相对定位 4、 元素的显示方式 5、 元素的层级
学习时间:
2021年1月6日
学习产出:
1、 技术笔记 1遍 2、CSDN 技术博客 1 篇 3、 制作58同城登录界面
固定定位
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
#d1{
width: 700px;
height: 700px;
border: 1px solid red;
}
#d2{
width: 80px;
height: 40px;
line-height: 30px;
text-align: center;
background-color: #ccc;
position: fixed;
right: 5px;
bottom: 30px;
}
a{
text-decoration: none;
}
</style>
</head>
<body>
<h1>我是h1</h1>
<p>我是p1</p>
<p>我是p2</p>
<div id="d1">
<div id="d2">
<a href="#">回到顶部</a>
</div>
</div>
</body>
</html>
绝对定位
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
div{
width: 400px;
height: 300px;
border: 1px solid red;
position: relative;
}
p{
position: absolute;
bottom: 30px;
width: 400px;
background-color: #CCCCCC;
text-align: center;
line-height: 30px;
}
</style>
</head>
<body>
<div id="">
<img src="img/book1.jpg" >
<p>书山有路勤为径</p>
</div>
</body>
</html>
相对定位
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
#d1,#d2{
width: 200px;
height: 200px;
border: 1px solid red;
}
#d1{
position: relative;
top: 150px;
right: 5px;
}
</style>
</head>
<body>
<div id="d1">
我是d1
</div>
<p>定位时观察我的位置</p>
<div id="d2">
</div>
</body>
</html>
元素的显示方式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
#d1,#d2{
width: 100px;
height: 100px;
border: 1px solid red;
display: inline;
}
#d3{
width: 500px;
height: 500px;
border: 1px solid yellow;
}
img{
width: 200px;
height: 300px;
display: block;
}
#d4{
width: 200px;
height: 200px;
background-color: #006666;
display: none;
}
</style>
</head>
<body>
<div id="d1">d1</div>
<div id="d2">
d2
</div>
<div id="d3">
<img src="img/book3.jpg" >
<img src="img/book2.jpg" >
</div>
<div id="d4">
</div>
<p>观察我的位置</p>
</body>
</html>
元素的层级
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.box3{
width: 200px;
height: 200px;
background-color: yellow;
position: absolute;
z-index: 5;
}
.box4{
width: 100px;
height: 100px;
background-color: orange;
position: absolute;
z-index: 1;
}
</style>
</head>
<body>
<div class="box1">1</div>
<div class="box2">2</div>
<div class="box3">3
<div class="box4">
4
</div>
</div>
</body>
</html>