Element selectors help you style all elements of a certain type on your webpage. They make it easy to change the look of headings, paragraphs, or any HTML tag all at once.
Element selectors in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
element {
property: value;
}The element is the HTML tag name you want to style, like p or h1.
All elements of that type on the page will get the styles inside the curly braces.
p {
color: blue;
}<h1> headings to be bigger and bold.h1 {
font-size: 2rem;
font-weight: bold;
}button {
background-color: lightgray;
border-radius: 0.5rem;
}This page styles all <p> elements with dark green text and a bigger font. The <h2> heading is styled with dark red color and a clean font. No classes or IDs are needed.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Element Selector Example</title> <style> p { color: darkgreen; font-size: 1.2rem; font-family: Arial, sans-serif; } h2 { color: darkred; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } </style> </head> <body> <h2>Welcome to My Page</h2> <p>This paragraph is styled using an element selector.</p> <p>All paragraphs look the same without adding classes.</p> </body> </html>
Element selectors apply styles to every matching tag on the page, so use them when you want consistent styling.
They have lower priority than class or ID selectors, so more specific selectors can override them.
Use element selectors to keep your CSS simple and avoid repeating styles.
Element selectors target all HTML tags of a certain type.
They help style many elements quickly and consistently.
They are simple and useful for basic styling without extra classes.
Practice
p { color: red; }Solution
Step 1: Understand element selector syntax
The selectorptargets all <p> tags in the HTML.Step 2: Apply style to all matching elements
All <p> elements will have their text color changed to red.Final Answer:
Styles all <p> elements on the page -> Option AQuick Check:
Element selector targets all tags named in selector [OK]
- Confusing element selector with class or id selectors
- Thinking it styles only one element
- Assuming it targets elements with a class or id
Solution
Step 1: Identify element selector syntax
Element selectors use the tag name directly, likeh1.Step 2: Check other options
.h1 { font-size: 2rem; } uses a class selector, #h1 { font-size: 2rem; } uses an id selector, *h1 { font-size: 2rem; } is invalid syntax.Final Answer:
h1 { font-size: 2rem; } -> Option CQuick Check:
Element selector = tag name only [OK]
- Using dot (.) or hash (#) before tag name
- Adding invalid characters before tag name
- Confusing element selector with universal selector
<div> <p>Hello</p> <p>World</p> <span>!</span> </div>
And this CSS:
p { color: blue; }What color will the text inside the <span> be?
Solution
Step 1: Identify which elements the selector targets
The CSS selectorpstyles only <p> elements, not <span>.Step 2: Determine the <span> text color
Since <span> is not targeted, it keeps the default color (usually black).Final Answer:
Default (usually black) -> Option BQuick Check:
Element selector affects only matching tags [OK]
- Assuming all text changes color
- Confusing element selector with universal selector
- Thinking styles cascade to all child elements automatically
.li { color: green; }Solution
Step 1: Analyze the selector syntax
The selector.litargets elements with class 'li', not the <li> tag.Step 2: Identify correct selector for <li>
To select all <li> elements, useliwithout a dot.Final Answer:
It selects elements with class 'li', not <li> tags -> Option AQuick Check:
Dot means class selector, not element selector [OK]
- Using dot before tag name
- Confusing class and element selectors
- Ignoring selector syntax rules
Solution
Step 1: Confirm element selector usage
button { background-color: #0055cc; color: #ffffff; padding: 1rem; border-radius: 0.25rem; } usesbuttonwithout dot or hash, correctly selecting all <button> elements.Step 2: Check color contrast and styling
button { background-color: #0055cc; color: #ffffff; padding: 1rem; border-radius: 0.25rem; } uses a dark blue background (#0055cc) and white text (#ffffff), ensuring good contrast and accessibility. It also adds padding and border-radius for better usability.Final Answer:
button { background-color: #0055cc; color: #ffffff; padding: 1rem; border-radius: 0.25rem; } -> Option DQuick Check:
Element selector + accessible colors = button { background-color: #0055cc; color: #ffffff; padding: 1rem; border-radius: 0.25rem; } [OK]
- Using id or class selectors instead of element selector
- Choosing low contrast colors hurting accessibility
- Forgetting padding or border-radius for better button look
