CSS At-rules
Special CSS rules that start with @ symbol
๐ What are CSS At-rules?
At-rules are CSS statements that begin with @ and provide instructions about how CSS should behave. They're like special commands for your stylesheet!
/* Example of @media at-rule */
@media (max-width: 768px) {
.mobile-text {
font-size: 14px;
}
}
Common At-rules
@media
Responsive design queries
@media (max-width: 600px) {
.text { font-size: 12px; }
}
@import
Import external stylesheets
@import url('fonts.css');
@keyframes
Define animations
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
@font-face
Define custom fonts
@font-face {
font-family: 'MyFont';
src: url('font.woff2');
}
๐น @media Queries (Responsive Design)
Use @media queries to adapt your website's layout and styles to different devices, screen sizes, and orientations. Target breakpoints based on content, not specific devices. Use min-width and max-width for screen size, orientation for portrait/landscape, and prefers-color-scheme for dark/light mode. Implement a mobile-first approach: style for small screens first, then enhance for larger ones. Media queries are essential for creating accessible, performant websites that work seamlessly across smartphones, tablets, and desktops.
/* Mobile First Approach */
.container {
padding: 10px;
font-size: 14px;
}
/* Tablet */
@media (min-width: 768px) {
.container {
padding: 20px;
font-size: 16px;
}
}
/* Desktop */
@media (min-width: 1024px) {
.container {
padding: 30px;
font-size: 18px;
max-width: 1200px;
margin: 0 auto;
}
}
/* Dark Mode */
@media (prefers-color-scheme: dark) {
.container {
background-color: #1f2937;
color: #f9fafb;
}
}
Output (resize window to see changes):
๐น @keyframes Animations
Create complex, multi-step animations with @keyframes for full control over intermediate states. Define a keyframe rule: @keyframes slide { from { transform: translateX(0); } to { transform: translateX(100px); } }. Apply it with animation: slide 2s ease infinite;. You can specify percentages (0%, 50%) for detailed sequences. Control playback with animation-direction, iteration-count, and fill-mode. Use for loading indicators, entrance animations, or continuous effects. Always respect prefers-reduced-motion.
/* Bounce Animation */
@keyframes bounce {
0%, 20%, 50%, 80%, 100% {
transform: translateY(0);
}
40% {
transform: translateY(-20px);
}
60% {
transform: translateY(-10px);
}
}
.bounce-element {
animation: bounce 2s infinite;
display: inline-block;
padding: 15px 30px;
background: #3b82f6;
color: white;
border-radius: 8px;
cursor: pointer;
}
/* Fade In Animation */
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.fade-in {
animation: fadeIn 1s ease-out;
}
Output:
๐น @supports (Feature Queries)
Feature queries check if a browser supports a specific CSS property or value, enabling conditional styling. The syntax @supports (property: value) { /* styles */ } applies enclosed styles only if the test passes. Use them to safely implement newer features like grid, aspect-ratio, or backdrop-filter without breaking layouts in older browsers. Combine with logical operators (and, or, not) for complex detection. This is a cornerstone of progressive enhancement and resilient CSS.
/* Check for Grid support */
@supports (display: grid) {
.grid-layout {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
}
/* Fallback for older browsers */
@supports not (display: grid) {
.grid-layout {
display: flex;
flex-wrap: wrap;
}
.grid-layout > * {
flex: 1 1 300px;
margin: 0.5rem;
}
}
/* Check for CSS custom properties */
@supports (--custom: property) {
.modern-component {
--primary: #3b82f6;
--secondary: #10b981;
background: var(--primary);
}
}
๐น @layer (Cascade Layers)
Cascade layers (@layer) allow you to define explicit precedence for your CSS rules, controlling the cascade order. Declare layers like @layer base, components, utilities;. Rules in later layers have priority over earlier ones, regardless of specificity. This prevents specificity wars and makes CSS easier to manage, especially in large projects or with third-party stylesheets. Layers help organize code into logical groups (reset, frameworks, themes, utilities) and provide a powerful tool for scalable, maintainable architecture.
/* Define layer order */
@layer reset, base, components, utilities;
/* Reset layer */
@layer reset {
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
}
/* Base styles */
@layer base {
body {
font-family: system-ui, sans-serif;
line-height: 1.6;
}
}
/* Component styles */
@layer components {
.button {
padding: 0.5rem 1rem;
border: none;
border-radius: 0.25rem;
background: #3b82f6;
color: white;
cursor: pointer;
}
}
/* Utility classes */
@layer utilities {
.text-center { text-align: center; }
.mt-4 { margin-top: 1rem; }
}