CSS At-Rules

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 22 Tutorials
@charset · @import · @font-face

What You’ll Learn

At-rules extend CSS beyond normal selectors. They declare resources, define animations, test support, organize cascade layers, and adapt components to containers. This hub links to each full tutorial and its try-it examples.

01

@charset

Encoding.

02

@import

Modular CSS.

03

@font-face

Web fonts.

04

@keyframes

Animation.

05

@supports

Feature queries.

06

@layer

Cascade order.

07

@container

Component queries.

08

22 guides

Full index.

Introduction

In CSS, at-rules are special statements that begin with @ and control how stylesheets behave. Unlike regular rules that target HTML elements with selectors, at-rules handle encoding, file imports, custom fonts, animations, and more.

What Are CSS At-Rules?

An at-rule provides meta-instructions to the browser about your stylesheet. Some end with a semicolon (@import, @charset); others use a block with curly braces (@font-face, @keyframes).

At-rules enable modular CSS, custom typography, and motion effects that would be impossible with selectors alone. Understanding placement rules — especially for @charset and @import — prevents subtle parsing bugs.

💡
Beginner Tip

Start with @font-face if you want custom fonts, or @keyframes if you want animations. Save @charset and @import for when you split CSS into multiple files.

At-Rules vs Regular Rules

  • Regular rule — p { color: blue; } — selector + declaration block.
  • At-rule — @import url("theme.css"); — starts with @, special syntax.
  • Related — start with @media, then browse media features under Media Queries.

📝 Syntax

Common at-rules side by side:

CSS
/* Must be first (if used) */
@charset "UTF-8";

/* Before other rules */
@import url("variables.css");

/* Custom font registration */
@font-face {
  font-family: MyFont;
  src: url("myfont.woff2") format("woff2");
}

/* Named animation steps */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

Placement rules

  • @charset — first in file; only one allowed
  • @import — after @charset, before all other rules
  • @font-face / @keyframes — anywhere, often grouped at top

Common at-rules on this site:

@charset @import @font-face @keyframes

⚡ Quick Reference

At-rulePurposeSyntax style
@charsetDeclare file encoding (UTF-8)Semicolon
@importInclude another stylesheetSemicolon
@font-faceRegister a custom fontBlock { }
@keyframesDefine animation stepsBlock { }
@mediaResponsive conditional stylesBlock { } (see Media Queries)

When to Use Each At-Rule

  • @charset — External CSS files with non-ASCII characters when encoding might be ambiguous.
  • @import — Splitting a project into theme, reset, and component files inside one entry stylesheet.
  • @font-face — Loading brand fonts not available on user systems.
  • @keyframes — Multi-step animations beyond simple transitions.
  • @media — Different styles for screen sizes, print, or reduced motion. See the @media tutorial and media features.

CSS At-Rule Tutorial Index

Search by at-rule name or browse by category. Every card links to a full guide with four try-it examples and FAQs.

Foundation & Files

2 tutorials

Declare encoding and pull in external stylesheets.

Fonts & Motion

2 tutorials

Register custom typefaces and define animation keyframes.

Conditions & Layout

3 tutorials

Test features, organize cascade priority, and build container-responsive components.

Advanced & Print

11 tutorials

Namespaces, print pages, typed properties, scope, entry styles, counters, and view transitions.

More

4 tutorials

Additional reference topics.

Examples Gallery

Five snippets showing each at-rule in action. Open the linked tutorials for live try-it editors.

🔠 File At-Rules

Encoding and importing stylesheets.

Example 1 — @charset UTF-8

Declare UTF-8 as the first line of an external CSS file.

CSS
@charset "UTF-8";

body {
  font-family: system-ui, sans-serif;
}
@charset Tutorial

How It Works

@charset is invisible on the page but tells the browser how to decode the CSS file bytes.

Example 2 — @import Stylesheet

Pull a variables file into your main stylesheet.

CSS
@import url("variables.css");
@import url("components.css");

body {
  margin: 0;
}
@import Tutorial

How It Works

Imports must appear before other rules. Each imported file’s rules cascade in order.

🎨 Fonts & Animation

Custom typefaces and motion effects.

Example 3 — @font-face

Register a custom font, then use it in font-family.

CSS
@font-face {
  font-family: BrandFont;
  src: url("brand.woff2") format("woff2");
}

h1 {
  font-family: BrandFont, sans-serif;
}

How It Works

@font-face downloads the font file; font-family: BrandFont applies it to matching elements.

Example 4 — @keyframes Definition

Define a fade-in animation with from and to steps.

CSS
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

How It Works

