CSS (Cascading Style Sheets) is a powerful language used to style HTML documents. While most CSS consists of rulesets that define how HTML elements should look, there is a special kind of statement known as an at-rule. These statements begin with an @
symbol and give the browser special instructions or behavior that can’t be handled with regular CSS selectors and declarations. These are known as CSS At-Rules.
What Are CSS At-Rules?
CSS At-Rules are instructions for the CSS processor. They allow you to control how CSS behaves, apply styles under certain conditions, import stylesheets, define animations, specify character sets, and more. At-rules are not selectors or declarations themselves — they control how the CSS is interpreted.
Each at-rule starts with an @ symbol followed by a keyword, such as @media
, @import
, or @keyframes
.
For example:
@media screen and (max-width: 600px) {
body {
background-color: lightblue;
}
}
In this case, the background color will change only when the screen width is 600px or less.
Why Are At-Rules Important?
At-rules provide flexibility and control in your CSS code. Instead of writing multiple stylesheets for different devices, languages, or animations, you can use at-rules to organize and optimize your code.
Common Types of CSS At-Rules
Let’s explore some commonly used at-rules:
1. @import
The @import
rule is used to import another CSS file into the current stylesheet. It helps in separating styles into modular files for better organization.
@import url("theme.css");
Tip: It must appear at the top of the CSS file, before any other rules (except
@charset
).
2. @media
The @media
rule is used for responsive design. It applies styles only if a certain media condition is met (like screen size, resolution, orientation, etc.).
@media (max-width: 768px) {
.menu {
display: none;
}
}
This is the foundation of mobile-first or responsive web design.
3. @font-face
This rule allows custom fonts to be used on a website. You can specify a font source and then use it throughout your stylesheet.
@font-face {
font-family: 'OpenSans';
src: url('OpenSans.woff2') format('woff2');
}
You can now use 'OpenSans'
anywhere in your CSS.
4. @keyframes
This is used in CSS animations. It defines what the animation should do at various steps of the timeline.
@keyframes slidein {
from {
transform: translateX(-100%);
}
to {
transform: translateX(0%);
}
}
This animation can then be applied to an element using the animation
property.
5. @charset
Defines the character encoding for the CSS file (like UTF-8). This must be the very first line in the file.
@charset "UTF-8";
Using the right character set ensures symbols and special characters display correctly.
6. @supports
The @supports
rule checks if the browser supports a particular CSS property or feature.
@supports (display: grid) {
.container {
display: grid;
}
}
This helps with progressive enhancement — you can write modern styles that work in newer browsers without breaking older ones.
7. @page
Used in print stylesheets, the @page
rule lets you control how the page looks when printed.
@page {
margin: 1in;
}
This is useful for print formatting, such as setting page size or orientation.
8. @namespace
Used in XML-based styles, this rule declares a namespace for CSS to differentiate between similarly named elements in different vocabulary
@namespace url(http://www.w3.org/1999/xhtml);
This is rarely used in modern web design but is important in XML-heavy applications.
9. @layer
(New in CSS)
The @layer
rule is part of the CSS Cascade Layers specification. It helps you organize and prioritize your CSS by grouping rules into named layers.
@layer reset {
* {
margin: 0;
padding: 0;
}
}
It ensures better control over which styles override others.
Syntax of At-Rules
At-rules generally follow one of these formats:
- With a block (contains CSS declarations or nested rules):
@media (condition) { /* CSS rules here */ }
- Without a block (standalone directive):
@import url("style.css");
Best Practices for Using At-Rules
- Use
@import
sparingly. It adds extra HTTP requests and can slow down page load time. - Place
@charset
and@import
at the very top of your stylesheet. - Use
@media
to build responsive layouts instead of relying on JavaScript. - Use
@supports
to handle newer CSS features gracefully. - Use
@keyframes
along with animation properties to enhance user interface experiences.
Summary
At-Rule | Purpose |
---|---|
@import | Imports external CSS files |
@media | Applies CSS based on screen or device rules |
@font-face | Declares custom fonts |
@keyframes | Creates CSS animations |
@charset | Declares character encoding |
@supports | Checks for browser support |
@page | Controls print page styles |
@namespace | Used in XML styling |
@layer | Organizes styles using layers |
Conclusion
CSS At-Rules are a core part of writing advanced and professional CSS. They provide powerful control over your stylesheets and help create flexible, scalable, and maintainable styles. Whether you’re importing files, handling responsive designs, or crafting animations, at-rules are essential for modern web development.
By mastering at-rules, you not only make your code more organized but also ensure it behaves well across different devices, screen sizes, and browsers.