iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

CSS Syntax

A CSS rule is made of a selector and a declaration block. The selector picks which elements to target; the block lists what to change about them.

Anatomy of a CSS rule

h1 { color : #04AA6D ; } selector property value A complete property:value pair is a declaration.
Fig 1. The four parts of a CSS rule.

Vocabulary

TermWhat it isExample
SelectorThe pattern that picks elements.h1, .btn, #hero
Declaration blockEverything between the curly braces.{ color: red; font-size: 14px; }
DeclarationOne property: value pair.color: red
PropertyThe thing you want to change.color, margin
ValueWhat to set the property to.red, 20px
Tip: The semicolon after each declaration is technically optional on the last one. Leave it in anyway — adding new lines later won't break.

Example

Example
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Verdana, sans-serif; }
.box { background: #04AA6D; color: #fff; padding: 20px; border-radius: 6px; }
</style>
</head>
<body>

<h1>CSS Syntax</h1>
<div class="box">Edit the CSS on the left, then click Run &raquo;</div>

</body>
</html>
Try it Yourself »

Exercise

Complete the rule that makes every paragraph dark grey.

p { color #333; }

Test yourself

Q1. In `h1 { color: red; }`, what is `h1`?
Q2. A `property: value` pair is called a…
Q3. Which character separates declarations inside a block?

Discussion

Loading…