好好爱自己!

[转]css父元素背景覆盖其子元素背景, 正常一般是子元素的背景会覆盖父元素的背景

原文:https://www.cnblogs.com/zouqin/p/5498495.html

问题: 怎么实现 父元素的背景覆盖子元素的背景呢?

 

————————————————————————————

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>

            .test1{
                width: 100px;
                height: 100px;
                border: 1px solid green;
                background: green;
                position: absolute;
                z-index: auto;
            }

            .test2{
                width: 100px;
                height: 100px;
                border: 1px solid black;
                background: black;
                position: relative;
                left: 20px;
                top: -20px;
                z-index: -1;
            }
        </style>
    </head>
    <body>
        <div class="test1">
            <div class="test2"></div>

        </div>

    </body>
</html>

  

父元素不设置z-index 的值时,默认是auto。此时在设置子元素的值为负数

了解一下,同一个“堆叠上下文中,元素的堆叠顺序”??

 
分类: CSS
 
 
« 上一篇: 

 

posted @ 2020-11-17 00:16  立志做一个好的程序员  阅读(2507)  评论(0编辑  收藏  举报

不断学习创作,与自己快乐相处