CSS Selectors

CSS Selectors are used to select HTML elements that you want to style. They help the browser determine which elements should receive the CSS rules.

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.

A selector always comes before the declaration block.

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;

}
An ID should be unique and used only once on a web page.
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;

}
Every element having the class note will receive the same style.
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.
The Universal Selector is commonly used at the beginning of a stylesheet to normalize spacing.

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.
Grouping selectors reduce duplicate CSS code.

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.
A space between selectors represents a descendant selector.

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.
The > symbol selects only direct child elements.

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.
The + symbol selects the immediately following sibling.

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
These selectors help target HTML elements more precisely and make your CSS cleaner and more efficient.

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.
Attribute selectors are useful when you want to style elements with specific attributes.

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.
Well-written selectors make your CSS easier to maintain and improve website performance.

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;

}
Simple selectors improve code readability and page performance.

14. CSS Selector Interview Questions

  1. What is a CSS selector?
  2. What is the difference between ID and Class selectors?
  3. Which symbol is used for a Class selector?
  4. Which symbol is used for an ID selector?
  5. What is the Universal Selector?
  6. What is a Grouping Selector?
  7. What is the Descendant Selector?
  8. What is the Child Selector?
  9. What is the Adjacent Sibling Selector?
  10. What is the General Sibling Selector?
  11. What is an Attribute Selector?
  12. When should you use Class selectors?
  13. When should you use ID selectors?
  14. Why are selectors important in CSS?
  15. Which selector is best for reusable styles?
These questions are commonly asked in front-end developer interviews.

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.
Congratulations! You have successfully completed the CSS Selectors lesson. You can now target HTML elements efficiently using different types of selectors.

Quick Quiz

Which symbol is used for the Class Selector?