元素的层级与绝对定位布局

目录

一、效果展示

二、元素层级

1、效果

2、特点

3、修改

三、绝对定位布局

1、水平垂直布局公式(在绝对定位下必须满足)

2、要求

四、代码

1、元素层级

2、绝对定位布局


一、效果展示

  

二、元素层级

1、效果

层级高的元素会覆盖层级低的元素

2、特点

(1)父元素层级永远不会超过子元素大小

(2)同层级优先显示标签靠后的元素

3、修改

(1)通过z-index修改层级优先级大小

(2)z-size需要数字参数,愈大层级越高

三、绝对定位布局

1、水平垂直布局公式(在绝对定位下必须满足)

(1)水平

left + margin-left\right + padding-left\right + content + right = width

(2)垂直

top + margin-top\bottom + padding-top\bottom + bottom = height

2、要求

(1)left、right、top、bottom具有最高优先级

(2)在过度约束条件下,若不对此四个值限定,则默认为auto,优先修改此四类值

四、代码

1、元素层级

<!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>元素的层级</title>
    <!-- 
        1.通过z-index修改层级优先级大小
        2.父元素层级永远不会超过子元素大小
        3.同层级优先显示标签靠后的元素

     -->
    <style>
        .box1{
            width: 200px;
            height: 200px;
            background-color: #bfa;
            position: relative;
            z-index: 1;
        }
        .box2{
            width: 200px;
            height: 200px;
            background-color: red;
            position: absolute;
            left: 50px;
            top: 50px;
            z-index: 2;
        }
        .box3{
            width: 200px;
            height: 200px;
            background-color: yellow;
            position: absolute;
            left: 100px;
            top: 100px;
            z-index: 3;
        }
        .box4{
            width: 100px;
            height: 100px;
            background-color: blue;
            position: absolute;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3">
        <div class="box4"></div>
    </div>
</body>
</html>

2、绝对定位布局

<!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>绝对定位布局</title>
    <!-- 
        水平垂直布局公式(在绝对定位下必须满足):
        1.水平
            left + margin-left\right + padding-left\right + content + right = width
        2.垂直
            top + margin-top\bottom + padding-top\bottom + bottom = height
        3.要求
            -left、right、top、bottom具有最高优先级
            -在过度约束条件下,若不对此四个值限定,则默认为auto,优先修改此四类值
     -->
    <style>
      .box1 {
        background-color: #bfa;
        width: 400px;
        height: 400px;
        position: relative;
      }
      .box2 {
        background-color: red;
        width: 50px;
        height: 50px;
        margin: auto;
        position: absolute;
        left: 0px;
        right: 0px;
        top: 0px;
        bottom: 0px;
      }
    </style>
  </head>
  <body>
    <div class="box1">
      <div class="box2"></div>
    </div>
  </body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值