Challenge - 5 Problems
Class Selector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ selector
intermediateWhat color will the text be?
Given the CSS and HTML below, what color will the text inside the
tag appear as in the browser?
CSS
HTML: <p class="highlight">Hello World</p> CSS: .highlight { color: red; } .highlight.special { color: blue; }
Attempts:
2 left
💡 Hint
Look at the class attribute in the HTML and which CSS selector matches it exactly.
✗ Incorrect
The paragraph has only the class 'highlight'. The selector '.highlight' applies red color. The '.highlight.special' selector requires both classes, which the paragraph does not have, so blue does not apply.
❓ layout
intermediateWhich CSS rule centers the box horizontally?
You want to center a
with class 'box' horizontally inside its parent. Which CSS rule using class selectors will do this correctly?
CSS
HTML: <div class="container"> <div class="box">Content</div> </div> CSS: .container { width: 300px; border: 1px solid black; } .box { width: 100px; height: 100px; background: lightblue; }
Attempts:
2 left
💡 Hint
Think about how margin auto works for block elements.
✗ Incorrect
Setting left and right margins to auto on a block element centers it horizontally inside its parent container. 'text-align' affects inline content, 'float: center' is invalid, and 'display: inline-block' alone does not center the box.
🧠 Conceptual
advancedWhat does this CSS selector target?
Consider the CSS selector '.menu > .item.active'. Which elements will this selector apply styles to?
Attempts:
2 left
💡 Hint
Look at the '>' symbol and the order of classes.
✗ Incorrect
The '>' means direct child. So '.menu > .item.active' targets elements that have both 'item' and 'active' classes and are direct children of an element with class 'menu'.
❓ accessibility
advancedHow to improve accessibility with class selectors?
You have a button with class 'btn-primary'. Which practice improves accessibility when styling this button using class selectors?
Attempts:
2 left
💡 Hint
Think about users with vision difficulties.
✗ Incorrect
High contrast colors help users with low vision read text easily. Light text on light background or very small fonts reduce readability and accessibility.
📝 Syntax
expertWhat error does this CSS code cause?
What error or problem will the following CSS code cause when used in a stylesheet?
.box { color: blue
background: yellow; }
CSS
.box { color: blue background: yellow; }
Attempts:
2 left
💡 Hint
Check punctuation carefully in CSS declarations.
✗ Incorrect
CSS requires each property declaration to end with a semicolon except the last one. Missing semicolon after 'color: blue' causes a syntax error and the whole rule may be ignored.
Practice
1. What does a CSS class selector start with to select elements by their class?
easy
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]
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
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]
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?
<p class="note">Hello!</p>.note { color: blue; }.alert { color: red; }medium
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]
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
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]
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
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]
Hint: Use .class:hover for hover styles on class elements [OK]
Common Mistakes:
- Using # instead of . for class selectors
- Forgetting :hover for hover effect
- Styling without hover when hover is needed
