css定位布局及其特性

css定位布局及其特性

1、定位其实就是玩的一个position属性,就和浮动玩的是float属性一样。

position属性用于指定一个元素在文档中的定位方式,其中top,left,right,bottom属性则最终决定了该元素在文档中的位置。

position有五大属性分别是:

  1. static 静态定位
  2. relative 相对定位
  3. absolute 决定定位
  4. fixed 固定定位
  5. sticky 粘性定位
相对定位及其特性

相对定位的元素是在文档中的正常位置偏移给定的值。也可以理解为,相对定位的元素时相对元素本身在文档中的位置进行偏移的。

看个实例:

<!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>Document</title>
    <style>
        .box1{
            width:100px;
            height:100px;
            background:pink;
        }
        .box2{
            width:100px;
            height:100px;
            background:skyblue;
            position: relative;
            left:100px;
            top:100px;
        }
        .box3{
            width:100px;
            height:100px;
            background:red;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
</body>
</html>

在这里插入图片描述

绝对定位及其特性

绝对定位的元素脱离了文档流,并且不占据空间;绝对定位的元素是相对于最近的非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>Document</title>
    <style>
        .box1{
            width:100px;
            height:100px;
            background:pink;
        }
        .box2{
            width:100px;
            height:100px;
            background:skyblue;
            position: absolute;
            left: 50px;
            top: 50px; 
        }
        .box3{
            width:100px;
            height:100px;
            background:red;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
</body>
</html>

在这里插入图片描述

<!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>Document</title>
    <style>
        body{
            height:2000px;
            position: relative;
        }
        .box1{
            width:500px;
            height:500px;
            border:1px black solid;
            margin:200px;
            position: relative;
        }
        .box2{
            width:100px;
            height:100px;
            background:pink;
            position: absolute;
            left:0;
            top:0;
        }
    </style>
</head>
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
</html>

在这里插入图片描述
当我们把其祖先元素的position:relitive注释掉之后:
在这里插入图片描述

固定定位及其特性

固定定位是相对于可视区域定位,不受祖先元素影响。也就是在这个可视区域内你设置多少偏移量,它就偏移多少。

<!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>Document</title>
    <style>
        body{
            height:2000px;
            position: relative;
        }
        .box1{
            width:500px;
            height:500px;
            border:1px black solid;
            margin:200px;
            position: relative;
        }
        .box2{
            width:100px;
            height:100px;
            background:pink;
            position: fixed;
            left:0;
            top:0;
        }
    </style>
</head>
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
</html>

在这里插入图片描述

粘性定位及其特性

粘性定位可以看做是相对定位和固定定位的组合,元素在跨越特定阈值之前为相对定位,之后为固定定位

<!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>Document</title>
    <style>
        body{
            height:2000px;
        }
        p {
            margin-top: 20px;
        }
        div{
            position: sticky;
            top : 0;
        }
    </style>
</head>

<body>
    <p>pppppppppppppppppp</p>
    <p>pppppppppppppppppp</p>
    <p>pppppppppppppppppp</p>
    <p>pppppppppppppppppp</p>
    <p>pppppppppppppppppp</p>
    <p>pppppppppppppppppp</p>
    <div>这是一个粘性盒子</div>
    <p>pppppppppppppppppp</p>
    <p>pppppppppppppppppp</p>
    <p>pppppppppppppppppp</p>
    <p>pppppppppppppppppp</p>
    <p>pppppppppppppppppp</p>
    <p>pppppppppppppppppp</p>
    <p>pppppppppppppppppp</p>
    <p>pppppppppppppppppp</p>
</body>

</html>

在这里插入图片描述
在这里插入图片描述

以上就是定位啦,其实项目开发中运用最多的还是‘子绝父相’

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

jieyucx

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

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

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

打赏作者

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

抵扣说明:

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

余额充值