使用position的fixed和absolute二者区别

我们知道,css的position定位属性中,表示绝对定位的有两个值,值absolute与值fixed。

都是绝对定位,二者有一致性,也有不同点。那么二者的区别是什么呢?那就是position的fixed值定位的元素会固定在原来的位置不变,不管你如何拖动滚动条,元素都不会改变位置,这从fixed这个英文单词的意思也可以看出,“固定的,不变的,固执的”。而absolute正好相反,拖动滚动条时元素会随着改变位置。

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
div{width:100px; height:100px; background: green;}
#left{position:absolute; left:0; top:100px;}
#right{position:fixed; right:0; top:100px;}
</style>
</head>
<body style="height:1000px">
<div id="left">absolute</div>
<div id="right">fixed</div>
</body>

</html>

猜你喜欢

转载自blog.csdn.net/xiaoxinshuaiga/article/details/83274915
今日推荐