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>
相关推荐
ShineWinsu22 分钟前
对于CSS:选择器的解析
前端·css·面试·html·web·选择器·网页
keyipatience28 分钟前
CSS 学习(1):语法、引入方式、选择器、字体与背景
前端·css·html
程序员大阳1 小时前
副队长HTML+CSS博客网站实战 (5)-文章详情页 article.html开发
css·html·博客网站·详情页
keyipatience2 小时前
CSS学习(2)|元素显示模式、盒模型、Flex 布局
javascript·css·html·css3
软件技术NINI16 小时前
圣罗兰彩妆html+css 4页
前端·css·html
溪语流沙18 小时前
【Web全栈进阶】Composition API与组件通信:props深入、emit、插槽
前端·css·vue.js
传人once1 天前
企业官网静态页基础结构如何搭
javascript·css
damoluomu1 天前
静态站点生成器怎么选:Node.js 生态里常用的五个(番外一)
javascript·css·typescript·node.js·html
程序员大阳1 天前
副队长HTML+CSS博客网站实战 (1)–项目介绍与整体规划
css·html·实战·教程·博客网站·规划
程序员大阳1 天前
副队长HTML+CSS博客网站实战 (3)–首页主体:Grid 文章卡片 + 侧边栏布局
css·html·侧边栏·博客网站·首页·文章卡片