TExcelTAD
Home › CSS › CSS Syntax

CSS Syntax

Every piece of CSS you write is made of rules (also called rule sets). A rule has two parts: a selector that picks elements, and a declaration block that says what to do with them.

css
h1 {
  color: navy;
  font-size: 32px;
}
Part In the example Meaning
Selector h1 Which elements the rule applies to
Declaration block { ... } Everything between the curly braces
Declaration color: navy; One property-value pair
Property color The aspect you want to change
Value navy What to set the property to

Declarations

A declaration is always written as property: value;

  • A colon separates the property from its value.
  • A semicolon ends the declaration.
  • You can put as many declarations in one block as you like.

Technically, the semicolon after the last declaration is optional, but always writing it is a good habit: when you later add another line, you will not accidentally break the rule.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  p {
    color: white;
    background-color: #264de4;
    padding: 12px;
    font-family: sans-serif;
    font-size: 18px;
    border-radius: 6px;
  }
</style>
</head>
<body>
  <p>One selector, six declarations.</p>
  <p>Every paragraph gets the same style.</p>
</body>
</html>

Whitespace and formatting

CSS ignores extra spaces and line breaks between tokens. These two rules are identical to the browser:

css
h2{color:red;margin:0}

h2 {
  color: red;
  margin: 0;
}

The second version is much easier for humans to read, so use one declaration per line and indent with two or four spaces. Tools called minifiers later squash your CSS into the compact form to save bytes.

Several selectors, one rule

If multiple elements should share the same styles, separate the selectors with commas. This is called grouping.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  h1, h2, h3 {
    font-family: Georgia, serif;
    color: darkslateblue;
    border-bottom: 2px solid #ccc;
  }
  h3 { color: crimson; }  /* override just the color */
</style>
</head>
<body>
  <h1>Main heading</h1>
  <h2>Sub heading</h2>
  <h3>Small heading</h3>
</body>
</html>

Values come in many types

The value you write depends on the property. Some common value types:

Value type Examples
Keywords bold, center, none, auto
Colors red, #ff6600, rgb(0 128 255)
Lengths 16px, 2em, 50%, 10vw
Numbers 1.5 (for line-height), 0.8 (for opacity)
Functions url(...), calc(100% - 20px), linear-gradient(...)
Lists font-family: Arial, Helvetica, sans-serif;

Some properties accept several values at once, separated by spaces. These are usually shorthand properties:

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  .box {
    border: 4px dashed orange;   /* width, style, color */
    margin: 20px auto;           /* vertical, horizontal */
    padding: 10px 30px;
    width: 200px;
    font: italic bold 20px sans-serif;
    text-align: center;
  }
</style>
</head>
<body>
  <div class="box">Shorthand values</div>
</body>
</html>

Case sensitivity

Property names and keywords are not case-sensitive (COLOR: RED works), but the convention is all lowercase. Class names and IDs, however, are case-sensitive: .Note and .note are different selectors.

Tip: If a declaration seems to do nothing, check the punctuation first. A missing colon or semicolon, or a typo in the property name, makes the browser skip that declaration silently.

Test yourself

In p { color: red; }, what is color?

  1. The selector
  2. The property
  3. The value
  4. The declaration block

Which character separates a property from its value?

  1. =
  2. ;
  3. :
  4. ,

How do you apply one rule to both h1 and p elements?

  1. h1 + p { }
  2. h1 p { }
  3. h1, p { }
  4. h1 & p { }