CSS `@` 规则

CSS `@` 规则

本文解释了CSS中的@规则。

您可以学习如何使用并编写类似@import@media@规则。

YouTube Video

预览的HTML

css-at-rule.html
  1<!DOCTYPE html>
  2<html lang="en">
  3<head>
  4    <meta charset="UTF-8">
  5    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6    <title>CSS At Rules</title>
  7    <link rel="stylesheet" href="css-base.css">
  8    <link rel="stylesheet" href="css-at-rule.css">
  9</head>
 10<body>
 11    <!-- Header -->
 12    <header>
 13        <h1>CSS At(@) Rules</h1>
 14    </header>
 15
 16    <!-- Main content -->
 17    <main>
 18        <header><h2>CSS At(@) Rules</h2></header>
 19        <article>
 20            <h3>@import</h3>
 21            <section>
 22                <header><h4>CSS</h4></header>
 23<pre class="sample">
 24@import url('styles.css');
 25</pre>
 26            </section>
 27        </article>
 28        <article>
 29            <h3>@media</h3>
 30            <section>
 31                <header><h4>CSS</h4></header>
 32<pre class="sample">
 33@media screen and (max-width: 600px) {
 34    body {
 35        background-color: lightblue;
 36    }
 37}
 38</pre>
 39            </section>
 40        </article>
 41        <article>
 42            <h3>@media Example(Screen Width)</h3>
 43            <section>
 44                <header><h4>CSS</h4></header>
 45<pre class="sample">
 46@media (min-width: 600px) {
 47    body {
 48        background-color: lightblue;
 49    }
 50}
 51@media (max-width: 599px) {
 52    body {
 53        background-color: lightpink;
 54    }
 55}
 56</pre>
 57            </section>
 58        </article>
 59        <article>
 60            <h3>@media Example(Orientation)</h3>
 61            <section>
 62                <header><h4>CSS</h4></header>
 63<pre class="sample">
 64@media (orientation: portrait) {
 65    body {
 66        font-size: 18px;
 67    }
 68}
 69@media (orientation: landscape) {
 70    body {
 71        font-size: 16px;
 72    }
 73}
 74</pre>
 75            </section>
 76        </article>
 77        <article>
 78            <h3>@media Example(Resolution)</h3>
 79            <section>
 80                <header><h4>CSS</h4></header>
 81<pre class="sample">
 82@media (min-resolution: 2dppx) {
 83    body {
 84        background-image: url('high-res-image.png');
 85    }
 86}
 87</pre>
 88            </section>
 89        </article>
 90        <article>
 91            <h3>@media Example(AND Condition)</h3>
 92            <section>
 93                <header><h4>CSS</h4></header>
 94<pre class="sample">
 95@media (min-width: 600px) and (orientation: portrait) {
 96    body {
 97        background-color: lightgreen;
 98    }
 99}
