Read and write selectors, declarations, properties, and values.
A CSS rule chooses elements and sets their styles. In this lesson, you will connect an HTML class to a CSS selector, then add declarations that tell the browser what to change.
Compare the selector in the stylesheet with the class on the heading in the HTML tab.
.highlight {
color: green;
font-size: 18px;
}The heading’s class value is highlight. A class selector starts with a dot, so .highlight matches this heading. The heading appears green at 18 CSS pixels.
Read the rule as: “Find elements with the class highlight, then apply these declarations.” A declaration sets a CSS property to a value. Here, color: green and font-size: 18px are two declarations.
| Part | Example | Purpose |
|---|---|---|
| Selector | .highlight | Chooses the elements to style. |
| Property | color | Names the feature to change. |
| Value | green | Specifies the setting for that feature. |
| Declaration block | Everything between { and } | Groups the declarations for the selector. |
The selector and its declaration block form the complete style rule.
Use the HTML hook first, then add the changes inside the block:
.highlight because the heading has class="highlight". The dot means “match this class,” rather than every heading.{ and } to make a declaration block.color: green;. color is the property to change; green is its value.font-size: 18px;. font-size expects a size, and 18px means 18 pixels.Each declaration changes one feature. That is why changing green changes the color without changing the text size, and changing 18px changes the size without changing the selector.
Before moving on, check your reasoning: if the HTML were <p class="note">, which selector would match it? The answer is .note, because the class name stays the same and the dot still marks a class selector.
Now complete this rule in your head. The selector and first declaration are already provided; add a declaration that makes the text 18 pixels:
.note {
color: purple;
__________________
}The missing declaration is font-size: 18px;. It goes inside the same braces because it changes another feature of the same matching elements.
Now use the same process with a new class. The HTML gives the heading a class named notice:
.notice {
__________________
__________________
}Fill the two blanks in the stylesheet to make the heading green with a font size of 18 pixels.
The completed rule is:
.notice {
color: green;
font-size: 18px;
}The tag changed from h1 to h2, but the reasoning did not: .notice matches because the class value is notice. The declarations go inside the braces because both changes apply to the same matching elements.
Each symbol has a job:
{ starts the declaration block, and } ends it.: separates a property from its value.; separates one declaration from the next.For example, color = green uses the wrong separator. The declaration must use a colon: color: green;. When a block has more than one declaration, the semicolon keeps the declarations separate. The final semicolon before } is optional, but keeping it makes adding another declaration less error-prone. A line break does not replace a semicolon.
These two rules have the same effect:
.highlight{color:green;font-size:18px;}.highlight {
color: green;
font-size: 18px;
}Use these formatting habits to make rules easier to scan:
font-size together.18px without a space between the number and its unit.Some spaces change the meaning of CSS. For example, 18px is valid, but 18 px is not: the number and unit must stay together.
A stylesheet can contain several style rules, each with its own selector and block:
h1 {
color: green;
}
p {
color: purple;
}The first rule selects headings; the second selects paragraphs. Their declarations change the text colors of the matching elements.
Try it yourself
HTML + CSS