Computer >> Computer tutorials >  >> Programming >> CSS

Grouping Selectors in CSS


The CSS grouping selector is used to select multiple elements and style them together. This reduces the code and extra effort to declare common styles for each element. To group selectors, each selector is separated by a space.

Syntax

The syntax for CSS grouping selector is as follows −

element, element {
   /*declarations*/
}

The following examples illustrate CSS grouping selector −

Example

<!DOCTYPE html>
<html>
<head>
<style>
article, p, img {
   display: block;
   margin: auto;
   text-align: center;
   border-bottom: double orange;
}
</style>
</head>
<body>
<article>Demo Text</article>
<p>This is demo text.</p>
<br/>
<img src="https://www.tutorialspoint.com/swing/images/swing.jpg">
</body>
</html>

Output

This gives the following output −

Grouping Selectors in CSS

Example

<!DOCTYPE html>
<html>
<head>
<style>
div::after,p::after{
   content: "Demo text";
   margin: 4px;
   box-shadow: inset 0 0 4px darkorange;
}
</style>
</head>
<body>
<div></div>
<p>This is example text.</p>
</body>
</html>

Output

This gives the following output −

Grouping Selectors in CSS