8.3 父子元素z-index

277 阅读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>Document</title>
    <style>
        /* 父子元素开启定位流
            1.如果只给父元素设置z-index 不给子元素设置 
                无论父元素z-index多大 子优先级最高
            2.如果只给子元素设置z-index 不给父元素设置
                若z-index<0 则父元素优先级高 否则 子元素优先级高
            3.两个元素同时设置z-index 无论父元素多大
                子元素优先级永远最大
            
        
        */
        .parent{
            width: 200px;
            height: 200px;
            background-color: cyan;
            position: relative;
            z-index:10;
        }
        .child{
            width: 100px;
            height: 100px;
            background-color: coral;
            position: relative;
            z-index: 15;
        }
        
    </style>
</head>
<body>
    <div class="parent">
        <div class="child"></div>
    </div>
    <div class="parent1"></div>
</body>
</html>