CSS Selectors Explained: How CSS Targets HTML Elements

I have a Web Development knowledge and Strong JavaScript and frontend development expertise for creating user-friendly websites along with impressive functionalities. I can deliver maintainable and scalable code with Best Practices of coding, SEO and Web accessibility. I can create website with AI Integration or Rag Systems.
When we start using CSS, the main question becomes, how does CSS know which element to style? A webpage can have many elements, so CSS needs a way to select the correct ones. This is where CSS selectors come in. Selectors are used to choose specific HTML elements so styles can be applied to them.
You can think of selectors like calling someone by name in a group of people. If you call everyone, all respond. If you call a specific person, only that person responds. In the same way, selectors help CSS target the right elements on a webpage.
In this article, we will learn why selectors are needed and understand the most commonly used selectors step by step, starting from element selectors and moving towards more specific ones.
Why CSS Selectors Are Needed
A webpage usually has many HTML elements like headings, paragraphs, buttons, images, and containers. If CSS did not have selectors, it would not know which element should receive the style. Selectors help CSS choose specific elements so we can style only the parts we want.
For example, imagine you want to change only paragraph text color. Without selectors, all elements might get the same style. But with selectors, you can target only paragraphs. So selectors are important because they allow us to control exactly which elements get styled on a webpage.
CSS Selector Syntax
Before learning different selectors, let’s first understand the basic syntax of how CSS targets elements. A CSS rule normally has two parts: the selector and the style block. The selector chooses the element, and the style block contains the styles to apply.
Basic syntax looks like this:
selector {
property: value;
}
Example:
p {
color: blue;
}
This rule tells the browser to make all paragraph text blue.
Element Selector
The element selector targets all elements of a specific HTML tag on the page.
For example, if you want to style all paragraph elements, you simply use the tag name as the selector.
Example:
p {
color: blue;
}
This will make the text color of all <p> elements blue.
Another example:
h1 {
font-size: 32px;
}
This applies the style to all <h1> headings on the page.
The element selector is simple and useful when you want the same style applied to all elements of a certain type.
Class Selector
The class selector is used when you want to style specific elements instead of all elements of the same type.
In HTML, we add a class using the class attribute, and in CSS we select it using a dot . before the class name.
Example HTML:
<p class="highlight">This paragraph is highlighted.</p>
<p>This paragraph is normal.</p>
CSS:
.highlight {
color: red;
}
Only the paragraph with the class highlight will become red.
A class can also be used on multiple elements.
Example:
<p class="highlight">Paragraph</p>
<h1 class="highlight">Heading</h1>
Both elements will get the same style.
ID Selector
The ID selector is used to style a single unique element on a page. An ID should normally be used only once in a webpage.
In HTML, an ID is added using the id attribute, and in CSS we select it using # before the ID name.
Example HTML:
<h1 id="mainTitle">Welcome</h1>
CSS:
#mainTitle {
color: green;
}
This style will apply only to the element that has the ID mainTitle.
Since IDs are meant to be unique, they are usually used for important sections or specific elements on the page.
Group Selectors
Sometimes you want to apply the same style to multiple elements. Instead of writing separate CSS rules for each element, group selectors allow you to target multiple elements together.
To group selectors, we separate them using commas.
Example:
h1, p, div {
color: blue;
}
This style will apply to all <h1>, <p>, and <div> elements.
Group selectors help reduce repeated code and keep CSS cleaner.
Descendant Selectors
The descendant selector is used to style elements that are inside another element.
It works by writing the parent selector first, followed by the child element selector, separated by a space.
Example HTML:
<div>
<p>This paragraph is inside div.</p>
</div>
<p>This paragraph is outside div.</p>
CSS:
div p {
color: blue;
}
Here, only the paragraph inside the div will become blue. The paragraph outside the div will not change.
This selector is useful when you want to style elements only within a certain section.
Basic Selector Priority (Very High Level)
Sometimes multiple selectors try to style the same element. In such cases, CSS decides which style should be applied based on selector priority.
In simple terms, ID selectors have higher priority than class selectors, and class selectors have higher priority than element selectors.
Example:
p {
color: blue;
}
.text {
color: red;
}
#title {
color: green;
}
If an element has all these styles applied, the ID selector style will win, then class, and then element style.
So the simple priority order is:
ID > Class > Element
Understanding this helps avoid confusion when styles don’t behave as expected.
CSS selectors are the foundation of styling webpages because they allow us to choose exactly which elements should receive styles. By understanding element, class, ID, group, and descendant selectors, you can control how your webpage looks in a much better way.
The best way to learn selectors is by practicing and experimenting with styles on your own HTML pages. Try using different selectors and inspect elements in the browser to see how styles are applied.
If you found this guide helpful, feel free to share your thoughts or suggestions in the comments. Let me know what topic you would like to learn next.
#chaicode #css #cssselectors #selectors #webdev #developer #frontend