CSS14:定位

文章详细阐述了CSS中的position属性,包括relative、absolute和fixed三种定位方式。relative为相对定位,保持在文档流中的位置并可调整;absolute实现绝对定位,脱离文档流;fixed定位则是元素位置相对于浏览器窗口固定,不会随滚动条移动。示例代码展示了不同定位方式的效果,以及如何影响元素层次和布局。
摘要由CSDN通过智能技术生成

定义:

position属性指定了元素的定位类型。

  • relative:相对定位
  • absolute:绝对定位
  • fixed:固定定位

其中绝对定位和固定定位会脱离文档流。

四个方向值:top,bottom,left,right。


相对定位

<!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>
        div{
            width: 200px;
            height: 200px;
            background-color: aquamarine;
            /* 相对定位 */
            position: relative;
            top: 200px;
            left: 200px;
        }
    </style>
</head>
<body>
    <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>
        .box1{
            width: 200px;
            height: 200px;
            background-color: aquamarine;
            /* 绝对定位 */
            position: absolute;
            top: 100px;
            left: 200px;
        }

        /* 证明是否脱离文档流 */
        .box2{
            width: 300px;
            height: 300px;
            background-color: salmon; 
        }
        
        .box3{
            width: 200px;
            height: 50px;
            background-color: pink;
            /* 绝对定位 */
            position: absolute;
            left: 50px;
            top: 100px; 
        }
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
</body>
</html>

box3之所以会覆盖住一部分box1,是因为每一次绝对定位就会创建一层浮动页面,如所示,实际上有三层图层。(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>
        .box1{
            width: 200px;
            height: 200px;
            background-color: red;
            position: fixed;
            top: 100px;
            left: 100px;
        }

        /* 证明是否脱离文档流 */
        .box2{
            width: 300px;
            height: 300px;
            background-color: blue; 
        }
        
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
</body>
</html>


设置定位之后,相对定位和绝对定位是相对于具有定位的父级元素进行位置调整,如果父级元素不存在定位,则继续向上逐级寻找,直到顶层文档。

 例如:container(container1(box))

<!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>
        /* container和container1都在文档流 */
        .container{
            width: 200px;
            height: 200px;
            background-color: pink;
            /* position: relative; */
            margin: 100px;
        }
        .container1{
            width: 200px;
            height: 200px;
            background-color: yellowgreen;
            /* position: relative; */
            margin: 200px;
        }
        .box{
            width: 100px;
            height: 100px;
            background-color: blue;
             /*  绝对定位  */
            position: absolute;
            top: 50px;
            left: 50px;
        }

        /* box父级元素无定位,所以位置移动是相对于页面 */
        
    </style>
</head>
<body>
    <div class="container">
        <div class="container1">

            <div class="box"></div>
        </div>

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

 

改变1:

.container1{
            width: 200px;
            height: 200px;
            background-color: yellowgreen;
            /* 相对定位 */
            position: relative;
            margin: 200px;
        }

改变2:

.container{
            width: 200px;
            height: 200px;
            background-color: pink;
            /* 相对定位 */
            position: relative;
            margin: 100px;
        }
        .container1{
            width: 200px;
            height: 200px;
            background-color: yellowgreen;
            margin: 200px;
        }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值