CSS at-rules are fundamental building blocks that instruct the CSS engine how to behave. They begin with an at-sign (@) followed by an identifier, and they control everything from how stylesheets import external resources to how animations are defined. Understanding at-rules is essential for writing modern, performant CSS that takes advantage of the latest browser capabilities.
This guide covers the most practical at-rules you'll use in everyday web development, with code examples and best practices for each. Whether you're building responsive layouts with container queries or creating smooth animations with keyframes, mastering these at-rules will elevate your CSS architecture. For teams looking to implement advanced CSS patterns across their applications, our web development services provide expert guidance on modern styling techniques.
Statement At-Rules
Statement at-rules are the simpler form of at-rules, ending with a semicolon rather than a block of nested rules. These at-rules typically provide meta-information to the browser or control how stylesheets import and organize their content.
@charset
The @charset at-rule specifies the character encoding used in the stylesheet. While typically unnecessary when using UTF-8 encoding, it can be useful in certain deployment scenarios.
@charset "utf-8";
@import
The @import at-rule allows you to include styles from other stylesheets, enabling modular CSS architecture.
@import url('components/buttons.css');
@import 'modules/forms.css' screen and (min-width: 768px);
However, @import statements have performance implications because they create additional HTTP requests. For optimizing stylesheet loading and performance, consider implementing a comprehensive web development strategy that includes build-time bundling.
@layer
The @layer at-rule enables cascade layers that provide explicit control over style precedence. This is one of the most significant additions to CSS in recent years.
@layer base {
h1 { font-size: 2rem; }
p { line-height: 1.6; }
}
@layer components {
.card {
border: 1px solid #e0e0e0;
border-radius: 8px;
}
}
@layer utilities {
.text-center { text-align: center; }
}
@namespace
The @namespace at-rule defines a default namespace or namespace prefix for XML-based selectors in stylesheets.
@namespace url("http://www.w3.org/1999/xhtml");
@namespace svg url("http://www.w3.org/2000/svg");
Understanding these foundational at-rules sets the stage for more advanced techniques like container queries and CSS custom properties.
Block At-Rules
Block at-rules contain nested rules, other at-rules, or declarations within curly braces, enabling complex conditional styling and rule grouping. These at-rules are the workhorses of modern CSS.
@media
The @media at-rule is fundamental to responsive web design, allowing you to conditionally apply styles based on device characteristics.
@media screen and (min-width: 768px) {
.container {
max-width: 720px;
padding: 2rem;
}
}
@media (prefers-color-scheme: dark) {
body {
background-color: #1a1a1a;
color: #e0e0e0;
}
}
@media (prefers-reduced-motion: reduce) {
* {
animation-duration: 0.01ms !important;
transition-duration: 0.01ms !important;
}
}
@keyframes
The @keyframes at-rule defines animation sequences by specifying styles at various points in the animation timeline.
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes slideUp {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.animated-element {
animation: slideUp 0.6s ease-out forwards;
}
@font-face
The @font-face at-rule enables custom fonts to be loaded and used in your stylesheet.
@font-face {
font-family: 'Inter';
src: url('/fonts/inter-var.woff2') format('woff2'),
url('/fonts/inter-var.woff') format('woff');
font-weight: 100 900;
font-style: normal;
font-display: swap;
}
@supports
The @supports at-rule allows you to test whether a browser supports a particular CSS property or value combination.
@supports (display: grid) {
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 1.5rem;
}
}
@supports (backdrop-filter: blur(10px)) {
.glass-effect {
background: rgba(255, 255, 255, 0.2);
backdrop-filter: blur(10px);
}
}
These block at-rules form the foundation of modern CSS development. When implementing complex layouts with animations and responsive behavior, our AI-powered development workflows can help automate testing across different browser capabilities.
Modern CSS At-Rules
Modern CSS has introduced several powerful at-rules that enable new patterns in responsive design, theming, and component architecture.
@container
Container queries represent a paradigm shift in responsive design, allowing styles to respond to the size of a parent container rather than the viewport. This approach is essential for building adaptive component libraries that work in any layout context.
.card-container {
container-type: inline-size;
container-name: card;
}
@container card (min-width: 400px) {
.card {
display: flex;
flex-direction: row;
}
}
@container card (max-width: 399px) {
.card-image {
aspect-ratio: 16/9;
}
}
@property
The @property at-rule registers custom properties with explicit type checking, default values, and inheritance behavior.
@property --primary-color {
syntax: '<color>';
inherits: false;
initial-value: #0066cc;
}
@property --spacing-unit {
syntax: '<length>';
inherits: true;
initial-value: 1rem;
}
@property --animation-duration {
syntax: '<time>';
inherits: false;
initial-value: 300ms;
}
@scope
The @scope at-rule defines explicit scoping boundaries for selectors, limiting where selectors apply within the document.
@scope (.article-component) {
:scope {
font-family: system-ui, sans-serif;
max-width: 65ch;
}
h1, h2, h3 {
line-height: 1.2;
margin-top: 1.5em;
}
}
@starting-style
The @starting-style at-rule defines the starting values for transitions when an element receives its first style update.
.modal {
opacity: 0;
transform: scale(0.95);
transition: opacity 0.2s ease, transform 0.2s ease;
@starting-style {
opacity: 0;
transform: scale(0.95);
}
}
.modal[open] {
opacity: 1;
transform: scale(1);
}
Modern at-rules like @container and @property are transforming how we approach web development projects. Combined with AI automation services, development teams can build more maintainable and performant stylesheets.
Additional At-Rules
@view-transition
The @view-transition at-rule opts the current document into view transitions, enabling smooth animated transitions between page states.
@view-transition {
navigation: auto;
}
::view-transition-old(root) {
animation: fade-out 0.3s ease-in-out;
}
::view-transition-new(root) {
animation: fade-in 0.3s ease-in-out;
}
@position-try
The @position-try at-rule defines custom position options for anchor-positioned elements, providing fallback positioning strategies.
.anchor-button {
anchor-name: --trigger;
}
.tooltip {
position-anchor: --trigger;
position-area: block-end center;
@position-try (--flip-block) {
position-area: block-start center;
}
}
@counter-style
The @counter-style at-rule defines custom counter styles for use with ordered lists and generated content.
@counter-style circled {
system: cyclic;
symbols: "①" "②" "③" "④" "⑤" "⑥" "⑦" "⑧" "⑨" "⑩";
suffix: " ";
}
@counter-style roman-upper {
system: upper-roman;
range: 1 5000;
}
These advanced at-rules enable sophisticated UI patterns that were previously impossible without JavaScript. For teams implementing modern CSS features, our web development expertise ensures optimal implementation across all browser targets.
Performance Considerations
Understanding the performance implications of at-rules helps you write efficient CSS that loads quickly and renders smoothly.
Network Impact
The @import at-rule creates synchronous network requests that block parallel downloads, making it one of the least performant ways to include styles. Modern build tools and bundlers consolidate stylesheets at build time, eliminating the need for runtime @import statements.
Animation Performance
Animations defined with @keyframes are most performant when they animate only transform and opacity properties, which browsers can accelerate on the GPU. Avoid animating properties like width, height, margin, or padding, which trigger layout recalculations.
Query Efficiency
For @media queries, placing mobile-first styles at the base level with progressive enhancement for larger screens typically produces the most efficient CSS. Container queries (@container) and cascade layers (@layer) have minimal runtime performance impact since they're resolved during style computation.
Best Practices Summary
- Use
@importsparingly; prefer build-time bundling - Animate only
transformandopacityfor GPU acceleration - Use mobile-first approach with
@mediaqueries - Avoid excessive nesting of queries and layers
- Register custom properties with
@propertyfor better performance
For optimal performance in your web projects, our development team follows these best practices to ensure fast loading times and smooth animations. Implementing these patterns as part of a comprehensive SEO strategy can significantly improve Core Web Vitals and search rankings.
Best Practices
Mobile-First Responsive Design
Start with mobile-first responsive design using @media queries, defining base styles for small screens and adding progressively enhanced styles for larger viewports. This approach reduces the CSS payload for mobile users.
Use Cascade Layers for Organization
Use cascade layers (@layer) to explicitly define style precedence, especially when incorporating third-party styles. Layers make override intentions clear and prevent specificity wars.
Progressive Enhancement with @supports
Leverage @supports feature queries for progressive enhancement of cutting-edge CSS features. Test for the specific property or value you want to use rather than relying on browser detection.
Register Custom Properties
Register custom properties with @property when you need type checking, inheritance control, or animation support. This provides better developer experience and unlocks animations on CSS variables.
Component-Based Responsive Design
Use @container for component-based responsive design where elements adapt to their container's size rather than the viewport. This pattern enables truly reusable components.
Font Loading Optimization
For custom fonts loaded with @font-face, always include the font-display descriptor to control loading behavior. The swap value provides the best balance of performance and user experience.
By following these best practices and leveraging modern CSS capabilities, you can build maintainable, performant stylesheets. Our web development team specializes in implementing these patterns for clients seeking professional front-end development support.
Frequently Asked Questions
What is the difference between statement and block at-rules?
Statement at-rules end with a semicolon and are used for meta-information like @charset and @import. Block at-rules use curly braces and contain nested rules, other at-rules, or declarations, such as @media and @keyframes.
When should I use @layer instead of specific selectors?
Use @layer when you need explicit control over style precedence, especially when combining styles from multiple sources like third-party libraries. Layers make overrides predictable without relying on specificity or order.
How do container queries differ from media queries?
Media queries respond to the viewport size, while container queries respond to the size of a parent container. Container queries enable truly component-based responsive design where widgets adapt to their available space.
Why should I use @property instead of regular CSS variables?
@property provides type checking, default values, and inheritance control for custom properties. It also enables animations on CSS variables and provides better developer experience with IDE support.
What is the most performant way to animate elements?
Animate only transform and opacity properties, which browsers can accelerate on the GPU. Use @keyframes with these properties for smooth, performant animations that don't trigger layout recalculations.