四、固定定位
固定定位就是相对浏览器窗口定位。无论页面如何滚动,这个盒子显示的位置不变
(返回顶部按钮、导航栏)
固定定位会脱离标准文档流。
p{
width: 100px;
height: 100px;
background-color: orange;
position: fixed;
top: 100px;
left: 100px;
}
4.1 固定定位的用途:返回顶部按钮
.backtop{
position: fixed;
bottom: 100px;
right: 30px;
width: 60px;
height: 60px;
background-color: gray;
text-align: center;
line-height:30px;
color:white;
text-decoration: none;
}
<a class="backtop">返回<br />顶部</a>
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
4.2 固定定位的用途:顶部导航条的固定
* {
margin: 0;
padding: 0;
}
.nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 60px;
background-color: #333;
z-index: 99999999;
}
.inner_c {
width: 1000px;
height: 60px;
margin: 0 auto;
}
.inner_c ul {
list-style: none;
}
.inner_c ul li {
float: left;
width: 100px;
height: 60px;
text-align: center;
line-height: 60px;
}
.inner_c ul li a {
display: block;
width: 100px;
height: 60px;
color: white;
text-decoration: none;
}
.inner_c ul li a:hover {
background-color: gold;
}
<div class="nav">
<div class="inner_c">
<ul>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
</ul>
</div>
</div>
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
p {
width: 100px;
height: 100px;
background-color: orange;
position: fixed;
top: 100px;
left: 100px;
}
</style>
</head>
<body>
<p>阿萨德</p>
<img src="images/0103-large.jpg" alt="">
</body>
</html>