定位:static、fixed、relative、absolute

定位

定位方式有:staticfixedrelativeabsolute

Static 静态定位(默认值)

无定位,元素正常出现了流中,不受leftrighttopbottom影响

(1)

<style type="text/css">
        div{
            width: 200px;
            height: 200px;
            background-color: aqua;
            position: static;
        }
    </style>
</head>
<body>
    <div></div>
    静态定位的演示

</body> 


(2) 

  #div1{
            width: 100px;
            height: 100px;
            background-color: aqua;
            position: fixed;

        left: 30px;
        top: 20px;
        }
        #div2{
            width: 100px;
            height: 100px;
            background-color: darkorange;
        }
    </style>
</head>
<body>
    <div id="div1"></div>
    <div id="div2"></div>
</body>    

             

从结果可以看出,fix定位会将元素从流中“摘”出来单独进行定位取决于lefttop

重新定位之后可能会出现重叠,通过z-index可以调节其顺序,但是定位z-index无效

(3)

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style type="text/css">
        #div1{
            width: 100px;
            height: 100px;
            background-color: rgba(255,0,0,1);
        }
        #div2{
            width: 100px;
            height: 100px;
            background-color: greenyellow;
            position: relative;
            top: 30px;
            left: 30px;
        }
        #div3{
            width: 100px;
            height: 100px;
            background-color: blueviolet;
        }
    </style>
</head>
<body>
    <div id="div1"></div>
    <div id="div2"></div>
    <div id="div3"></div>
</body>

                  

从结果可以看出,相对定位是从原有的位置进行位移,但并不影响后续位置

(4)

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style type="text/css">
        #div1{
            width: 100px;
            height: 100px;
            background-color: rgba(255,0,0,1);
        }
        #div2{
            width: 100px;
            height: 100px;
            background-color: greenyellow;
            position: fixed;
            top: 30px;
            left: 30px;
        }
        #div3{
            width: 100px;
            height: 100px;
            background-color: blueviolet;
        }
    </style>
</head>
<body>
    <div id="div1"></div>
    <div id="div2"></div>
    <div id="div3"></div>
</body>

                  

从结果可以看出:绝对定位的元素将从流中被“摘”出来,依靠left属性进行定位。

fixed类似,但是参照物不同

Fixed 参照根元素(html

Absolute 参照父容器

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值