Skip to main content

Command Palette

Search for a command to run...

CSS Selectors 101: Targeting Elements with Precision

A simple guide to how CSS knows what to style 🎯

Updated
4 min read
CSS Selectors 101: Targeting Elements with Precision
G
I enjoy blending technology with business to build solutions that create real impact. I’m fascinated by how technology empowers people, businesses, and startups. I believe thoughtful use of technology quietly improves lives, and that belief fuels everything I build and explore 💻.

When we start learning CSS, one of the first questions that naturally comes up is how CSS knows which part of the HTML to style. We write rules, add colors, change sizes, but unless CSS knows where to apply those rules, nothing really happens. This is where CSS selectors come in. Selectors are simply a way to tell CSS, this is the element I want you to style. Without selectors, CSS would have no direction and no purpose.

You can think of selectors as a way of choosing elements from your HTML. Just like in real life, when you want to talk to someone specific, you need to call them out clearly. CSS works in the same way. It needs a clear instruction about which element or group of elements should receive a certain style. Once this idea clicks, CSS becomes much easier to understand.

The most basic type of selector is the element selector. This selector targets HTML elements directly by their tag name. For example, if you want to style all paragraph tags on a page, you simply write a rule for the p element. This tells CSS to apply the same styling to every paragraph present in the document. Element selectors are simple and powerful, but they affect all instances of that element, which is not always what we want.

As our web pages grow, we often need more control. This is where class selectors become useful. A class allows us to label specific elements so that we can style only those elements and ignore the rest. In HTML, we assign a class attribute to an element, and in CSS, we target that class using a dot followed by the class name. This gives us flexibility because the same class can be reused on multiple elements. It is a clean and practical way to apply consistent styling without affecting everything.

Going one step further, we have ID selectors. An ID is meant to be unique and should be used only once on a page. When we assign an ID to an element, we are saying that this element is special and should be treated differently from others. In CSS, an ID selector is written using a hash symbol followed by the ID name. Because IDs are unique, they allow very precise styling. However, beginners should use them carefully and only when uniqueness is truly required.

Sometimes, we want to apply the same style to multiple elements at once, even if they are different types. Group selectors help us do exactly that. By separating multiple selectors with commas, we can write one CSS rule that applies to all of them. This keeps our CSS clean and avoids repetition. It also makes the code easier to read and maintain, especially as projects become larger.

Another important concept is the descendant selector. This selector allows us to target elements that are inside other elements. For example, if you want to style only the paragraphs that are inside a div, you can do that using a descendant selector. This is extremely useful because it helps us apply styles based on structure, not just labels. It reflects how HTML is actually nested and organized.

While working with selectors, it is also important to understand that not all selectors are treated equally by CSS. There is a basic priority system involved. At a very high level, ID selectors have more priority than class selectors, and class selectors have more priority than element selectors. This means that if multiple rules try to style the same element, the one with higher priority will usually win. Understanding this early prevents confusion when styles do not behave as expected.

To really see the impact of selectors, it helps to look at before and after styling. Before applying CSS, HTML appears plain and unstructured. Once selectors are used correctly, the same HTML becomes organized, readable, and visually appealing. Nothing changes in the HTML itself, only the way we select and style elements does.

At the end of the day, CSS selectors are the foundation of CSS. Every layout, every color, every design decision depends on selecting the right elements. Once you are comfortable with selectors, the rest of CSS feels far less intimidating. As a beginner, spending time understanding selectors deeply is one of the best investments you can make in your frontend journey.