What is the difference between classes and IDs in CSS?

Instruction: Explain the difference between using a class selector and an ID selector in CSS.

Context: This question tests the candidate's knowledge on CSS selectors and their specificity, which is crucial for styling HTML elements effectively.

Updated

Official answer available

Read the opening below, then unlock the full answer and practical guidance.

A class selector such as .card matches a class token and can style many elements. An ID selector such as #featured matches an ID value. An element may have multiple class tokens; HTML IDs must be unique within the element tree.

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS example</title> <style> .card { color: darkgreen; padding: 4px; } #featured...

Related Questions