CSS Selectors 101: Targeting Elements with Precision

Published: (January 30, 2026 at 02:34 PM EST)
1 min read
Source: Dev.to

Source: Dev.to

Introduction

CSS selectors are patterns that allow you to choose which HTML elements you want to style. Without them you cannot apply colors, fonts, or layouts to your page.

Element Selector

Targets all elements of a specific type, like all paragraphs:

p {
  color: blue;
}

All paragraph text will appear in blue.

Class Selector

Use a . in front of the class name.

.highlight {
  color: red;
}
Test

Test

ID Selector

IDs are unique identifiers for a single, specific element. Each ID should appear only once per page.

#header {
  font-size: 30px;
}
## Heading

Group Selectors

Group selectors with commas to apply the same style to multiple elements.

h1, h2, h3 {
  font-family: arial;
}

Descendant Selectors

Target elements that are inside other elements.

article p {
  color: blue;
}

Selector Priority: The Basics

When multiple selectors target the same element, CSS decides which style to apply based on specificity:

ID Selector > Class Selector > Element Selector

What a color?
Back to Blog

Related posts

Read more »

CSS Selectors 101

CSS Selectors – How to Target Elements You've written some HTML. Now you want to make it look good with CSS. But how do you tell CSS which elements to style? T...

Building a Fluid, Minimalist Portfolio

!Cover image for Building a Fluid, Minimalist Portfoliohttps://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F%...

My 2026 Developer Portfolio

Introduction Hi! I'm Ahmed Anter Elsayed, a passionate developer and educator in Python, AI, and web development. Live Portfolio Check out my live portfolio he...