层叠上下文 stacking context 示例

本文深入探讨了CSS中的层叠上下文,通过实例展示了不同定位方式(如absolute、float、z-index等)对元素显示的影响。文章还涵盖了背景图片设置和jQuery获取元素的方法,是理解CSS布局和定位的良好教程。
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>层叠上下文</title>
    <script src="js/jquery-3.4.1.min.js"></script>
    <style>
        body {
            background-color: white;
            background-image:
                radial-gradient(hsla(0, 0%, 87%, 0.31) 9px, transparent 10px),
                repeating-radial-gradient(hsla(0, 0%, 87%, 0.31) 0,
                    hsla(0, 0%, 87%, 0.31) 4px, transparent 5px,
                    transparent 20px, hsla(0, 0%, 87%, 0.31) 21px,
                    hsla(0, 0%, 87%, 0.31) 25px, transparent 26px,
                    transparent 50px);
            background-size: 30px 30px, 90px 90px;
            background-position: 0 0;
        }

        .one,
        .two,
        .three,
        .four,
        .five,
        .six {
            width: 200px;
            height: 200px;
        }

        .one {
            position: absolute;
            z-index: -1;
            background: #8874c1;
        }

        .two {
            background: #4f6fc1;
            margin-left: 100px;
        }

        .three {
            float: left;
            background: #51cd8d;
            margin-top: -100px;
        }

        .four {
            display: inline-block;
            background: #9cd262;
            margin-left: -100px;
        }

        .five {
            position: absolute;
            background: #d9ac4c;
            margin-top: -130px;
            margin-left: 50px;
        }

        .six {
            position: absolute;
            z-index: 1;
            background: #d93953;
            margin-top: -50px;
            margin-left: 150px;
        }
    </style>
</head>

<body>
    <div class="one">one —— z-index为负值</div>
    <div class="two">two —— block块状水平盒子</div>
    <div class="three">three —— 浮动盒子</div>
    <div class="four">four —— inline/inline-block水平盒子</div>
    <div class="five">five —— z-index:auto/z-index:0</div>
    <div class="six">six —— z-index为正值</div>
</body>
<script>
    //如何用jquery获得每个ul下最后一个li
    /*     $(function () {

            $("#mg").each(function () {
                var y = $(this).children().last();
                // alert(y.text());
                y[y.length - 1].style.marginLeft = "200px";
                console.log(y[y.length - 1].style.marginLeft);
            });

        }); */
</script>

</html>

转自: https://www.jianshu.com/p/8645b29f96b6

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值