Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 139 ยท Getting Started

CSS Syntax

Learn the anatomy of CSS rules, declarations, properties and values, how whitespace and case work, why invalid CSS is skipped, and what at-rules are.

CSS has one of the smallest grammars of any language you will learn. A stylesheet is just a list of rules, and almost every rule has the same shape. Once you can read that shape fluently, you can read any stylesheet, no matter how large.

In this lesson you will name every part of a rule, see how whitespace and letter case behave, and learn something unusual about CSS: when it meets a mistake, it doesn't stop. It quietly skips the part it can't understand and carries on. That behavior is both forgiving and a common source of "why doesn't this work?" moments, so it pays to understand it.

Anatomy of a rule

Here is a typical rule, sometimes called a ruleset:

CSS
.card {
  background: white;
  padding: 16px;
  border-radius: 8px;
}

Its parts are:

  • Selector: .card. Chooses the elements to style. Here it's every element with class="card".
  • Declaration block: everything between { and }.
  • Declarations: each property: value; line inside the block.
  • Property: the name before the colon, such as padding. It says which aspect of the element to change.
  • Value: what comes after the colon, such as 16px. It says how to change it.

Semicolons separate declarations. Strictly, the last one in a block doesn't need a semicolon, but always add it anyway. When you later add another line below, a missing semicolon is an easy bug to create and a hard one to spot.

A complete rule

Values come in many shapes

Every property accepts particular kinds of values. A few you will meet constantly:

  • Keywords: bold, center, none, auto.
  • Lengths: a number plus a unit, like 16px, 1.5rem or 50%. Zero needs no unit: margin: 0;.
  • Colors: teal, #1e90ff, rgb(30 144 255).
  • Strings: quoted text, like "Georgia" or the content of a pseudo-element.
  • Functions: a name followed by parentheses, like calc(100% - 2rem) or url(/samples/sunset.svg).

Some properties take several values separated by spaces. padding: 8px 16px; sets vertical and horizontal padding at once. Others take comma-separated lists, such as the font stack font-family: Inter, Arial, sans-serif;, where commas mean "or, if that is not available".

Shorthand properties

Many related properties have a shorthand that sets them all at once. These two blocks are equivalent:

CSS
.box {
  border-width: 2px;
  border-style: solid;
  border-color: navy;
}

.box {
  border: 2px solid navy;
}

Shorthands are convenient, but watch out: a shorthand also resets any sub-properties you leave out to their initial values. You will meet shorthands for margin, padding, font, background and more throughout the course.

Several selectors, one rule

To apply the same declarations to different elements, list the selectors separated by commas. This is called grouping:

CSS
h1,
h2,
h3 {
  font-family: Georgia, serif;
  color: #333;
}

Putting each selector on its own line is a common convention that keeps long groups readable. You will learn every kind of selector in the next chapter, starting with Selector Basics.

Whitespace and case

Outside of strings, CSS doesn't care about whitespace. Spaces, tabs and line breaks between the parts of a rule are ignored, so these two rules are identical to the browser:

CSS
p{color:gray;margin:0}

p {
  color: gray;
  margin: 0;
}

Minified production CSS often looks like the first version to save bytes, but write the second. One declaration per line, two-space indentation and a space after each colon make stylesheets far easier to read and to compare in version control.

There are a few places where whitespace does matter:

  • Between values: 1px solid red is three values; 1pxsolid red is nonsense.
  • Inside selectors: div p and div.p mean completely different things, as you will see in Combinators.
  • Inside calc(): + and - must have spaces around them. calc(100% - 20px) works; calc(100%-20px) does not.

As for letter case: property names and keywords are case-insensitive (COLOR: RED technically works), but everyone writes them in lowercase. Class names and IDs are case-sensitive, though: .Card will not match class="card". Element names in HTML documents aren't case-sensitive, but lowercase is the convention.

Mistakes are skipped, not fatal

