sass-site/source/documentation/at-rules/css.liquid

226 lines
5.5 KiB
Plaintext
Raw Normal View History

---
title: CSS At-Rules
table_of_contents: true
---
2023-05-30 22:27:01 +02:00
{% compatibility 'dart: 1.15.0', 'libsass: false', 'ruby: false', 'feature: Name Interpolation' %}
2023-05-30 23:23:34 +02:00
LibSass, Ruby Sass, and older versions of Dart Sass don't support
[interpolation][] in at-rule names. They do support interpolation in values.
2023-05-30 23:23:34 +02:00
[interpolation]: /documentation/interpolation
{% endcompatibility %}
{% markdown %}
2023-05-30 23:23:34 +02:00
Sass supports all the at-rules that are part of CSS proper. To stay flexible and
forwards-compatible with future versions of CSS, Sass has general support that
covers almost all at-rules by default. A CSS at-rule is written
`@<name> <value>`, `@<name> { ... }`, or `@<name> <value> { ... }`. The name
must be an identifier, and the value (if one exists) can be pretty much
anything. Both the name and the value can contain [interpolation][].
[interpolation]: /documentation/interpolation
{% endmarkdown %}
{% codeExample 'css' %}
2023-05-30 23:23:34 +02:00
@namespace svg url(http://www.w3.org/2000/svg);
@font-face {
font-family: "Open Sans";
src: url("/fonts/OpenSans-Regular-webfont.woff2") format("woff2");
}
@counter-style thumbs {
system: cyclic;
symbols: "\1F44D";
}
===
@namespace svg url(http://www.w3.org/2000/svg)
@font-face
font-family: "Open Sans"
src: url("/fonts/OpenSans-Regular-webfont.woff2") format("woff2")
@counter-style thumbs
system: cyclic
symbols: "\1F44D"
{% endcodeExample %}
{% markdown %}
2023-05-30 23:23:34 +02:00
If a CSS at-rule is nested within a style rule, the two automatically swap
positions so that the at-rule is at the top level of the CSS output and the
style rule is within it. This makes it easy to add conditional styling without
having to rewrite the style rule's selector.
{% endmarkdown %}
{% codeExample 'nested-css-at-rule' %}
2023-05-30 23:23:34 +02:00
.print-only {
display: none;
2023-05-30 23:23:34 +02:00
@media print { display: block; }
}
===
.print-only
display: none
2023-05-30 23:23:34 +02:00
@media print
display: block
{% endcodeExample %}
2023-05-30 22:27:01 +02:00
{{ '## `@media`' | markdown }}
{% compatibility 'dart: 1.11.0', 'libsass: false', 'ruby: 3.7.0', 'feature: Range Syntax', 'useMarkdown: false' %}
2023-05-30 23:23:34 +02:00
{% markdown %}
LibSass and older versions of Dart Sass and Ruby Sass don't support media
queries with features written in a [range context][]. They do support other
standard media queries.
[range context]: https://www.w3.org/TR/mediaqueries-4/#mq-range-context
{% endmarkdown %}
2023-06-01 19:14:36 +02:00
{% codeExample 'range-syntax', false %}
2023-05-30 23:23:34 +02:00
@media (width <= 700px) {
body {
background: green;
}
}
===
@media (width <= 700px)
body
background: green
===
@media (width <= 700px) {
body {
background: green;
}
}
{% endcodeExample %}
2023-05-30 22:27:01 +02:00
{% endcompatibility %}
{% markdown %}
2023-05-30 23:23:34 +02:00
The [`@media` rule][] does all of the above and more. In addition to allowing
interpolation, it allows [SassScript expressions][] to be used directly in the
[feature queries][].
2023-05-30 23:23:34 +02:00
[`@media` rule]: https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries
[SassScript expressions]: /documentation/syntax/structure#expressions
[feature queries]: https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries#Targeting_media_features
{% endmarkdown %}
{% codeExample 'media-rule' %}
2023-05-30 23:23:34 +02:00
$layout-breakpoint-small: 960px;
2023-05-30 23:23:34 +02:00
@media (min-width: $layout-breakpoint-small) {
.hide-extra-small {
display: none;
}
}
2023-05-30 23:23:34 +02:00
===
$layout-breakpoint-small: 960px
2023-05-30 23:23:34 +02:00
@media (min-width: $layout-breakpoint-small)
.hide-extra-small
display: none
{% endcodeExample %}
{% markdown %}
2023-05-30 23:23:34 +02:00
When possible, Sass will also merge media queries that are nested within one
another to make it easier to support browsers that don't yet natively support
nested `@media` rules.
{% endmarkdown %}
{% codeExample 'merge-media-queries' %}
2023-05-30 23:23:34 +02:00
@media (hover: hover) {
.button:hover {
border: 2px solid black;
2023-05-30 23:23:34 +02:00
@media (color) {
border-color: #036;
}
}
}
2023-05-30 23:23:34 +02:00
===
@media (hover: hover)
.button:hover
border: 2px solid black
@media (color)
border-color: #036
{% endcodeExample %}
{% markdown %}
2023-05-30 23:23:34 +02:00
## `@supports`
2023-05-30 23:23:34 +02:00
The [`@supports` rule][] also allows [SassScript expressions][] to be used in
the declaration queries.
2023-05-30 23:23:34 +02:00
[SassScript expressions]: /documentation/syntax/structure#expressions
[`@supports` rule]: https://developer.mozilla.org/en-US/docs/Web/CSS/@supports
{% endmarkdown %}
{% codeExample 'support-at-rule' %}
2023-05-30 23:23:34 +02:00
@mixin sticky-position {
position: fixed;
@supports (position: sticky) {
position: sticky;
}
}
2023-05-30 23:23:34 +02:00
.banner {
@include sticky-position;
}
===
@mixin sticky-position
position: fixed
@supports (position: sticky)
position: sticky
2023-05-30 22:27:01 +02:00
2023-05-30 23:23:34 +02:00
.banner
@include sticky-position
{% endcodeExample %}
{% markdown %}
2023-05-30 23:23:34 +02:00
## `@keyframes`
2023-05-30 23:23:34 +02:00
The [`@keyframes` rule][] works just like a general at-rule, except that its
child rules must be valid keyframe rules (`<number>%`, `from`, or `to`) rather
than normal selectors.
2023-05-30 23:23:34 +02:00
[`@keyframes` rule]: https://developer.mozilla.org/en-US/docs/Web/CSS/@keyframes
{% endmarkdown %}
{% codeExample 'keyframes' %}
2023-05-30 23:23:34 +02:00
@keyframes slide-in {
from {
margin-left: 100%;
width: 300%;
}
2023-05-30 23:23:34 +02:00
70% {
margin-left: 90%;
width: 150%;
}
2023-05-30 23:23:34 +02:00
to {
margin-left: 0%;
width: 100%;
}
}
2023-05-30 23:23:34 +02:00
===
@keyframes slide-in
from
margin-left: 100%
width: 300%
2023-05-30 23:23:34 +02:00
70%
margin-left: 90%
width: 150%
2023-05-30 23:23:34 +02:00
to
margin-left: 0%
width: 100%
{% endcodeExample %}