- CSS selectors
- Basic selectors
- Grouping selectors
- Combinators
- Pseudo-classes and pseudo-elements
- Structure of a selector
- Specifications
- See also
- CSS Selectors
- CSS Selectors
- The CSS element Selector
- Example
- The CSS id Selector
- Example
- The CSS class Selector
- Example
- Example
- Example
- The CSS Universal Selector
- Example
- The CSS Grouping Selector
- Example
- All CSS Simple Selectors
- How Do You Group Selectors In CSS
- Importance Of Grouping Selectors
- Example Of Grouping Selectors
- Typical Mistakes Of Grouping Selectors
CSS selectors
CSS selectors define the pattern to select elements to which a set of CSS rules are then applied.
CSS selectors can be grouped into the following categories based on the type of elements they can select.
Basic selectors
Selects all elements. Optionally, it may be restricted to a specific namespace or to all namespaces.
Syntax: * ns|* *|*
Example: * will match all the elements of the document.
Selects all elements that have the given node name.
Syntax: elementname
Example: input will match any element.
Selects all elements that have the given class attribute.
Syntax: .classname
Example: .index will match any element that has class=»index» .
Selects an element based on the value of its id attribute. There should be only one element with a given ID in a document.
Syntax: #idname
Example: #toc will match the element that has id=»toc» .
Selects all elements that have the given attribute.
Syntax: [attr] [attr=value] [attr~=value] [attr|=value] [attr^=value] [attr$=value] [attr*=value]
Example: [autoplay] will match all elements that have the autoplay attribute set (to any value).
Grouping selectors
The , selector is a grouping method that selects all the matching nodes.
Syntax: A, B
Example: div, span will match both and elements.
Combinators
The » » (space) combinator selects nodes that are descendants of the first element.
Syntax: A B
Example: div span will match all elements that are inside a element.
The > combinator selects nodes that are direct children of the first element.
Syntax: A > B
The ~ combinator selects siblings. This means that the second element follows the first (though not necessarily immediately), and both share the same parent.
Syntax: A ~ B
Example: p ~ span will match all elements that follow a , immediately or not.
The + combinator matches the second element only if it immediately follows the first element.
Syntax: A + B
Example: h2 + p will match the first element that immediately follows an h2 element.
The || combinator selects nodes which belong to a column.
Syntax: A || B
Example: col || td will match all elements that belong to the scope of the .
Pseudo-classes and pseudo-elements
The : pseudo allow the selection of elements based on state information that is not contained in the document tree.
Example: a:visited will match all elements that have been visited by the user.
The :: pseudo represent entities that are not included in HTML.
Example: p::first-line will match the first line of all elements.
Structure of a selector
The term ‘selector’ can refer to one of the following:
A selector with a single component, such as a single id selector or type selector, that’s not used in combination with or contains any other selector component or combinator. A given element is said to match a simple selector when that simple selector accurately describes the element. All basic selectors, attributes, and single pseudo-classes and pseudo-elements are simple selectors.
A sequence of simple selectors that are not separated by a combinator. A compound selector represents a set of simultaneous conditions on a single element. A given element is said to match a compound selector when the element matches all the simple selectors in the compound selector.
In a compound selector, the type selector or a universal selector in a compound selector must come first in the sequence of selectors. Only one type selector or universal selector is allowed in the sequence. Since whitespace represents the descendant combinator, no whitespace is allowed between the simple selectors in a compound selector.
Example: a#selected
A sequence of one or more simple and/or compound selectors that are separated by combinators. A complex selector represents a set of simultaneous conditions on a set of elements. These conditions apply in the context of relationships described by the combinators. A given element is said to match a complex selector when the element matches compound selectors and the combinators between the compound selectors.
Examples: a#selected > .icon <. >, .box h2 + p <. >, a .icon
A selector representing an element relative to one or more anchor elements preceded by a combinator. Relative selectors that don’t begin with an explicit combinator have an implied descendant combinator.
Examples: + div#topic > #reference <. >, > .icon <. >, dt:has(+ img) ~ dd
A comma-separated list of simple, compound, or complex selectors. If the constituent selector type of a selector list is important but unspecified, it is called a complex selector list. A given element is said to match a selector list when the element matches any (at least one) of the selectors in that selector list. Read more about when a selector list is deemed invalid and how to construct a forgiving selector list.
Example: #main, article.heading
Specifications
See the pseudo-class and pseudo-element specification tables for details on those.
See also
CSS Selectors
A CSS selector selects the HTML element(s) you want to style.
CSS Selectors
CSS selectors are used to «find» (or select) the HTML elements you want to style.
We can divide CSS selectors into five categories:
- Simple selectors (select elements based on name, id, class)
- Combinator selectors (select elements based on a specific relationship between them)
- Pseudo-class selectors (select elements based on a certain state)
- Pseudo-elements selectors (select and style a part of an element)
- Attribute selectors (select elements based on an attribute or attribute value)
This page will explain the most basic CSS selectors.
The CSS element Selector
The element selector selects HTML elements based on the element name.
Example
Here, all
elements on the page will be center-aligned, with a red text color:
The CSS id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element is unique within a page, so the id selector is used to select one unique element!
To select an element with a specific id, write a hash (#) character, followed by the id of the element.
Example
The CSS rule below will be applied to the HTML element with
Note: An id name cannot start with a number!
The CSS class Selector
The class selector selects HTML elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed by the class name.
Example
In this example all HTML elements with will be red and center-aligned:
You can also specify that only specific HTML elements should be affected by a class.
Example
In this example only
elements with will be red and center-aligned:
HTML elements can also refer to more than one class.
Example
In this example the
element will be styled according to and to
This paragraph refers to two classes.
Note: A class name cannot start with a number!
The CSS Universal Selector
The universal selector (*) selects all HTML elements on the page.
Example
The CSS rule below will affect every HTML element on the page:
The CSS Grouping Selector
The grouping selector selects all the HTML elements with the same style definitions.
Look at the following CSS code (the h1, h2, and p elements have the same style definitions):
h2 text-align: center;
color: red;
>
p text-align: center;
color: red;
>
It will be better to group the selectors, to minimize the code.
To group selectors, separate each selector with a comma.
Example
In this example we have grouped the selectors from the code above:
All CSS Simple Selectors
Selector | Example | Example description |
---|---|---|
#id | #firstname | Selects the element with > |
.class | .intro | Selects all elements with > |
element.class | p.intro | Selects only elements with > |
* | * | Selects all elements |
element | p | Selects all elements |
element,element. | div, p | Selects all elements and all elements |
How Do You Group Selectors In CSS
Learn how to efficiently apply the same style rules to multiple elements by grouping selectors in CSS.
Important disclosure: we’re proud affiliates of some tools mentioned in this guide. If you click an affiliate link and subsequently make a purchase, we will earn a small commission at no additional cost to you (you pay nothing extra). For more information, read our affiliate disclosure.
Importance Of Grouping Selectors
Grouping selectors in CSS is essential for several reasons, as it improves the efficiency, maintainability, and readability of your code. Here are some key points highlighting the importance of grouping selectors:
- Code Reusability: By grouping selectors, you can apply the same style rules to multiple elements without repeating the rules for each element. This reduces redundancy in your code and promotes reusability.
- Improved Maintainability: When you group selectors, you can make changes to the shared styles in one place, rather than having to update multiple style declarations. This makes your code easier to maintain and update.
- Increased Readability: Grouping selectors that share the same style rules helps keep your CSS code organized and more readable. This makes it easier for you and other developers to understand the structure and purpose of the styles.
- Reduced File Size: Grouping selectors can help reduce the overall size of your CSS file, as it eliminates the need to repeat the same style rules for multiple selectors. This can lead to faster page load times and better performance.
- Consistent Styling: By grouping selectors, you can ensure consistent styling across multiple elements. This can help create a cohesive look and feel throughout your website or application.
Example Of Grouping Selectors
In CSS, you can group selectors to apply the same styles to multiple elements. To do this, simply separate the selectors with a comma. This is useful when you want to apply the same set of style rules to different elements without repeating the rules for each selector. Here’s an example:
/* This will apply the same styles to h1, h2, and p elements */ h1, h2, p
In this example, the color, font-family, and font-weight properties are applied to the h1, h2, and p elements.
Here’s another example of grouping selectors in CSS:
/* This will apply the same styles to article, section, and aside elements */ article, section, aside
In this example, the background-color, border, border-radius, padding, and margin-bottom properties are applied to the article, section, and aside elements. The comma separates the different selectors, allowing you to group them together and apply the same styles to all of them.
Typical Mistakes Of Grouping Selectors
Below are some typical mistakes made while grouping selectors in CSS and their solutions:
- Missing comma between selectors: A common mistake is to forget the comma between the selectors, which can lead to unintended results.
/* Incorrect: Missing comma between h1 and h2 selectors */ h1 h2, p < color: red; >/* Correct: Comma between h1 and h2 selectors */ h1, h2, p
2. Incorrect use of whitespace: Using unnecessary whitespace or not using whitespace correctly can cause issues in your CSS code.
/* Incorrect: Extra whitespace between selectors */ h1 , h2 , p < color: red; >/* Correct: Proper use of whitespace */ h1, h2, p
3. Misunderstanding the specificity: When using grouped selectors, the specificity of each selector is not combined. The styles are applied individually according to each selector’s specificity, which can lead to confusion.
Solution: Be aware of the specificity of each selector in a grouped declaration, and consider using more specific selectors if necessary.
4. Overusing grouped selectors: Grouping selectors can make your CSS more concise, but overusing this technique can make your code harder to maintain and understand.
Solution: Only use grouped selectors when the same styles need to be applied to multiple elements, and separate the styles that are unique to each element.
5. Inconsistently applying styles: Sometimes, you may forget to apply styles consistently when using grouped selectors. This can lead to unintended variations in styling.
Solution: Review your grouped selectors carefully to ensure that all intended styles are applied consistently to the targeted elements.
Throughout this tutorial, we discussed the importance of grouping selectors, provided examples of how to group selectors, and highlighted some common mistakes and their solutions.
By understanding and applying the concept of grouping selectors, you can create well-organized, efficient, and maintainable stylesheets that ensure consistent styling across your website or application.