My Studio

This is my studio page where I share all of my projects , experiments , learnings , blogs/articles , and also I share daily or frequent logs of my learnings or what currently I am working on.

CSS Selectors Explained

#CSS#Web Development#Frontend

Selectors are one of the first things you’ll encounter while learning css. They are the backbone of styling websites. CSS selectors allow you to target specific HTML elementsand apply style to them.

Why CSS Selectors Are Needed

Think of a webpage as a crowd of people at a party. Each person is an HTML element, like a paragraph (<p>), heading (<h1>), or a button (<button>). Now, imagine you want to tell some people to wear a red hat. Without knowing who they are, you can’t give instructions.

CSS selectors act like your way of addressing people in the crowd. You can call out:

  • Everyone in the room (all elements of a certain type)
  • People wearing a specific outfit (elements with a certain class)
  • A single person (element with a unique ID)

Selectors let you pick the right element to style, just like addressing the right person in a crowd.

Element Selector

The element selector is the simplest way to target elements. You just use the name of the HTML tag.

Example:

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
p {
  color: blue;
}

Result: All <p> elements turn blue.

It’s like saying, “Everyone who is a paragraph, wear blue!”

Class Selector

Sometimes, you don’t want to style all elements of a type, just some of them. That’s where classes come in. You assign a class to elements and target them with a dot (.) in CSS.

Example:

<p class="highlight">Important text</p>
<p>Normal text</p>
.highlight {
  background-color: yellow;
}

Result: Only the paragraph with class="highlight" gets a yellow background.

It’s like “People wearing a yellow jacket, stand out!”

ID Selector

When you want to target one unique element, use an ID. IDs are denoted with a hash (#) in CSS.

Example:

<h1 id="main-title">Welcome!</h1>
#main-title {
  font-size: 36px;
}

Result: Only the heading with id="main-title" changes size.

If we take the party eg . then there it is like “Hey John, you wear a red hat!” (addressing a single person uniquely)

IDs are unique, meaning no two elements should have the same ID on a page.

Group Selectors

Sometimes you want to apply the same style to multiple elements without repeating CSS. Group selectors let you do that using a comma.

Example:

h1, h2, h3 {
  font-family: Arial, sans-serif;
}

Result: All headings (h1, h2, h3) share the same font.

Eg. - “All managers and team leads, stand in line!”

Descendant Selectors

What if you want to style elements inside other elements? Descendant selectors help you target nested elements.

Example:

<div class="card">
  <p>This is inside a card.</p>
</div>
.card p {
  color: green;
}

Result: Only paragraphs inside .card turn green.

Comparing to real eg. “Everyone sitting at the VIP table, wear green hats!”

Basic Selector Priority (Very High Level)

Sometimes multiple selectors apply to the same element. CSS decides which one wins using priority (specificity):

  • Element selectors → least specific

  • Class selectors → medium specificity

  • ID selectors → most specific

Example:

p { color: blue; }      /* element selector */
.highlight { color: yellow; } /* class selector */
#main { color: red; }    /* ID selector */

If a paragraph has class="highlight" and id="main", it will appear red, because the ID selector has the highest priority.

Here’s a quick eg to visualize how selectors work:

HTML:

<h1 id="title">Welcome!</h1>
<p class="highlight">Hello World</p>
<p>Regular text</p>

CSS:

h1 { color: blue; }
.highlight { color: orange; }

The heading turns blue, the highlighted paragraph turns orange, and the normal paragraph stays default.

This demonstrates how selectors allow precise targeting.

CSS selectors are the starting point for styling any website. Once you understand element → class → ID → groups → descendants, you can style any HTML structure with ease. Think of them as your map to communicate with the right elements, without them, you’d be shouting blindly at your webpage!