CSS @layer总结

文章目录

CSS @layer总结

概述

@layer 用于给 CSS 样式分层级,谁的等级高,就使用谁的样式。

@layer 不受选择器权重影响,除了!important和内联样式。

复制代码
@layer 低等级,中等级,高等级;

优先级:!import > 内联样式 > 高等级 > 中等级 > 低层级

简单使用

受等级影响

第1步:定义层的顺序。

第2步:定义层里的样式。

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>块元素居中一</title>
    <style>
        @layer A, B;

        div {
            width: 100px;
            height: 100px;
        }

        @layer A {
            .box {
                background-color: blue;
            }
        }

        @layer B {
            .box {
                background-color: red;
            }
        }
    </style>
</head>
<body>
<div class="box"></div>
</body>
</html>

说明:B 层级比较高,因此使用 B 的样式。

不受权重影响

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>块元素居中一</title>
    <style>
        @layer A, B;

        div {
            width: 100px;
            height: 100px;
        }

        @layer A {
            #box {
                background-color: blue;
            }
        }

        @layer B {
            .box {
                background-color: red;
            }
        }
    </style>
</head>
<body>
<div class="box" id="box"></div>
</body>
</html>

项目中使用

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>块元素居中一</title>
    <style>
        /* =====================================
   第一步:定义全局层顺序(整个项目只写一次)
   顺序:越靠后,优先级越高
===================================== */
        @layer reset, base, theme, components, utilities;

        /* =====================================
           第二层:样式重置(最低优先级)
        ===================================== */
        @layer reset {
            * {
                margin: 0;
                padding: 0;
                box-sizing: border-box;
            }

            ul, ol {
                list-style: none;
            }

            a {
                text-decoration: none;
            }
        }

        /* =====================================
           第三层:基础标签样式
        ===================================== */
        @layer base {
            body {
                font-family: system-ui, sans-serif;
                line-height: 1.6;
                color: #333;
                background: #fff;
            }

            img {
                max-width: 100%;
                display: block;
            }
        }

        /* =====================================
           第四层:主题变量
        ===================================== */
        @layer theme {
            :root {
                --primary: #42b983;
                --danger: #f56565;
                --radius: 6px;
            }
        }

        /* =====================================
           第五层:业务组件(按钮、卡片、导航)
        ===================================== */
        @layer components {
            .btn {
                padding: 8px 16px;
                border-radius: var(--radius);
                background: var(--primary);
                color: white;
                border: none;
                cursor: pointer;
            }

            .card {
                padding: 20px;
                border-radius: var(--radius);
                box-shadow: 0 2px 10px #0001;
            }
        }

        /* =====================================
           第六层:工具类(最高优先级)
        ===================================== */
        @layer utilities {
            .text-center {
                text-align: center;
            }

            .mt-10 {
                margin-top: 10px;
            }

            .mt-20 {
                margin-top: 20px;
            }

            .bg-danger {
                background: var(--danger) !important;
            }
        }
    </style>
</head>
<body>
<div class="card text-center mt-20">
    <h2>原生 CSS + @layer</h2>
    <button class="btn mt-10">普通按钮</button>
    <button class="btn bg-danger mt-10">红色按钮</button>
</div>
</body>
</html>
相关推荐
大家的林语冰2 小时前
CSS 新函数上市,一行代码让文本自动变色,无需 JS 也能符合 W3C 无障碍对比度标准
前端·javascript·css
DFT计算杂谈2 小时前
VASP 磁性结构可视化:一键生成完美 VESTA / MCIF
java·前端·css·html·css3
ZC跨境爬虫3 小时前
跟着 MDN 学CSS day_49:定位实例练习从入门到精通
前端·css·学习
用户059540174464 小时前
GitHub Actions 自动化测试流水线踩坑实录:一个 `&&` 符号,折腾了 4 小时,但前端事故率降为 0
前端·css
用户059540174464 小时前
大模型多轮对话“失忆”踩坑实录:一次线上事故让我排查了48小时,最终靠 Playwright + Pytest 把记忆锁死
前端·css
ZC跨境爬虫5 小时前
跟着 MDN 学CSS day_50:(传统布局方法与网格系统)
前端·css·ui·tensorflow·媒体
Js_x5 小时前
HTML实现类星露谷小游戏
css·html·css3
biubiubiu_LYQ1 天前
入门开发者基础篇之CSS浮动布局:一文吃透浮动底层逻辑
前端·css
ZC跨境爬虫1 天前
跟着 MDN 学CSS day_45:媒体查询入门指南——从语法到移动优先实践
前端·css·ui·html·tensorflow·媒体