@keyframes only defines steps; you reference the name in a separate rule.

Example 5 — Applying a Keyframe Animation

Connect @keyframes to an element with the animation shorthand.

CSS
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.box {
  animation: fadeIn 2s ease-in-out;
}
Try It Yourself

How It Works

The animation property runs the fadeIn keyframes over 2 seconds with easing.

💬 Usage Tips

  • Order matters — Put @charset and @import at the very top.
  • Prefer link for critical CSS — Use <link> in HTML for faster parallel downloads.
  • woff2 first — In @font-face, list modern formats before fallbacks.
  • Name keyframes clearly — Use descriptive names like slideUpMenu not anim1.
  • Group at-rules — Keep fonts and keyframes in dedicated partial files.

⚠️ Common Pitfalls

  • @import after rules — Browsers ignore @import not at the top.
  • Comments before @charset — Strict parsers may reject anything before @charset.
  • Chained @import — Nested imports slow loading; prefer flat structure or HTML links.
  • Missing font formats — Provide woff2; add fallbacks for older browsers if needed.
  • Animation without keyframes — animation: fadeIn fails if @keyframes fadeIn is missing.

🧠 How CSS At-Rules Work

1

Parser sees @

The CSS engine recognizes an at-rule instead of a selector.

Parse
2

Rule-specific action

Import files, register fonts, or store keyframe definitions.

Process
3

Cascade with normal rules

Imported and declared styles merge with selector rules by cascade order.

Apply
=

Enhanced stylesheet

Modular files, custom fonts, and animations work together on the page.

🖥 Browser Compatibility

The at-rules covered on this page — @charset, @import, @font-face, and @keyframes — have 100% browser support in Chrome, Firefox, Safari, Edge, and Opera.

✓ Baseline · Universal support

CSS At-Rules

All major browsers support these at-rules in linked .css files and embedded <style> blocks.

100% Modern browsers
Google Chrome All versions · External CSS
Full support
Mozilla Firefox All versions · External CSS
Full support
Apple Safari All versions · External CSS
Full support
Microsoft Edge All versions · External CSS
Full support
Opera All modern versions
Full support
CSS at-rules 100% supported

Bottom line: @charset, @import, @font-face, and @keyframes are safe for production. Prefer <link> over @import for critical-path performance; use woff2 in @font-face for best font loading.

🎉 Conclusion

CSS at-rules control stylesheet behavior beyond normal selectors. They enable modular CSS with @import, correct encoding with @charset, custom typography with @font-face, and rich motion with @keyframes.

Use this hub to compare at-rules, then dive into each tutorial for try-it examples and detailed FAQs. Pair at-rules with media queries and properties for complete responsive designs.

💡 Best Practices

✅ Do

  • Place @charset and @import at the file top
  • Use <link> for critical stylesheets
  • Provide woff2 in @font-face
  • Name @keyframes descriptively
  • Split fonts and animations into partial files

❌ Don’t

  • Put rules before @import
  • Chain many nested @import files
  • Skip fallback fonts in font-family
  • Reference undefined keyframe names
  • Put secrets in imported CSS comments

Key Takeaways

Knowledge Unlocked

Five things to remember about CSS at-rules

Your map to all four at-rule tutorials on this site.

5
Core concepts
utf 02

@charset

First line.

Encode
imp 03

@import

Modular.

Files
font 04

@font-face

Custom.

Type
key 05

@keyframes

Motion.

Animate

❓ Frequently Asked Questions

An at-rule is a CSS statement that starts with @ and provides instructions beyond normal selector blocks. Examples include @charset for encoding, @import for other files, @font-face for custom fonts, and @keyframes for animations.
Regular rules use selectors like p or .card followed by { property: value; }. At-rules begin with @ and often have special syntax — some use a block { }, others end with a semicolon like @import url("file.css");
@charset must be the very first item if used. @import rules must come before any other rules except @charset. @font-face and @keyframes can appear anywhere, though many developers group them near the top.
@keyframes defines named animation steps. You reference that name with animation: fadeIn 2s; on an element. Pair @keyframes with transition or animation properties for motion effects.
For performance, <link rel="stylesheet"> in HTML is usually better because browsers download linked files in parallel. @import is convenient for splitting CSS into modules inside one file, but it can slow page load.
Read the overview and comparison table, try the five examples, then open the @charset tutorial first if you are new to at-rules. Follow the sidebar order: charset, font-face, import, keyframes.

Start Your First At-Rule Tutorial

Open the @charset guide, or follow sidebar order through @keyframes.

@charset tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

22 people found this page helpful