This tutorial explains what CSS3 selectors are and how to use them, with concrete examples you can try immediately. You’ll learn the main selector types—from basic element selectors to pseudo-classes and attribute selectors—and see how each one targets specific parts of your HTML.
Formula
The number of unique class combinations you can create from a set of elements is given by the binomial coefficient:
C(n, k) = n! / (k! × (n – k)!)
- n = total number of elements (or classes) you have
- k = number of classes you apply to each element
- ! = factorial (e.g., 5! = 5 × 4 × 3 × 2 × 1)
This is the same formula used for lottery odds or choosing a committee. It tells you how many distinct groups of size k you can pick from n items, ignoring order.
Worked Example
Using the default values from the calculator above (n = 5 elements, k = 2 classes per element):
- Compute 5! = 5 × 4 × 3 × 2 × 1 = 120
- Compute 2! = 2 × 1 = 2
- Compute (5 – 2)! = 3! = 3 × 2 × 1 = 6
- Plug into the formula: 120 / (2 × 6) = 120 / 12 = 10
So with 5 elements and 2 classes each, you can create 10 unique class combinations. The calculator shows the same result: 10.
Why CSS3 Selectors Matter
Selectors are the backbone of CSS. They determine which elements a style rule applies to. In CSS3, selectors became far more powerful than in earlier versions, allowing you to target elements based on attributes, position, state, and even negation—without adding extra classes or IDs to your HTML.
This means cleaner markup, easier maintenance, and more dynamic styling. For example, instead of adding a class to every other row in a table, you can use :nth-child(even) to style alternating rows automatically.
Basic Selectors
These are the selectors you’ll use most often:
- Element selector – targets all instances of an HTML tag. Example:
p { color: blue; }makes all paragraphs blue. - Class selector – targets elements with a specific class attribute. Example:
.highlight { background: yellow; }. - ID selector – targets one element with a specific id. Example:
#header { height: 80px; }. IDs must be unique on a page. - Universal selector –
*matches every element. Use sparingly as it can be slow.
You can combine them: div.highlight selects only <div> elements with the class highlight.
Attribute Selectors
CSS3 introduced several new attribute selectors that match parts of an attribute value:
[attr]– element has the attribute. Example:input[required].[attr="value"]– exact match. Example:a[target="_blank"].[attr^="value"]– starts with. Example:a[href^="https"]selects secure links.[attr$="value"]– ends with. Example:img[src$=".png"].[attr*="value"]– contains anywhere. Example:p[class*="note"].
These are case-sensitive by default; add i at the end (e.g., [attr$=".PNG" i]) for case-insensitive matching.
Pseudo-Classes and Pseudo-Elements
Pseudo-classes target elements in a specific state, while pseudo-elements target parts of an element.
Common pseudo-classes:
:hover– when the mouse is over the element.:focus– when the element has keyboard focus (e.g., an input).:nth-child(n)– the nth child of its parent. Example:li:nth-child(odd).:not(selector)– elements that do NOT match the selector. Example:p:not(.intro).:checked– for selected checkboxes or radio buttons.
Common pseudo-elements:
::before– inserts content before the element’s content.::after– inserts content after the element’s content.::first-line– styles the first line of a block.::selection– styles the portion the user selects.
Note the double colon for pseudo-elements in CSS3 to distinguish them from pseudo-classes, though older browsers accept single colon for some.
Combinators
Combinators define the relationship between two selectors:
- Descendant (space) –
div pselects all<p>inside a<div>, at any depth. - Child (>) –
ul > liselects only direct<li>children of<ul>. - Adjacent sibling (+) –
h2 + pselects the first<p>immediately after an<h2>. - General sibling (~) –
h2 ~ pselects all<p>that follow an<h2>.
These let you style based on document structure without adding classes.
Specificity and Cascade
When multiple rules match the same element, specificity decides which wins. Specificity is calculated as:
- Inline styles (highest)
- ID selectors
- Class, attribute, and pseudo-class selectors
- Element and pseudo-element selectors
For example, #nav a (ID + element) beats .nav a (class + element). If specificity is equal, the last rule in the stylesheet wins. Understanding this prevents “why isn’t my style applying?” headaches.
Common Mistakes
- Using ID selectors too often – they are hard to override and not reusable. Prefer classes.
- Overly specific selectors –
div ul li ais fragile; a small HTML change breaks it. - Forgetting the difference between
+and~–+targets only the immediate next sibling,~targets all following siblings. - Using
:nth-childwhen you mean:nth-of-type–:nth-childcounts all siblings, while:nth-of-typecounts only siblings of the same tag.
Frequently Asked Questions
What is the difference between a pseudo-class and a pseudo-element?
A pseudo-class targets an element in a particular state (like :hover or :focus), while a pseudo-element targets a part of the element (like ::before or ::first-letter). In CSS3, pseudo-elements use double colons (::) to distinguish them.
Can I use CSS3 selectors in all browsers?
Most modern browsers support all CSS3 selectors. Older versions of Internet Explorer (before IE9) had partial support, especially for attribute selectors and some pseudo-classes. Use progressive enhancement—apply basic styling first, then add advanced selectors that older browsers ignore.
What does the :not() selector do?
The :not() pseudo-class excludes elements that match a given selector. For example, p:not(.intro) selects all paragraphs except those with class intro. In CSS3 it accepts only a simple selector (no combinators), but CSS4 allows more complex arguments.
How do I select the first child of a parent?
Use :first-child to select the first child element regardless of type, or :first-of-type to select the first element of its specific tag. For example, p:first-of-type selects the first paragraph inside its parent, even if there are other elements before it.
Why is my :hover style not working?
Common reasons include: the selector is too specific and being overridden, the element is not visible (e.g., opacity or visibility), or the hover state is applied to a parent that doesn’t cover the area. Check the specificity and ensure the element actually receives the hover event.
What is the difference between nth-child and nth-of-type?
:nth-child counts all siblings regardless of type, so p:nth-child(2) selects a paragraph only if it is the second child overall. :nth-of-type(2) selects the second paragraph among its siblings, ignoring other element types. This distinction is crucial when mixing elements.
Related
Formula used
in t | Number of elements | - | 5 | 1 | 1000 in c | Classes per element | - | 2 | 1 | 10 out comb | Possible class combinations | - | ncr(t, c) | 0
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Cypress Studio AI Selectors & Sensitive Data
- Enshrouded Palm Wood Logs: How to Get Them
- Feet and Inches to cm Converter: How to Calculate167.64 cm
- Grounded 2: Diving Bell Bed in Into the Abyss
- CPM Calculation for Budgeting5.00 $
- 1 Ton to kW Calculator: Convert Tons to Kilowatts3.5169 kW