Skip to main content

Command Palette

Search for a command to run...

Concepts To Know

Published
•2 min read•View as Markdown

1. Why CSS selectors are needed

  1. They help target specific HTML elements

  2. They tell the browser which elements to style

  3. They avoid applying styles to unwanted elements

  4. They make styling precise and controlled

  5. They help separate styling logic clearly

  6. They allow different styles for different elements

  7. They make CSS reusable and maintainable

Easy Step:
CSS selectors act like an address system that tells CSS where to apply styles.

2. Element selector

Element selector

Example

p {
  color: blue;
}

Explanation

  • The element selector targets all <p> elements

  • The style is applied to every paragraph on the page

  • It is used when you want to style all elements of the same type

Easy Task:
Element selector = style all elements with the same tag name.

3. Class selector

Example

.box {
  color: red;
}

Explanation

  • Targets all elements with class="box"

  • Can be reused on multiple elements

4. ID Selector

Example

#title {
  color: blue;
}

Explanation

  • Targets the element with id="title"

  • ID must be unique

  • Has higher priority than class selector

5. Group Selector

Example

h1, h2, p {
  font-family: Arial;
}

Explanation

  • Applies the same style to multiple selectors

  • Helps reduce repeated CSS

  • Makes code cleaner

6. Descendant selector

Example

div p {
  color: green;
}

Explanation

  • Targets <p> elements inside a <div>

  • Helps style elements based on structure

  • Very useful for nested layouts

7. Basic selector priority (important)

From lowest to highest priority:

  1. Element selector (p)

  2. Class selector (.box)

  3. ID selector (#title)

Note:
If multiple styles apply, the one with higher priority wins.