100</pre>
101            </section>
102        </article>
103        <article>
104            <h3>@media Example(OR Condition)</h3>
105            <section>
106                <header><h4>CSS</h4></header>
107<pre class="sample">
108@media (max-width: 400px), (orientation: landscape) {
109    body {
110        color: gray;
111    }
112}
113</pre>
114            </section>
115        </article>
116        <article>
117            <h3>@media Example(NOT Condition)</h3>
118            <section>
119                <header><h4>CSS</h4></header>
120<pre class="sample">
121@media not all and (min-width: 600px) {
122    body {
123        font-size: 14px;
124    }
125}
126</pre>
127            </section>
128        </article>
129        <article>
130            <h3>@font-face</h3>
131            <section>
132                <header><h4>CSS</h4></header>
133<pre class="sample">
134@font-face {
135  font-family: 'CustomFont';
136  src: url('customfont.woff2') format('woff2'),
137       url('customfont.woff') format('woff');
138}
139</pre>
140            </section>
141        </article>
142        <article>
143            <h3>@keyframes</h3>
144            <section>
145                <header><h4>CSS</h4></header>
146<pre class="sample">
147@keyframes slidein {
148    from {
149        transform: translateX(100%);
150    }
151    to {
152        transform: translateX(0%);
153    }
154}
155
156div {
157    animation: slidein 2s ease-in-out;
158}
159</pre>
160            </section>
161        </article>
162        <article>
163            <h3>@supports</h3>
164            <section>
165                <header><h4>CSS</h4></header>
166<pre class="sample">
167@supports (display: grid) {
168    .container {
169        display: grid;
170    }
171}
172</pre>
173            </section>
174        </article>
175        <article>
176            <h3>@page</h3>
177            <section>
178                <header><h4>CSS</h4></header>
179<pre class="sample">
180@page {
181    size: A4;
182    margin: 2cm;
183}
184</pre>
185            </section>
186        </article>
187        <article>
188            <h3>@charset</h3>
189            <section>
190                <header><h4>CSS</h4></header>
191<pre class="sample">
192@charset "UTF-8";
193</pre>
194            </section>
195        </article>
196        <article>
197            <h3>@layer</h3>
198            <section>
199                <header><h4>CSS</h4></header>
200<pre class="sample">
201@layer reset {
202    h1 {
203        margin: 0;
204    }
205}
206@layer base {
207    h1 {
208        font-size: 24px;
209        color: blue;
210    }
211}
212@layer theme {
213    h1 {
214        color: red;
215    }
216}
217</pre>
218                <header><h4>HTML</h4></header>
219<pre>
220&lt;h1&gt;CSS Layer Test&lt;/h1&gt;
221&lt;p&gt;
222    This page demonstrates how to apply styles to
223    the &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; element using CSS layers.
224&lt;/p&gt;
225</pre>
226                <header><h4>HTML+CSS</h4></header>
227                <section class="sample-view">
228                    <h1>CSS Layer Test</h1>
229                    <p>
230                        This page demonstrates how to apply styles to
231                        the <code>&lt;h1&gt;</code> element using CSS layers.
232                    </p>
233                </section>
234            </section>
235        </article>
236        <article>
237            <h3>@layer with order</h3>
238            <section>
239                <header><h4>CSS</h4></header>
240<pre class="sample">
241@layer theme, base, reset;
242
243@layer reset {
244    h1 {
245        margin: 0;
246    }
247}
248@layer base {
249    h1 {
250        font-size: 24px;
251        color: blue;
252    }
253}
254@layer theme {
255    h1 {
256        color: red;
257    }
258}
259</pre>
260                <header><h4>HTML</h4></header>
261<pre>
262&lt;h1&gt;CSS Layer Test&lt;/h1&gt;
263&lt;p&gt;
264    This page demonstrates how to apply styles to
265    the &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; element using CSS layers.
266&lt;/p&gt;
267</pre>
268                <header><h4>HTML+CSS</h4></header>
269                <section class="sample-view">
270                    <h1>CSS Layer Test</h1>
271                    <p>
272                        This page demonstrates how to apply styles to
273                        the <code>&lt;h1&gt;</code> element using CSS layers.
274                    </p>
275                </section>
276            </section>
277        </article>
278        <article>
279            <h3>@import &amp; @layer</h3>
280            <section>
281                <header><h4>CSS</h4></header>
282<pre class="sample">
283@import url('css/reset.css') layer(reset);
284@import url('css/base.css') layer(base);
285@import url('css/theme.css') layer(theme);
286</pre>
287                <header><h4>HTML</h4></header>
288<pre>
289&lt;h1&gt;CSS Layer Test&lt;/h1&gt;
290&lt;p&gt;
291    This page demonstrates how to apply styles to
292    the &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; element using CSS layers.
293&lt;/p&gt;
294</pre>
295                <header><h4>HTML+CSS</h4></header>
296                <section class="sample-view">
297                    <h1>CSS Layer Test</h1>
298                    <p>
299                        This page demonstrates how to apply styles to
300                        the <code>&lt;h1&gt;</code> element using CSS layers.
301                    </p>
302                </section>
303            </section>
304        </article>
305    </main>
306</body>
307</html>

CSS @ 规则

CSS @规则(at-rules)是用于控制样式表行为和应用条件的特殊指令。与普通CSS属性不同,@规则会基于条件应用样式或导入外部资源。常见的@规则包括@media@import@keyframes等。

示例@ 规则的类型

@import

1@import url('styles.css');

@import用于将其他CSS文件导入当前样式表。这使得外部CSS文件的重复利用变得简单。

