css-postion定位

参考:https://www.cnblogs.com/guolao/p/9048308.html

static 定位

HTML 元素的默认值,即没有定位,遵循正常的文档流对象。

静态定位的元素不会受到 top, bottom, left, right影响。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS-position-static</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/normalize/8.0.0/normalize.css">
    <style>
        .container{
            background-color: #868686;
            width: 100%;
            height: 300px;
        }
        .content{
            background-color: yellow;
            width: 100px;
            height: 100px;
            position: static;
            left: 10px;/* 这个left没有起作用 */
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="content">    
        </div>
    </div>
</body>
</html>

对 content 的 position 设定 static 后,left就失效了,而元素(content)就以正常的 normal flow 形式呈现。

 

relative定位

个人理解:相对于normal flow中的原位置来定位。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>CSS-position-relative</title>  
    <link rel="stylesheet" href="https://cdn.bootcss.com/normalize/8.0.0/normalize.css">
    <style>
            .container{
                background-color: #868686;
                width: 100%;
                height: 300px;
            }
            .content_0{
                background-color: yellow;
                width: 100px;
                height: 100px;               
            }
            .content_1{
                background-color: red;
                width: 100px;
                height: 100px;
                position: relative;/* 这里使用了relative  */            
            }
            .content_2{
                background-color: black;
                width: 100px;
                height: 100px;               
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="content_0">    
            </div>
            <div class="content_1">    
            </div>
            <div class="content_2">    
            </div>
        </div>   
</body>
</html>

这是没有设置left、top等属性时,正常出现在normal flow中的位置。

接着添加left、top:

.content_1{
                background-color: red;
                width: 100px;
                height: 100px;
                position: relative;/* 这里使用了relative  */
                left: 20px;/* 这里设置了left和top */
                top: 20px;            
            }

可以看到,元素(content_1)的位置相对于其原位置(normal flow中的正常位置)进行了移动。

absolute定位

个人理解:生成绝对定位的元素,其相对于 static 定位以外的第一个父元素进行定位,会脱离normal flow。注意:是除了static外

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>CSS-position-static</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/normalize/8.0.0/normalize.css">
    <style>
        .container{
            background-color: #868686;
            width: 100%;
            height: 300px;
            margin-top: 50px;
        }
        .content{
            background-color: red;
            width: 100px;
            height: 100px;
            position: absolute;
            top: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="content">    
        </div>
    </div>
</body>
</html>

因为 content 的父元素 container 没有设置 position,默认为 static,所以找到的第一个父元素是 body(<body></body>),可以看成是元素(content)相对于 body 向下移动10px。

fixed定位

个人理解:fixed相对于浏览器窗口固定,滚动浏览器窗口并不会使其移动,会脱离normal flow。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>CSS-position-static</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/normalize/8.0.0/normalize.css">
    <style>
        .container{
            background-color: #868686;
            width: 100%;
            height: 1000px;
        }
        .content{
            background-color: yellow;
            width: 100px;
            height: 100px;
            position: fixed;/* 这里使用了fixed */
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="content">    
        </div>
    </div>
</body>
</html>

sticky定位

(了解)盒位置根据正常流计算(这称为正常流动中的位置),然后相对于该元素在流中的 flow root(BFC)和 containing block(最近的块级祖先元素)定位。在所有情况下(即便被定位元素为 table),该元素定位均不对后续元素造成影响。当元素 B 被粘性定位时,后续元素的位置仍按照 B 未定位时的位置来确定。position: sticky对 table元素的效果与 position: relative 相同。

inherit定位

规定应该从父元素继承 position 属性的值。

inherit 继承父元素,这个用得不多,所以也不继续深入了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值