CSS布局中的定位

定位可以将一个盒子固定在一个位置。

使用方法:             选择器 {

                                             position:属性值;        

                               }       

 

属性值:

                              static                        静态定位(默认值)

                              relative                     相对定位

                              absolute                   绝对定位

                              fixed                         固定 定位 

相对定位 position:relative,两个块级元素上下排列,给第一个盒子加上相对定位,可以看到下面的盒子没有发生变化,并且可以随便移动第一个盒子,说明相对定位元素即使移走也会占位置。且相对定位会根据父元素来移动位置。代码和效果如下:

<!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>
        .box {
            width: 500px;
            height: 500px;
            background-color: blue;
            margin-left: 100px;
        }

        .box1 {
            width: 200px;
            height: 200px;
            background-color: pink;
            position: relative;
            left: 100px;
        }

        .box2 {
            width: 100px;
            height: 100px;
            background-color: gold;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box1"></div> 
        <div class="box2"></div>
    </div>
</body>
</html>

绝对定位 position:absolute,两个块级元素上下排列,给第一个盒子加上相对定位,可以看到下面的盒子会跑上去跟第一个盒子并排,说明绝对定位不会占位置,下面的元素会移动。绝对定位会根据带有定位的父元素来移动,会一层一层网上找定位,如果都无定位会根据浏览器来移动。代码和对比效果如下:

<!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>
        .box {
            width: 500px;
            height: 500px;
            background-color: blue;
            margin-left: 100px;
        }

        .box1 {
            width: 200px;
            height: 200px;
            background-color: pink;
            position: absolute;
            left: 100px;
        }

        .box2 {
            width: 100px;
            height: 100px;
            background-color: gold;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box1"></div> 
        <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>
        .box {
            width: 500px;
            height: 500px;
            background-color: blue;
            margin-left: 100px;
            position: relative;
        }

        .box1 {
            width: 200px;
            height: 200px;
            background-color: pink;
            position: absolute;
            left: 100px;
        }

        .box2 {
            width: 100px;
            height: 100px;
            background-color: gold;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box1"></div> 
        <div class="box2"></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>Document</title>
    <style>
        .box {
            width: 500px;
            height: 500px;
            background-color: blue;
            margin-left: 100px;
        }

        .box1 {
            width: 200px;
            height: 200px;
            background-color: pink;
            position: fixed;
            left:0;
            top: 300px;
        }

        .box2 {
            width: 100px;
            height: 100px;
            background-color: gold;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box1"></div> 
        <div class="box2"></div>
    </div>
</body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值