css定位(position)

css定位(position)

css定位是用来指定元素在文档中的定位方式。不决定元素在文档中的最后位置
position
从图中可以看出,css中的position属性目前市面上的大多数浏览器全都支持。

position属性值

position的属性值目前MDN官方文档上是五个,我在写这篇文章之前只知道四个,第五种也是刚学,
如果有一些错误的地方,敬请指正

position属性值有五个,分别是:

position:static

静态定位

这是一个关键字,元素设置了这个属性,就表示他是正常的布局方式,如果再设置了top、 bottom、left、right、z-index等属性,那么这些属性就不会起作用,失效了
接下来看一个例子,首先是正常布局,加上了top,left等属性:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定位</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        div {
            width: 100px;
            height: 100px;
            outline: 1px solid;
            background-color: red;
            position: relative;
            top: 20px;
            left: 20px;
        }
    </style>
</head>

<body>
    <div></div>
</body>

</html>

运行截图如下:
static
这是没有给div增加position:static属性的样子。可以看出来,红色的div设置的top,left,属性,都能起效果,接下来,我把static属性,给div加上,那么会发生什么呢?

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定位</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        div {
            width: 100px;
            height: 100px;
            outline: 1px solid;
            background-color: red;
            position: relative;
            top: 20px;
            left: 20px;
            position: static;
        }
    </style>
</head>

<body>
    <div></div>
</body>

</html>

运行结果如下:
static
这样从运行的结果上可以看出,红色div设置的top,left属性并没有起效果。

说到这,可能有一些初学者就会发现,我还在div的css样式加上了position: relative;我就先卖个关子,这个属性我会在下面讲解。

其实这个position: static;属性,我本人可能太菜了,我并没有在实际场景中用过,其实我把div中的两个position属性都去掉,这个div的top,left,也是一样不起效果的:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定位</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        div {
            width: 100px;
            height: 100px;
            outline: 1px solid;
            background-color: red;
            top: 20px;
            left: 20px;
        }
    </style>
</head>

<body>
    <div></div>
</body>

</html>

运行结果如下:
没有position

可能有一些初学者又有疑惑了,这是怎么回事呢?
其实这很简单,这只是因为div在文档常规流中,所以给他设置top,left等属性不起效果。

position:relative

相对定位

相对定位不脱离文档流,它是相对于自己原来的位置进行移动,结合着top,left等属性进行一定量的偏移。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定位</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        div {
            width: 100px;
            height: 100px;
            outline: 1px solid;
            background-color: red;
        }
        
        div:nth-of-type(2) {
            position: relative;
            left: 100px;
            top: 20px;
            background-color: blueviolet;
        }
    </style>
</head>

<body>
    <div></div>
    <div></div>
    <div></div>
</body>

</html>

运行结果如下:
relative
从运行结果上可以看出,我给第二个div设置了position:relative属性,第二个div是相对于自己的原来位置进行偏移的。一般相对定位用来微调元素的位置。

position:absolute

绝对定位

元素设置这个属性,就会脱离正常的文档流,要想进行偏移移动,要给元素一个参照物,一般是设置了position:relative属性的父元素、或者是最近的非 static 定位祖先元素。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定位</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        div {
            width: 100px;
            height: 100px;
            outline: 1px solid;
            background-color: red;
        }
        
        div:nth-of-type(3) {
            background-color: brown;
        }
        
        div:nth-of-type(3) .container {
            left: 100px;
            top: 20px;
            background-color: blueviolet;
        }
    </style>
</head>

<body>
    <div></div>
    <div></div>
    <div>
        <div class="container"></div>
    </div>
</body>

</html>

运行结果如下:
position:absolute
从效果图中可以看出,没有设置绝对定位,蓝色的div在正常的文档流中正常显示。

蓝色外面的div明明也设置了颜色,为什么不显示呢,这是由于里面和外面的一样大小,所以盖住了。
设置大一点
这就可以解释了,我给外面的div设置了大一下,所以就显示出来。

