34. Element, class and ID selectors
Module 7 · About 20 minutes · Lesson 33; review earlier concepts as needed
Open Interactive LessonDiscover
What changes when you use element, class and id selectors, and how can you prove it?
- Explain element, class and id selectors using the example source
- Predict the visual or structural effect of one controlled change
- Repair the supplied fault and verify actual output
Predict
Record the expected structure or visual change before rendering. Name one thing that should remain unchanged.
Understand
A type selector matches element names, a class selector matches reusable class tokens, and an ID selector matches a unique identifier. Classes usually make reusable component styling easier than IDs. Specificity is only one stage in the cascade; importance, layers and origin can take precedence. Inspect actual matches before diagnosing a conflict.
Important source lines
- index.html:1 <h1 id="page-title">Selector practice</h1><p class="highlight">A reusable highlight.</p><p>Unselected text.</p>
- The highest-level heading for a page topic. Attribute guidance: global attributes.
- styles.css:1 .highlight { background-color: #fff0b3; }
- Paints the background color behind content and padding. Accepted grammar: <color>. Context: block.
The first example teaches this lesson’s primary contract. Additional examples are optional related references or applications, each with its own purpose and source; an example without an authored check requires the stated visual/manual review.
Example 1: Element, class and ID selectors
Only the paragraph with the matching class receives the background; the other paragraph remains unchanged.
index.html
<h1 id="page-title">Selector practice</h1><p class="highlight">A reusable highlight.</p><p>Unselected text.</p>
styles.css
.highlight { background-color: #fff0b3; }script.js
Expected: Only the paragraph with the matching class receives the background; the other paragraph remains unchanged.
Edit and Render This ExampleExample 2: Property: background-color
Paints the background color behind content and padding.
index.html
<section class="stage"><article class="sample"><h2>Readable content</h2><p>Longer sample text demonstrates wrapping, spacing and typography. Change a value, inspect its computed style and compare the resulting box.</p><a href="#details">Read the details</a></article><article class="peer">A second item</article></section><section id="details"><h2>Details</h2></section>
styles.css
body { font:18px/1.5 system-ui; color:#17324d; margin:1rem; }
.stage { padding:1rem; border:1px dashed #9caec0; } .sample { padding:1rem; border:2px solid #849ab1; max-width:30rem; }
.sample { background-color: #e3edf8; }script.js
Expected: Inspect .sample in the block context. Paints the background color behind content and padding. The browser’s CSS.supports and computed-style report are the authority for this installed browser; a declaration can be valid but depend on additional layout or font conditions.
Edit and Render This ExampleExample 3: Dismissible alert
An alert announces a high-priority message. Keep its dismiss control outside the live region.
index.html
<section data-component aria-label="Dismissible alert"> <div id="notice"><p role="alert">The file could not be saved.</p><button id="dismiss">Dismiss alert</button></div><button id="retry">Retry save</button> </section>
styles.css
* { box-sizing: border-box; }
body { margin: 0; color: #142537; background: #eef4fa; font: 16px/1.5 system-ui, sans-serif; }
[data-component] { max-width: 44rem; margin: 1rem auto; padding: 1rem; border: 1px solid #a1b2c4; border-radius: .75rem; background: white; }
button, input, select, textarea { font: inherit; padding: .4rem .65rem; margin: .2rem; }
button { cursor: pointer; } :focus-visible { outline: 3px solid #a33e0c; outline-offset: 3px; }
label { display: block; } [hidden] { display: none !important; }
table { border-collapse: collapse; } th, td { padding: .4rem; border-bottom: 1px solid #a1b2c4; }
script.js
const root=document.querySelector("[data-component]");
root.querySelector('#dismiss').onclick=()=>{root.querySelector('#retry').focus();root.querySelector('#notice').hidden=true;};Expected: Choose a focus target for a page with no Retry button.
Edit and Render This ExampleExample 4: Property: padding
Creates internal space between content and border.
index.html
<section class="stage"><article class="sample"><h2>Readable content</h2><p>Longer sample text demonstrates wrapping, spacing and typography. Change a value, inspect its computed style and compare the resulting box.</p><a href="#details">Read the details</a></article><article class="peer">A second item</article></section><section id="details"><h2>Details</h2></section>
styles.css
body { font:18px/1.5 system-ui; color:#17324d; margin:1rem; }
.stage { padding:1rem; border:1px dashed #9caec0; } .sample { padding:1rem; border:2px solid #849ab1; max-width:30rem; }
.sample { padding: 24px; }script.js
Expected: Inspect .sample in the block context. Creates internal space between content and border. The browser’s CSS.supports and computed-style report are the authority for this installed browser; a declaration can be valid but depend on additional layout or font conditions.
Edit and Render This ExampleExample 5: Element: main
The document’s principal content, normally one visible instance.
index.html
<main><h1>Principal content</h1><p>The central task belongs here.</p></main>
styles.css
body { font:18px/1.6 system-ui; margin:1.5rem; color:#17324d; } img,video,svg { max-width:100%; height:auto; } th,td { padding:.5rem; border:1px solid #a8b8c8; } :focus-visible { outline:3px solid #a44322; outline-offset:3px; }script.js
Expected: The document’s principal content, normally one visible instance. Review its real DOM and native behavior; structural/media-only examples are labeled.
Edit and Render This ExampleBuild and test
Only the paragraph with the matching class receives the background; the other paragraph remains unchanged. Repair the current exercise without removing necessary content. Checks inspect the stated structural/computed/stylesheet contract, not exact source text.
Starter index.html
<h1 id="page-title">Selector practice</h1><p class="highlight">A reusable highlight.</p><p>Unselected text.</p>
Starter styles.css
.missing { background-color: #fff0b3; }Objective contract
[
{
"type": "style",
"selector": ".highlight",
"property": "background-color",
"value": "rgb(255, 240, 179)",
"label": ".highlight computes background-color: rgb(255, 240, 179)"
}
]Hint 1
Locate the first difference between the intended result and the real preview. Inspect matching elements and computed styles.
Hint 2
The relevant source uses .highlight {. The exercise instead uses .missing {. Identify its effect before changing it.
Hint 3
Restore the intended .highlight { operation or implement an equivalent structure/style. Run the actual checks and explain why it works.
Complete reference solution
index.html
<h1 id="page-title">Selector practice</h1><p class="highlight">A reusable highlight.</p><p>Unselected text.</p>
styles.css
.highlight { background-color: #fff0b3; }Explain, reflect and extend
Which part of the actual preview or inspector proves this result? Explain one boundary case and name what automated checks do not verify.
Apply this concept to a different realistic piece of content. Predict its structure or visual behavior, test narrow and wide widths where relevant, then record one invalid case and its repair.
Troubleshooting and professional practice
If nothing changes, confirm that you edited the active file and ran Run Preview, or enabled Auto Preview. Inspect the actual DOM because the browser may repair invalid nesting. For CSS, check selector matches, valid declarations, active queries and computed values. Preserve local asset paths and inspect warnings. Only the paragraph with the matching class receives the background; the other paragraph remains unchanged.
Knowledge check
Explain why the stated result follows from the source. Predict a second input or width and test it independently. Automated contracts and written explanations are reviewed separately.
Optional AI prompts
- Explain this lesson’s source line by line using the actual objective above.
- Help implement one small extension and define normal, boundary and failure tests.
- Review/debug the source against actual preview observations; verify standards and never claim rendering without evidence.
Continue to Lesson 35: Combining selectors and organizing styles