What happens if you misspell a property or give it a value it doesn't understand? In most programming languages, an error stops everything. CSS takes a different approach: the browser ignores the invalid declaration and keeps going. Every other valid declaration still applies.

Invalid declarations are ignored

The note gets its background, border and font size, but neither the crimson text nor the padding. Fix the typos (color, padding: 12px) and run it again.

This forgiving behavior is a deliberate design choice. It means a browser that doesn't support a new property simply skips it, so you can safely write modern CSS with older fallbacks right before it:

CSS
.panel {
  background: rgb(240 240 255);      /* every browser understands this */
  background: color-mix(in oklch, white, blue 6%); /* newer browsers override it */
}

Older browsers ignore the line they don't understand and keep the first one. Newer ones apply both in order, so the second wins.

Silent failure cuts both ways. Because the browser never complains, a typo can sit in a stylesheet unnoticed for months. When a style isn't applying, check the declaration in your browser's DevTools: invalid declarations are shown struck through with a warning icon. You will learn to do this in Inspecting CSS with DevTools.

Broken brackets are worse

A misspelled property only costs you one declaration. Missing punctuation can cost more. A forgotten closing brace makes the browser read the following rules as part of the broken block, so they can vanish too:

A missing brace

Here the browser sees p { while it's still inside the h2 block, so it treats the p and .after rules as nested rules that only match inside an h2 (CSS now supports nesting). The paragraphs are not inside an h2, so they lose both the purple and the bold. Add the missing } after color: darkcyan; and everything works again. Good indentation makes this kind of mistake easy to spot.

At-rules

Not every statement in CSS is a selector rule. At-rules start with an @ sign and give the browser instructions or conditions. Some end with a semicolon, others have a block. You will study each of these in its own lesson; for now, just recognize them:

CSS
/* Load a font file */
@font-face {
  font-family: "Brand";
  src: url(/fonts/brand.woff2) format("woff2");
}

/* Apply rules only on wide screens */
@media (width >= 50rem) {
  .sidebar {
    display: block;
  }
}

/* Apply rules only if the browser supports a feature */
@supports (display: grid) {
  .gallery {
    display: grid;
  }
}

/* Define an animation */
@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* Declare the order of cascade layers */
@layer reset, base, components;

The most common at-rules are @media (Media Queries), @font-face (Web Fonts), @keyframes (Keyframe Animations), @supports (Feature Queries), @layer (Cascade Layers) and @container (Container Queries).

Your first at-rule

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In .alert { border: 1px solid red; }, which part is the value?

    .alert is the selector, border the property, and 1px solid red the value. Everything between the braces is the declaration block.
  2. 2. A rule contains colour: blue; and margin: 10px;. What does the browser do?

    Invalid declarations are skipped one by one; the rest of the rule still applies. CSS only knows the US spelling color.
  3. 3. Which of these is case-sensitive?

    Class names and IDs must match the HTML exactly, so .Card does not match class="card". Properties and keywords are case-insensitive, though lowercase is the convention.
  4. 4. Why is it useful that browsers skip CSS they don't understand?

    Older browsers ignore the newer declaration and keep the fallback, while newer browsers apply the later one. That is progressive enhancement.
  5. 5. Which statement is an at-rule?

    At-rules start with @. @media applies the rules inside it only when its condition is true.

Summary

  • A rule is a selector followed by a declaration block; each declaration is property: value;.
  • Values can be keywords, lengths, colors, strings or functions; shorthands set several properties at once.
  • Comma-separated selectors share one declaration block.
  • Whitespace mostly doesn't matter; class names and IDs are case-sensitive.
  • Invalid declarations are skipped silently, which allows fallbacks but hides typos. A missing brace can swallow later rules.
  • At-rules like @media, @font-face and @keyframes start with @ and give the browser instructions or conditions.

Next: Comments and Code Organization, where you learn to annotate and structure a stylesheet so it stays readable as it grows.