要点:

  • @import必须写在样式表的开头。
  • 由于性能问题,@import应在大型项目中避免使用。

@media

1@media screen and (max-width: 600px) {
2  body {
3    background-color: lightblue;
4  }
5}
  • @media使用媒体查询根据特定条件(如屏幕宽度、分辨率等)应用样式。它经常用于实现响应式设计。

常用条件

以下是媒体查询中常用的条件。

基于宽度的条件
 1@media (min-width: 600px) {
 2    body {
 3        background-color: lightblue;
 4    }
 5}
 6@media (max-width: 599px) {
 7    body {
 8        background-color: lightpink;
 9    }
10}
  • 最小宽度 (min-width) 在宽度等于或大于指定值时应用样式。
  • 最大宽度 (max-width) 在宽度等于或小于指定值时应用样式。
设备方向 (orientation)
 1@media (orientation: portrait) {
 2    body {
 3        font-size: 18px;
 4    }
 5}
 6@media (orientation: landscape) {
 7    body {
 8        font-size: 16px;
 9    }
10}
  • 设备方向 (orientation) 可以设置为 纵向 (portrait)横向 (landscape)
分辨率 (resolution)
1@media (min-resolution: 2dppx) {
2    body {
3        background-image: url('high-res-image.png');
4    }
5}
  • 可以根据 分辨率 (resolution) 应用样式。
  • 在此示例中,为高分辨率显示器指定了高分辨率背景图像。

组合条件

媒体查询还可以组合多个条件。

与条件 (and)
1@media (min-width: 600px) and (orientation: portrait) {
2    body {
3        background-color: lightgreen;
4    }
5}
  • 当所有条件都满足时, 条件会应用样式。
  • 在此示例中,当屏幕宽度为 600px 或更大且为纵向时,body 的背景颜色设为浅绿色。
或条件 (,)
1@media (max-width: 400px), (orientation: landscape) {
2    body {
3        color: gray;
4    }
5}
  • 当任一条件满足时, 条件会应用样式。
  • 在此示例中,当屏幕宽度为 400px 或更小,或为横向时,body 的文本颜色设为灰色。
非条件 (not)
1@media not all and (min-width: 600px) {
2    body {
3        font-size: 14px;
4    }
5}
  • 当条件不满足时, 条件会应用样式。
  • 在此示例中,对于所有设备,当屏幕宽度不是 600px 或更大时,body 的字体大小设为 14px

@font-face

1@font-face {
2  font-family: 'CustomFont';
3  src: url('customfont.woff2') format('woff2'),
4       url('customfont.woff') format('woff');
5}

@font-face用于定义网页字体。通过该规则,外部字体可以下载并应用到网页中。

  • 可以使用自定义字体,而不是依赖标准字体。
  • 需要注意字体文件的大小。

@keyframes

 1@keyframes slidein {
 2  from {
 3    transform: translateX(100%);
 4  }
 5  to {
 6    transform: translateX(0%);
 7  }
 8}
 9
10div {
11  animation: slidein 2s ease-in-out;
12}

@keyframes用于创建CSS动画。动画的每一步都可以指定样式。

  • 这使得动画控制更加精细。
  • 它与animation属性一起使用。

@supports

1@supports (display: grid) {
2  .container {
3    display: grid;
4  }
5}

@supports用于检查浏览器是否支持某些CSS特性,并根据结果应用样式。

  • 它有助于在旧浏览器和新浏览器之间保持兼容性。
  • 可以通过特性检测应用回退样式。

@page

1@page {
2  size: A4;
3  margin: 2cm;
4}

@page用于控制打印时的样式。这对于设置页面大小和边距非常有用。

要点:

  • 用于为打印媒体设置样式。
  • 您还可以自定义特定页面元素的样式,例如页眉和页脚。

@charset

1@charset "UTF-8";

@charset 指定 CSS 文件的字符编码。主要指定为 UTF-8。

  • 必须在样式表的开头指定。
  • 尤其适用于多语言网站。

@layer

@layer 是一个新的 @ 规则,用于组织 CSS 样式表的优先级,允许通过层将多个 CSS 规则分组和管理。此规则在大型样式表或涉及多个外部样式表的项目中非常有用,可以更方便地避免 CSS 冲突。

