CSS Selectors
Table of contents
In CSS, selectors are used to target the HTML elements on our web pages that we want to style. There are a wide variety of CSS selectors available, allowing for fine-grained precision when selecting elements to style.
What is a selector?
You have met selectors already. A CSS selector is the first part of a CSS Rule. It is a pattern of elements and other terms that tell the browser which HTML elements should be selected to have the CSS property values inside the rule applied to them. The element or elements which are selected by the selector are referred to as the subject of the selector.
Some code with the h1 highlighted. In earlier articles, you met some different selectors and learned that there are selectors that target the document in different ways — for example by selecting an element such as h1, or a class such as .special.
In CSS, selectors are defined in the CSS Selectors specification; like any other part of CSS, they need to have support in browsers for them to work. The majority of selectors that you will come across are defined in the Level 3 Selectors specification, which is a mature specification, therefore you will find excellent browser support for these selectors.
Subscribe to my newsletter
Read articles from Nachiket Keripale directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by