Difficulty: Easy
Correct Answer: A class selector can be reused on many elements, while an id selector should be unique within a page and normally targets a single element.
Explanation:
Introduction / Context:
CSS uses class and id selectors to target elements in different ways. Understanding how they differ is essential for writing clean, semantic markup and styles. This question examines whether you know the intended reuse pattern and uniqueness rules for class and id attributes in HTML and CSS, a topic that appears frequently in interviews and coding practice.
Given Data / Assumptions:
- We are styling HTML elements using CSS class and id attributes.
- Class values are referenced with a dot prefix in CSS, and ids are referenced with a hash prefix.
- The page follows standard HTML practices regarding attribute usage.
- The focus is on reuse and uniqueness, not on performance myths.
Concept / Approach:
A class attribute is designed for grouping multiple elements that share a common style. In CSS, .button or .highlight can apply to many tags across the page. By contrast, an id attribute should be unique within a single HTML document and is usually used to identify a specific element, such as #main or #header. JavaScript often uses ids for element lookup. The correct answer must emphasize reusability for classes and uniqueness for ids.
Step-by-Step Solution:
Step 1: Recall that class attributes can appear on multiple elements at the same time.
Step 2: Remember that an id attribute should appear only once per page.
Step 3: Examine each option and look for the one that correctly states these usage rules.
Step 4: Select the statement that clearly contrasts reusable classes with unique ids.
Verification / Alternative check:
You can verify this by checking HTML specifications and best practice guides. They consistently describe class as a way to classify groups of elements and id as a unique identifier. Browser developer tools also treat ids as unique hooks for Dom inspection and scripting, while classes are designed for repeated application. This aligns with the selected option.
Why Other Options Are Wrong:
Option B is misleading because performance differences are minor and not the main conceptual distinction. Option C is wrong since id selectors can style any property, not just colors. Option D is false because classes can be used on any element, not just tables. Option E is incorrect because classes are not required to be unique, and ids should be unique only within a single document, not across an entire site.
Common Pitfalls:
A common pitfall is overusing ids for styling, which can make CSS hard to override and reuse. Another issue is assigning the same id to multiple elements, which breaks uniqueness assumptions and can confuse JavaScript. Good practice is to rely mainly on classes for styling and reserve ids for unique Dom hooks or layout anchors such as navigation targets.
Final Answer:
A CSS class selector is meant to be reused on many elements, while an id selector should be unique within a page and normally targets a single element.
Discussion & Comments