position: fixed属性用法详解

巴扎黑
发布: 2017-07-18 17:08:17
原创
2268 人浏览过

<!DOCTYPE html>
<html>
<head>
<title>当锚点定位遇上position: fixed</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}

body {
position: relative;
}

a {
color: white;
text-decoration: none;
}

.nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100px;
background-color: black;
}

.part-one,
.part-two,
.part-three {
width: 100%;
height: 800px;
}

.part-one {
background-color: red;
padding-top: 100px;
}

.part-two {
background-color: blue;
padding-top: 100px;
margin-top: -100px;
}

.part-three {
background-color: yellow;
padding-top: 100px;
margin-top: -100px;
}
</style>
</head>
<body>
<div class="nav">
<a href="#part-one">part one</a>
<a href="#part-two">part two</a>
<a href="#part-three">part three</a>
</div>
<div class="part-one" id="part-one">I&#39;m part one</div>
<div class="part-two" id="part-two">I&#39;m part two</div>
<div class="part-three" id="part-three">I&#39;m part three</div>
</body>
</html>
登录后复制
<!DOCTYPE html>
<html>

	<head>
		<title>CSS解决有固定导航时链接锚点定位偏移</title>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}
			
			body {
				position: relative;
			}
			
			a {
				color: white;
				text-decoration: none;
			}
			
			.nav {
				position: fixed;
				top: 0;
				left: 0;
				width: 100%;
				height: 100px;
				background-color: black;
			}
			
			.part-one,
			.part-two,
			.part-three {
				width: 100%;
				height: 800px;
			}
			
			.part-one {
				background-color: red;
				padding-top: 100px;
			}
			
			.part-two {
				background-color: blue;
				padding-top: 100px;
				margin-top: -100px;
			}
			
			.part-three {
				background-color: yellow;
				padding-top: 100px;
				margin-top: -100px;
			}
		</style>
	</head>

	<body>
		<div class="nav">
			<a href="#part-one">part one</a>
			<a href="#part-two">part two</a>
			<a href="#part-three">part three</a>
		</div>
		<div class="part-one" id="part-one">I&#39;m part one</div>
		<div class="part-two" id="part-two">I&#39;m part two</div>
		<div class="part-three" id="part-three">I&#39;m part three</div>
	</body>

</html>
登录后复制

  

以上是position: fixed属性用法详解的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!