CSS的定位属性

目录

绝对定位(position:absolute)

文档流

层叠顺序:

应用

 相对定位( position:relative)

文档流

 层叠顺序

 应用

 固定定位(position:fixed)

文档流

层叠顺序

应用

 层叠顺序的改变(z-index)


使用场景:想要覆盖盒子或移动盒子位置的时候使用。

绝对定位(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>
        *{
            padding: 0;
            margin: 0;
        }
        div{
            width: 200px;
            height: 200px;
            background-color: red;
            /* 绝对定位 */
            position: absolute;

        }
        h1{
            width: 400px;
            height: 300px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <!-- 并列结构下通常为竖着排列 -->
    <div></div>
    <h1></h1>
</body>
</html>

运行结果: 

层叠顺序:

默认情况下,当多个盒子同时给了定位,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>
        div:nth-of-type(1){
            width: 300px;
            height: 300px;
            background-color: red;
            position: absolute;
        }
        div:nth-of-type(2){
            width: 200px;
            height: 200px;
            background-color: blue;
            position: absolute;
        }
        div:nth-of-type(3){
            width: 100px;
            height: 100px;
            background-color: purple;
            position: absolute;
        }
    </style>
</head>
<body>
    <!-- 多个盒子绝对定位时,html结构在后面的最先显示 -->
    <div></div>
    <div></div>
    <div></div>

</body>
</html>

运行结果: 

应用

可以通过top,left,right,bottom移动位置。参照物可以是浏览器窗口的第一屏,或者有定位设置的父元素。

参照物为第一屏时:

代码:

<!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>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 100px;
            height: 100px;
            background-color: red;
            /* 绝对定位 */
            position: absolute;
            /* 设置属性 */
            left: 200px;
            top: 500px;
        }
    </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>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 500px;
            height: 500px;
            margin-left:100px;;
            background-color: red;
            /* 相对定位 */
            position: relative;
        }
        p{
            width: 50px;
            height: 50px;
            background-color: black;
            /* 绝对定位 */
            position: absolute;
            left: 10px;
            top: 200px;
        }
    </style>
</head>
<body>
    <div>
        <p></p>
    </div>
</body>
</html>

运行结果: 

 相对定位( 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>
        *{
            padding: 0;
            margin: 0;
        }
        div{
            width: 200px;
            height: 200px;
            background-color: red;
            /* 相对定位 */
            position: relative;

        }
        h1{
            width: 400px;
            height: 300px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <!-- 并列结构下通常为竖着排列 -->
    <div></div>
    <h1></h1>
</body>
</html>

运行结果:

 层叠顺序

默认情况下,当多个盒子同时给了相对定位,盒子没有层叠的视觉效果,但是移动位置之后,可以看到层叠效果,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>
        div:nth-of-type(1){
            width: 300px;
            height: 300px;
            background-color: red;
            position:relative;
        }
        div:nth-of-type(2){
            width: 200px;
            height: 200px;
            background-color: blue;
            position:relative;
            /* 通过给负数达到盒子向上位移的效果 */
            top:-100px;

        }
        div:nth-of-type(3){
            width: 100px;
            height: 100px;
            background-color: purple;
            position:relative;
            /* 通过给负数达到盒子向上位移的效果 */
            top:-150px;

        }
    </style>
</head>
<body>
    <!-- 多个盒子相对定位时,html结构在后面的最先显示 -->
    <div></div>
    <div></div>
    <div></div>

</body>
</html>

运行结果:

盒子没有层叠的视觉效果。

 移动位置之后,可以看到层叠效果。

 应用

可以通过top  left right bottom移动位置,参照物:  自己的初始位置。

注意点:即使移动了位置,原有空间还在。

 代码:

<!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>
        *{
            margin: 0;
            padding: 0;
        }
        div:nth-of-type(1){
            width: 100px;
            height: 100px;
            background-color: red;
            /* 相对定位 */
            position: relative;
            /* 设置属性 */
            top: -50px;
        }
        div:nth-of-type(2){
            width: 100px;
            height: 100px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div></div>
    <div></div>
</body>
</html>

运行结果:

 固定定位(position:fixed

文档流

一个盒子给了固定定位之后,该盒子是悬空不占位的,只是原地悬空不占位,不会进行位置移动。

层叠顺序

默认情况下,当多个盒子同时给了定位,HTML结构在后的定位层在上

应用

可以通过top  left right bottom移动位置,参照物:  浏览器的当前窗口(视口)

代码:

<!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>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            /* 给整个页面设置高度,出滚动条以便观察 */
            height: 5000px;
        }
        div{
            width: 100px;
            height: 100px;
            background-color: blue;
            /* 固定定位 */
            position: fixed;
            right: 100px;
            bottom: 100px;
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

运行结果:

移动前:

 移动后:

 层叠顺序的改变(z-index)

想要改变层叠顺序,可以通过属性z-index改变。

注意:

  1. 默认值是0
  2. 数值越大层越靠上
  3. 不带单位
  4. 没有最大值和最小值
  5. 可以给负数

代码:

<!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:nth-of-type(1){
            width: 300px;
            height: 300px;
            background-color: red;
            position: absolute;
        }
        div:nth-of-type(2){
            width: 200px;
            height: 200px;
            background-color: blue;
            position: absolute;
        }
        div:nth-of-type(3){
            width: 100px;
            height: 100px;
            background-color: purple;
            position: absolute;
            z-index: -1;
        }
    </style>
</head>
<body>
    <!-- 多个盒子绝对定位时,html结构在后面的最先显示 ,但更改z-index属性值以后显示顺序就会改变-->
    <div></div>
    <div></div>
    <div></div>

</body>
</html>

运行结果: 

以上就是常用的三个定位的详细解释,码字不易点个赞再走吧~~~~ 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大聪明码农徐

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

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

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

打赏作者

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

抵扣说明:

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

余额充值