好了,继续回归绝对定位。现在我给外面的div设置相对定位,给里面的div设置绝对定位,我们一起来看结果:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定位</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        div {
            width: 100px;
            height: 100px;
            outline: 1px solid;
            background-color: red;
        }
        
        div:nth-of-type(3) {
            background-color: brown;
            position: relative;
        }
        
        div:nth-of-type(3) .container {
            position: absolute;
            left: 100px;
            top: 20px;
            background-color: blueviolet;
        }
    </style>
</head>

<body>
    <div></div>
    <div></div>
    <div>
        <div class="container"></div>
    </div>
</body>

</html>

运行结果如下:
绝对定位
从运行的结果我们可以看出来,设置了绝对定位的元素只要它的父元素设置了相对定位,那么设置了绝对定位的元素就会相对于设置相对定位的元素进行相对的一些偏移

position:fixed

固定定位

固定定位,自然是脱离正常的文档流的,设置固定定位,那么元素就相当于是把浏览器的视口当作参照的,自然也就是相对于浏览器的视口进行定位的。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定位</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            width: 100%;
            height: 2000px;
        }
        
        div {
            width: 100px;
            height: 100px;
            outline: 1px solid;
            background-color: red;
        }
        
        div:nth-of-type(3) {
            background-color: brown;
        }
        
        div:nth-of-type(3) .container {
            position: fixed;
            left: 100px;
            top: 100px;
            background-color: blueviolet;
        }
    </style>
</head>

<body>
    <div></div>
    <div></div>
    <div>
        <div class="container"></div>
    </div>
</body>

</html>

运行结果如下:
固定定位
从图中可以看出,设置了固定定位的元素是相对于浏览器进行定位的,跟它的父元素没有关系
固定定位
无论浏览者怎么滚动浏览器的视口,设置了固定定位的元素都不会改变。

position:sticky

粘性定位

这是css3中新出的一个定位属性:

元素根据正常文档流进行定位,然后相对它的最近滚动祖先(nearest scrolling ancestor)和 containing block (最近块级祖先 nearest block-level ancestor),包括 table-related 元素,基于top, right, bottom, 和 left的值进行偏移。偏移值不会影响任何其他元素的位置。
该值总是创建一个新的层叠上下文(stacking context)。注意,一个 sticky 元素会“固定”在离它最近的一个拥有“滚动机制”的祖先上(当该祖先的overflow 是 hidden, scroll, auto, 或 overlay时),即便这个祖先不是最近的真实可滚动祖先。这有效地抑制了任何“sticky”行为(详情见Github issue on W3C CSSWG)。这是MDN官方文档的解释

我解释一下,通俗的说,这个粘性定位可以说是相对定位固定定位的组合,把他们两个定位属性合二为一了。
通过一个小例子来解释一下:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定位</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            height: 2000px;
        }
        
        div:nth-of-type(1) {
            width: 100px;
            height: 50px;
            background-color: cadetblue;
        }
        
        div:nth-of-type(2) {
            position: sticky;
            width: 50px;
            height: 50px;
            margin-left: 20px;
            margin-top: 20px;
            top: 50px;
            background-color: red;
        }
    </style>
</head>

<body>
    <div></div>
    <div></div>
</body>

</html>

运行结果如下:
粘性定位
现在距离浏览器的顶部是50px,接下来,我滚动浏览器的窗口,把距离缩小一点:
粘性定位
可以看到,只要往下移动,那么它就是固定定位了,需要注意几个点

  1. 需要指定 toprightbottomleft 四个值其中之一,才可以使粘性定位起效果。否则它的作用就与相对定位相同。并且 topbottom 同时设置时,top 起效果的优先级高;leftright 同时设置时,left 起效果的优先级高。
  2. 父元素的高度不能低于 设置了sticky的元素的高度,否则就失效。
  3. 如果父元素没有设置定位,那么就相对于浏览器的视口进行定位,否则就以设置了定位的父元素为参考点。

小菜鸡一枚。
若有错误,请在评论区中指正批评

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端志茗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值