CSS Selectors
1. What are CSS Selectors?
CSS selectors are patterns used to select HTML elements.
Instead of styling every element individually, selectors allow you to apply styles efficiently.
Example
p{
color:blue;
}
The selector p selects every paragraph on the page.
2. Element Selector
The Element Selector selects all HTML elements of the same type.
Syntax
h1{
color:red;
}
HTML Example
Welcome
CSS Tutorial
Output
- Both headings become red.
| Selector | Selects |
|---|---|
| h1 | All <h1> elements |
| p | All paragraphs |
| img | All images |
3. ID Selector (#)
The ID Selector selects one unique HTML element.
HTML
Welcome to CSS
CSS
#title{
color:green;
text-align:center;
}
| Symbol | Meaning |
|---|---|
| # | ID Selector |
4. Class Selector (.)
The Class Selector styles multiple HTML elements having the same class name.
HTML
First Paragraph
Second Paragraph
CSS
.note{
color:blue;
font-weight:bold;
}
| Symbol | Meaning |
|---|---|
| . | Class Selector |
5. Universal Selector (*)
The Universal Selector selects every HTML element on the page.
Example
*{
margin:0;
padding:0;
box-sizing:border-box;
}
Uses
- Reset browser default spacing.
- Apply common styles to all elements.
- Create consistent layouts.
6. Grouping Selector (,)
The Grouping Selector is used to apply the same CSS style to multiple HTML elements at the same time.
Syntax
h1, h2, p{
color:blue;
}
Output
- All
<h1>elements become blue. - All
<h2>elements become blue. - All
<p>elements become blue.
7. Descendant Selector (Space)
The Descendant Selector selects elements that are inside another element, regardless of how deeply they are nested.
HTML
Paragraph One
Paragraph Two
CSS
div p{
color:red;
}
Output
- Paragraph inside the div becomes red.
- The outside paragraph remains unchanged.
8. Child Selector (>)
The Child Selector selects only the direct children of an element.
HTML
Direct Child
Nested Paragraph
CSS
div > p{
color:green;
}
Output
- The first paragraph becomes green.
- The nested paragraph is not selected.
9. Adjacent Sibling Selector (+)
The Adjacent Sibling Selector selects the first element immediately following another element.
HTML
Heading
Paragraph One
Paragraph Two
CSS
h2 + p{
color:purple;
}
Output
- Only the first paragraph becomes purple.
- The second paragraph remains unchanged.
10. General Sibling Selector (~)
The General Sibling Selector selects all sibling elements that come after a specified element.
HTML
Heading
Paragraph One
Paragraph Two
Paragraph Three
CSS
h2 ~ p{
color:orange;
}
Output
- All paragraphs after the heading become orange.
| Selector | Meaning |
|---|---|
| , | Grouping Selector |
| Space | Descendant Selector |
| > | Child Selector |
| + | Adjacent Sibling Selector |
| ~ | General Sibling Selector |
11. Attribute Selector
The Attribute Selector selects HTML elements based on their attributes.
Example
input[type="text"]{
border:2px solid blue;
}
HTML
Output
- The text box gets a blue border.
- The password box remains unchanged.
12. CSS Selector Best Practices
- Use meaningful class names.
- Avoid unnecessary long selectors.
- Prefer class selectors over ID selectors for reusable styles.
- Use ID selectors only for unique elements.
- Keep your CSS simple and organized.
- Group similar selectors together.
- Use external CSS for large projects.
- Avoid duplicate CSS rules.
- Write readable and properly indented code.
- Test selectors in different browsers.
13. Common Selector Mistakes
| Mistake | Correct Practice |
|---|---|
| Using spaces incorrectly | Understand descendant and child selectors. |
| Repeating the same styles | Use grouping selectors. |
| Using too many ID selectors | Use classes whenever possible. |
| Complex selectors | Keep selectors short and readable. |
| Wrong class or ID name | Check spelling carefully. |
❌ Wrong
.note p div{
color:red;
}
✔ Better
.note{
color:red;
}
14. CSS Selector Interview Questions
- What is a CSS selector?
- What is the difference between ID and Class selectors?
- Which symbol is used for a Class selector?
- Which symbol is used for an ID selector?
- What is the Universal Selector?
- What is a Grouping Selector?
- What is the Descendant Selector?
- What is the Child Selector?
- What is the Adjacent Sibling Selector?
- What is the General Sibling Selector?
- What is an Attribute Selector?
- When should you use Class selectors?
- When should you use ID selectors?
- Why are selectors important in CSS?
- Which selector is best for reusable styles?
15. Lesson Summary
In this lesson, you learned:
- ✔ What CSS Selectors are.
- ✔ Element Selector.
- ✔ ID Selector.
- ✔ Class Selector.
- ✔ Universal Selector.
- ✔ Grouping Selector.
- ✔ Descendant Selector.
- ✔ Child Selector.
- ✔ Adjacent Sibling Selector.
- ✔ General Sibling Selector.
- ✔ Attribute Selector.
Quick Quiz
Which symbol is used for the Class Selector?