Class selectors let you style many elements the same way by giving them a shared name. This helps keep your webpage looking neat and consistent.
Class selectors in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
.className { property: value; }
.highlight { background-color: yellow; }
<p class="highlight">This paragraph is highlighted.</p>.button { color: white; background-color: blue; padding: 0.5rem 1rem; border-radius: 0.25rem; }
This webpage shows two paragraphs and two buttons. The paragraph with class 'highlight' has a yellow background and bold text. The button with class 'button' is styled with white text on blue background and rounded corners. The other paragraph and button have default styles.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Class Selector Example</title> <style> .highlight { background-color: yellow; padding: 1rem; font-weight: bold; } .button { color: white; background-color: blue; padding: 0.5rem 1rem; border-radius: 0.25rem; border: none; cursor: pointer; } </style> </head> <body> <p>This paragraph is normal.</p> <p class="highlight">This paragraph is highlighted with a yellow background.</p> <button class="button">Click Me</button> <button>Normal Button</button> </body> </html>
You can add multiple classes to one element by separating them with spaces, like class="button highlight".
Class names should be meaningful and easy to remember.
Class selectors are case sensitive in CSS, so .Button and .button are different.
Class selectors start with a dot and target elements with that class name.
They let you style many elements the same way easily.
Use class selectors to keep your webpage consistent and organized.
Practice
Solution
Step 1: Understand CSS selector symbols
Class selectors always start with a dot (.) to target elements with that class.Step 2: Compare with other selectors
ID selectors use #, element selectors use no symbol, and * selects all elements.Final Answer:
A dot (.) before the class name -> Option AQuick Check:
Class selector = dot (.) [OK]
- Using # instead of . for class selectors
- Omitting the dot before the class name
- Confusing class selectors with element selectors
highlight?Solution
Step 1: Identify correct class selector syntax
The class selector uses a dot (.) followed by the class name, so.highlightis correct.Step 2: Check other options
#highlight targets an ID, highlight alone targets elements named 'highlight', and *highlight is invalid syntax.Final Answer:
.highlight { color: red; } -> Option CQuick Check:
Class selector syntax = dot + class name [OK]
- Using # instead of . for classes
- Leaving out the dot before class name
- Confusing class selectors with element selectors
<p class="note">Hello!</p>.note { color: blue; }.alert { color: red; }Solution
Step 1: Match class in HTML with CSS selector
The <p> tag has class "note", so the CSS rule.note { color: blue; }applies.Step 2: Check other CSS rules
The .alert class styles red but does not apply here because the element does not have that class.Final Answer:
Blue -> Option BQuick Check:
Class matches CSS selector = blue text [OK]
- Confusing class names and applying wrong styles
- Ignoring that only matching classes apply styles
- Assuming default color when class is present
menu? menu { font-size: 1.2rem; }Solution
Step 1: Check selector syntax
The selectormenutargets elements named <menu>, not class "menu". Class selectors need a dot before the name.Step 2: Verify other parts
Curly braces and font size are correct. Class names are case-sensitive but lowercase is valid.Final Answer:
Missing dot before class name -> Option DQuick Check:
Class selector needs dot (.) [OK]
- Forgetting the dot before class name
- Confusing element selectors with class selectors
- Assuming uppercase class names are required
primary to have a blue background and white text, but only when hovered. Which CSS code correctly uses class selectors and pseudo-classes?Solution
Step 1: Identify correct class selector with hover
To style elements with class "primary" on hover, use.primary:hover.Step 2: Check other options
#primary targets an ID, not class. button.primary styles all buttons with class but not on hover. .primary styles all with class but no hover effect.Final Answer:
.primary:hover { background-color: blue; color: white; } -> Option AQuick Check:
Class selector + :hover pseudo-class = .primary:hover { background-color: blue; color: white; } [OK]
- Using # instead of . for class selectors
- Forgetting :hover for hover effect
- Styling without hover when hover is needed
