好好爱自己!

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

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

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

 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!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 @   立志做一个好的程序员  阅读(2600)  评论(0编辑  收藏  举报
努力加载评论中...

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

点击右上角即可分享
微信分享提示