New: Complete Beginner's Guide to Coding is now available in Premium
Updated: Indian Govt Exam roadmaps now include salary breakdowns & timelines
Tip: Use the Career Hub to explore all career paths in one place
Tutorials CSS CSS Selectors

CSS Selectors

CSS

Selectors tell CSS which HTML elements to style. There are many types:

  • Elementp { } styles all paragraphs
  • Class.box { } styles elements with class="box"
  • ID#header { } styles the element with id="header"
  • Universal* { } styles everything

Classes are the most commonly used selector in real projects.

Example — CSS
/* Element selector */
h1 { color: red; }

/* Class selector (most used!) */
.highlight {
    background: yellow;
    padding: 4px 8px;
}

/* ID selector */
#main-title {
    font-size: 2.5rem;
    font-weight: bold;
}

/* Multiple selectors */
h1, h2, h3 {
    font-family: Arial, sans-serif;
}
Result

Red Heading (element)

Highlighted text (class)

Big Title (ID)

Interview Questions — CSS Selectors

5 questions commonly asked in interviews

Q1 What are CSS selectors? Beginner

CSS selectors are patterns used to select HTML elements for styling.

Q2 What is an element selector? Beginner

An element selector targets HTML tags directly, such as p, h1, or div.

Q3 What is a class selector? Beginner

A class selector targets elements using a class name and starts with a dot, like .box.

Q4 What is an ID selector? Intermediate

An ID selector targets a unique element using #, such as #header.

Q5 What is selector specificity? Advanced

Specificity decides which CSS rule has higher priority when multiple rules target the same element.

Frequently Asked Questions

Common doubts about CSS Selectors

Yes, one HTML element can have multiple classes separated by spaces.

Classes are better for reusable styling. IDs should be used for unique elements.

The * selector selects all elements on the page.

Yes, descendant selectors can target elements inside other elements.

In HTML, class and ID names are generally case-sensitive in CSS matching.

Test Your Knowledge

5 questions · Earn 50 XP

0 / 5
Q1 Which selector targets all paragraphs?
Q2 Which symbol is used for class selector?
Q3 Which symbol is used for ID selector?
Q4 Which selector selects all elements?
Q5 Which selector is more specific?

More on CSS Selectors

Cheatsheet, tips, resources & what to learn next

Quick Cheatsheet

Element Selector
p { color: red; }
Class Selector
.box { padding: 20px; }
ID Selector
#header { background: blue; }
Universal Selector
* { box-sizing: border-box; }
Descendant Selector
.card p { font-size: 16px; }

Pro Tips

1

Use classes for reusable design.

2

Avoid using too many ID selectors for styling.

3

Keep selector names meaningful.

4

Avoid very long nested selectors.

5

Use universal selector carefully because it affects all elements.

Up Next

CSS Colors CSS Box Model CSS Margin CSS Padding CSS Flexbox