Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Styling a Simple Webpage Using Class Selectors
📖 Scenario: You are creating a simple webpage with different sections. You want to style these sections using CSS class selectors to give each a unique look.
🎯 Goal: Build a webpage with three sections and style each section differently using CSS class selectors.
📋 What You'll Learn
Create an HTML structure with three elements, each having a unique class name.
Add a CSS block that uses class selectors to style each section with different background colors and padding.
Use semantic HTML5 elements.
Ensure the CSS uses class selectors with a dot prefix.
Make the page responsive with readable text and distinct section backgrounds.
💡 Why This Matters
🌍 Real World
Class selectors are used daily to style websites, allowing developers to target specific groups of elements easily and consistently.
💼 Career
Understanding class selectors is essential for front-end web development jobs, as they form the foundation of CSS styling and layout control.
Progress0 / 4 steps
1
Create the HTML structure with three sections
Write the HTML code to create three <section> elements with the classes intro, content, and footer. Each section should contain a <p> with some placeholder text.
CSS
Hint
Use the class attribute inside each <section> tag to assign the classes intro, content, and footer.
2
Add a CSS block with class selectors
Create a <style> block and define CSS rules for the classes .intro, .content, and .footer. For now, set the padding property to 1rem for all three classes.
CSS
Hint
Use the dot . before each class name in CSS to select that class. For example, .intro { padding: 1rem; }.
3
Add background colors to each class
Inside the existing <style> block, add a background-color property to each class: .intro should be #e0f7fa, .content should be #fff9c4, and .footer should be #ffe0b2.
CSS
Hint
Write background-color: #e0f7fa; inside the .intro block, and similarly for the other classes.
4
Add a responsive font size to paragraphs
Inside the <style> block, add a CSS rule for section p that sets the font-size to 1.125rem and the line-height to 1.5 for better readability on all devices.
CSS
Hint
Use the selector section p to style all paragraphs inside sections.
Practice
(1/5)
1. What does a CSS class selector start with to select elements by their class?
easy
A. A dot (.) before the class name
B. A hash (#) before the class name
C. No symbol, just the class name
D. An asterisk (*) before the class name
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 A
Quick Check:
Class selector = dot (.) [OK]
Hint: Class selectors always begin with a dot (.) [OK]
Common Mistakes:
Using # instead of . for class selectors
Omitting the dot before the class name
Confusing class selectors with element selectors
2. Which of the following is the correct CSS syntax to style all elements with class highlight?
easy
A. highlight { color: red; }
B. #highlight { color: red; }
C. .highlight { color: red; }
D. *highlight { color: red; }
Solution
Step 1: Identify correct class selector syntax
The class selector uses a dot (.) followed by the class name, so .highlight is 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 C
Quick Check:
Class selector syntax = dot + class name [OK]
Hint: Remember: dot + class name for class selectors [OK]
Common Mistakes:
Using # instead of . for classes
Leaving out the dot before class name
Confusing class selectors with element selectors
3. Given this HTML and CSS, what color will the text inside the <p> tag be?
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 B
Quick Check:
Class matches CSS selector = blue text [OK]
Hint: Match class attribute with dot selector name [OK]
Common Mistakes:
Confusing class names and applying wrong styles
Ignoring that only matching classes apply styles
Assuming default color when class is present
4. What is wrong with this CSS if you want to style elements with class menu?
menu { font-size: 1.2rem; }
medium
A. Font size value is invalid
B. Missing curly braces
C. Class name should be uppercase
D. Missing dot before class name
Solution
Step 1: Check selector syntax
The selector menu targets 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 D
Quick Check:
Class selector needs dot (.) [OK]
Hint: Class selectors always start with a dot (.) [OK]
Common Mistakes:
Forgetting the dot before class name
Confusing element selectors with class selectors
Assuming uppercase class names are required
5. You want to style all buttons with class primary to have a blue background and white text, but only when hovered. Which CSS code correctly uses class selectors and pseudo-classes?
hard
A. .primary:hover { background-color: blue; color: white; }
B. #primary:hover { background-color: blue; color: white; }
C. button.primary { background-color: blue; color: white; }
D. .primary { background-color: blue; color: white; }
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 A