Skip to main content

Command Palette

Search for a command to run...

Speaking CSS: Learning the Language of Element Selection

How to hit the right element every time without breaking your layout.

Updated
4 min read
Speaking CSS: Learning the Language of Element Selection
S
Trying to transition my career to explore new things, new tech

Imagine trying to find a specific house in a large city. You might look for the neighbourhood, a particular street name, or the house number. CSS Selectors works in the same way. They allow you to choose specific HTML elements on your webpage and apply styles to them. Without selectors, you couldn't style anything, it's the foundation of CSS.
Without selectors, CSS is just a list of rules with no destination. Mastering them is the difference between a messy stylesheet and a professional, scalable codebase.

Why CSS Selectors Are Needed?

We can relate the selectors to a school that has thousands of students. To organise the students, certain rules are applied to them to maintain decorum of the school.

  • All students must wear the uniform. This is the element selector as it applies to all students

  • Students should wear their house badges. This is class selector.

  • The prefects or captains need to wear their badges. This is the ID selectors.

The HTML act as structure (like the school) and CSS Selectors as the rules (rules of the school) as to how people should act.

The Trinity Of CSS Selectors

Element Selector

The element selector targets all elements of a specific type.

Class Selector

Class selectors target elements that share a specific class attribute. Classes are reusable and can be applied to multiple elements. You use a dot (.) before the class name in CSS.

ID Selector

ID selectors target one unique element on the page. Use a hash (#) before the ID name in CSS.

Class vs ID: When to use which?

Classes (.classname)ID (#idname)
Can be used multiple times on the pageMust be unique on the page
Used for styling group of elementsUsed for styling one-of-a-kind elements
Reusable and flexibleUsed for page anchors and JavaScript
Lower specificity than IDHighest specificity

Group Selectors

If you want different elements to share the exact same styles, instead of repetetion, group them using a comma. Group selectors let you target multiple selectors at once by separating them with commas. It's like making one announcement to multiple groups simultaneously.

The advantage of using group selector is:

  • It follows the DRY principle

  • Makes CSS easier to maintain and update

  • Improves code readability

  • Keeps stylesheet smaller and more efficient

Descendant Selectors

Descendant selectors target elements that are nested inside other elements. They let you be very specific about which elements to style based on their location in the HTML structure.

The advantage of descendant selector is:

  • Create context-specific styles without extra classes

  • Style elements differently based on where they appear

  • Keep your HTML cleaner with fewer class attributes

  • Build more maintainable, scalable stylesheets

CSS Selector Specificity

What happens if a paragraph has a class and an ID, and both have different colors? The browser follows a "Specificity Hierarchy":

  • ID Selector: Highest Priority

  • Class Selector: Medium Priority

  • Element Selector: Lowest priority

Key Takeaways

  • Selectors are how CSS finds elements - Without them, you can't apply any styles

  • Element selectors target all instances of an HTML element (broad)

  • Class selectors target reusable groups of elements (flexible and common)

  • ID selectors target unique, one-of-a-kind elements (specific and powerful)

  • Group selectors let you style multiple selectors efficiently with commas

  • Descendant selectors target elements based on their location in HTML structure

  • Specificity matters - IDs beat classes, classes beat elements

GoalSelector TypeExample
Style every single paragraphElementp { ... }
Style all "primary" buttonsClass.btn-primary { ... }
Style only the main logoID#main-logo { ... }
Style multiple headings at onceGrouph1, h2, h3 { ... }
Style a link only if it's in the footerDescendantfooter a { ... }

Mastering these fundamental selectors is absolutely essential for CSS. They're the building blocks you'll use in every stylesheet you write. As you become comfortable with these basics, you'll be ready to explore more advanced selectors like attribute selectors, pseudo-classes, and combinators.

The beauty of CSS selectors is their flexibility - you can target elements as broadly or as specifically as needed. Start practicing with these core selectors, and you'll develop an intuition for which selector to use in different situations.