Skip to main content
Home / Articles /

The At-Rules of CSS

The at-rule is a statement that provides CSS with instructions to perform or how to behave. Each statement begins with an @ followed directly by one of several available keywords that acts as the identifier for what CSS should do. This is the common syntax, though each at-rule is a variation of it.

Regular Rules

Regular rules are ones that follow a regular syntax:



This rule defines the character set used by the browser. It comes in handy if the stylesheet contains non-ASCII characters (e.g. UTF-8). Note that a character set that is placed on the HTTP header will override any @charset rule.

@charset "UTF-8";


This rule is inserted at the very top of the file and instructs the stylesheet to request and include an external CSS file as if the contents of that file were right where that line is.

@import 'global.css';

With the popularity of CSS preprocessors that support @import, it should be noted that they work differently than native CSS @import is a separate HTTP request for that file.


This rule is particularly useful for applying CSS to XML HTML (XHTML) so that XHTML elements can be used as selectors in the CSS.

/* Namespace for XHTML */
@namespace url(;

/* Namespace for SVG embedded in XHTML */
@namespace svg url(;

Nested Rules

Nested rules contain a subset of additional statements, some of which might be conditional to a specific situation.

  /* Nested Statements */


This rule specifies conditions for styles that apply to a specific page. For example, we can provide a URL then customize the styles for that particular page. Those styles will be ignored on other pages.

  /* Rules for a specific page */
  /* Rules for pages with a URL that begin with... */
  /* Rules for any page hosted on a domain */

  /* Rules for all secure pages */
  /* Start styling */
  body { font-family: Comic Sans; }


The support for @document is pretty weak at the time of this writing:

Chrome Safari Firefox Opera IE Android iOS
No No -moz No No No No


This rule allows us to load custom fonts on a webpage. There are varying levels of support for custom fonts, but this rule accepts statements that create and serve those fonts.

@font-face {
  font-family: 'MyWebFont';


This rule is the basis for keyframe animations on many CSS properties, by allowing us to mark the start and stop (and in-between) marks for what is being animated.

@keyframes pulse {
  0% {
    background-color: #001f3f;
  100% {
    background-color: #ff4136;


This rule contains conditional statements for targeting styles to specific screens. These statements can include screen sizes, which can be useful for adapting styles to devices:

/* iPhone in Portrait and Landscape */
@media only screen 
  and (min-device-width: 320px) 
  and (max-device-width: 480px)

    .module { width: 100%; }


Or applying styles only to the document when it is printed:

@media print {



This rule defines styles that are to individual pages when printing the document. It specifically contains pseudo-elements for styling the :first page as well as the :left and :right margins of the page.

@page :first {
  margin: 1in;


This rule tests whether a browser supports a feature, then applies the styles for those elements if the condition is met. It’s sort of like Modernizr but tailored specially for CSS properties.

/* Check one supported condition */
@supports (display: flex) {
  .module { display: flex; }

/* Check multiple conditions */
@supports (display: flex) and (-webkit-appearance: checkbox) {
  .module { display: flex; }

Here’s the, uh, support for @supports:

Chrome Safari Firefox Opera IE Android iOS
28+ No 31+ 12.1+ Edge 4.4+ No

Wrapping Up

The at-rule is where it’s at for making CSS do some crazy and interesting things. While the examples here are basic, we can see how they might be used to handcraft styles to very specific conditions, thereby creating user experiences and interactions that match a scenario.