定位层级关系(Z-index标签层叠)详解以及标签包含规范

定位层级关系(Z-index标签层叠)

当对多个标签设置定位时,定位标签之间有可能会发生重叠,要想改变重叠的顺序,可以对定位标签应用z-index属性,取值可取正整数,负数和0,默认状况下z-index属性值为0,并且取值越大,设置该属性的定位标签在层叠标签中越居上

☞ 只有定位(静态定位除外)的元素才有层级关系
☞ 通过z-index属性去设置(改变)元素的层级关系
☞ 默认的z-index: auto(或者为0)
层级特点:
1.如果z-index值相同,那么后面的定位元素会压着前面的定位元素(后来居上)2.如果z-index值不相同,那么z-index的值越大,元素的层级越高。
3.如果子元素的父元素设置了定位(绝对定位或者相对定位或者固定定位),父元素层级(z-index)越大,该元素的层级越高。
注意:
◆通过z-index改变元素的层级关系的时候,一定要保证当前元素属于定位元素。
◆z-index的取值可以是负数。

全屏切换

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        body,html{
            width: 100%;
            height: 100%;
        }
        .box{
            width: 100%;
            height: 100%;
            position: relative;
        }
        .box div{
            width: 100%;
            height: 100%;
        }
        .one{
            background:url("images/2.jpg");
            background-size: cover;
            position: absolute;
        }
        .two{
            background:url("images/3.jpg");
            background-size: cover;
            position: absolute;
        }
        .box .pages{
            width: 210px;
            height: 100px;
            position: absolute;
            z-index: 30;
            bottom: 0px;
            left: 50%;
            margin-left: -105px;
        }
        .box a{
            display: inline-block;
            border-radius: 50%;
            width: 100px;
            height: 100px;
            line-height: 100px;
            text-align: center;
            background:rgba(0,0,0,.6);
            font-size: 20px;
            color: white;
            text-decoration: none;
        }
        .one:target{
            z-index: 2;
        }
    </style>
</head>
<body>
<div class="box">
    <div class="one" id="one"></div>
    <div class="two" id="two"></div>
    <div class="pages">
        <a href="#one">1</a>
        <a href="#two">2</a>
    </div>
</div>
</body>
</html>

在这里插入图片描述

标签包含规范

1.推荐行内元素包含行内元素。
2.最好段落标签中不能出现标题。
3.段落中最好不要出现div
发布了37 篇原创文章 · 获赞 3 · 访问量 755

猜你喜欢

转载自blog.csdn.net/dwjdj/article/details/104081378