在 CSS 样式表中,优先级通常由“层叠”决定,但通过使用 @layer,您可以更灵活和明确地控制样式的优先级。

基本语法

1@layer <layer-name> {
2  /* Styles within this layer */
3}
  • 使用 @layer 定义样式时,其语法如下。
 1@layer reset {
 2    h1 {
 3        margin: 0;
 4    }
 5}
 6@layer base {
 7    h1 {
 8        font-size: 24px;
 9        color: blue;
10    }
11}
12@layer theme {
13    h1 {
14        color: red;
15    }
16}
  • 例如,您可以这样在不同的层中定义样式:
  • 在这种情况下,分别定义了 reset 层、base 层和 theme 层,最终的 h1 样式由 theme 层内容决定,结果应用了 color: red;

多层的优先级

@layer 的强大功能在于它允许管理层之间样式的优先级。在上述示例中,由于 theme 层最后定义,它覆盖了其他层中定义的样式。

此外,@layer 可用于管理外部样式表或从其他库加载的 CSS 文件中的优先级层叠。

全局优先级
 1@layer theme, base, reset;
 2
 3@layer reset {
 4    h1 {
 5        margin: 0;
 6    }
 7}
 8@layer base {
 9    h1 {
10        font-size: 24px;
11        color: blue;
12    }
13}
14@layer theme {
15    h1 {
16        color: red;
17    }
18}
  • 在 CSS 层叠中,层的顺序按优先级排列,后定义的样式优先。例如,您可以通过如下书写明确指定层的顺序。
  • 这确保了即使 theme 层是首先定义的,样式也会按照 themebasereset 的顺序应用。

@import@layer 一起使用

1@import url('css/reset.css') layer(reset);
2@import url('css/base.css') layer(base);
3@import url('css/theme.css') layer(theme);
  • 通过结合使用 @import@layer,还可以管理外部样式表中的层。
  • 这允许将外部样式表放置在不同的层中,从而管理它们的优先级。

@layer 的关键点

@layer 是 CSS 样式管理中的一项强大工具,为避免大型项目中的样式冲突提供了一种便捷方法。在处理多种样式来源时,它在简洁管理样式优先级方面尤为突出。请记住以下几点。

  • 通过按层分离样式,您可以控制层叠优先级。
  • 通过将其与 @import 一起使用,您可以将外部 CSS 文件纳入层管理。
  • 定义多个层可以防止样式冲突。

这可以最大限度地减少样式冲突,并且即使在需要复杂样式管理的项目中也能实现高效设计。

使用 @layer 的示例

使用 @layer 时,您可以考虑层命名约定、导入顺序以及与项目规模相适应的层数量等方面。

层命名

层的名称是基于项目的结构和目的来决定的。以下是一些常见的层命名示例:。

1@layer base, layout, components, utilities;
  • base:定义基础样式的层,例如重置样式和排版。
  • layout:定义页面布局和网格系统样式的层。
  • components:定义可重用UI组件(如按钮和卡片)样式的层。
  • utilities:定义工具类样式的层(例如:marginpaddingtext-center)。
清晰的导入顺序
1@import url('base.css') layer(base);
2@import url('layout.css') layer(layout);
3@import url('components.css') layer(components);
4@import url('utilities.css') layer(utilities);
  • 处理多个CSS文件时,使用 @import 设置清晰的顺序非常重要。这种顺序可以确保预期的优先级得以维持。
层的整合与简化
 1@layer common {
 2  body {
 3    margin: 0;
 4    padding: 0;
 5    box-sizing: border-box;
 6  }
 7
 8  .container {
 9    max-width: 1200px;
10    margin: 0 auto;
11  }
12}
  • 在小型项目中,限制层的数量可以降低复杂性。

总结

CSS中的@规则是支持灵活样式应用并增强网页设计性能的强大工具。@media@keyframes@supports使得在考虑设备差异和浏览器兼容性时更容易设置样式。此外,@import@font-face可以有效整合外部资源以实现更丰富的表达。

理解并正确使用这些@规则能够进行更高效的CSS代码编写。

您可以在我们的YouTube频道上使用Visual Studio Code跟随上述文章进行学习。 请也查看我们的YouTube频道。

YouTube Video