CSSGrouping Selectors

Grouping Selectors

Grouping selectors (comma-separated) apply the same styles to multiple selectors, reducing code duplication. It's useful when unrelated elements need the same styling.

Grouping Syntax

CSS
/* Grouping selectors with comma */
h1, h2, h3 {
  font-weight: bold;
  color: navy;
}

/* Same as */
h1 {
  font-weight: bold;
  color: navy;
}

h2 {
  font-weight: bold;
  color: navy;
}

h3 {
  font-weight: bold;
  color: navy;
}

/* Multiple types together */
p, li, span {
  line-height: 1.6;
}
Practical Examples

CSS
/* Reset margins on all heading types */
h1, h2, h3, h4, h5, h6 {
  margin: 0;
  margin-top: 20px;
}

/* Style all text inputs */
input[type="text"],
input[type="email"],
input[type="password"],
textarea {
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

/* Reset list styling */
ul, ol {
  list-style: none;
  padding: 0;
}

/* Interactive elements focus state */
a:focus,
button:focus,
input:focus {
  outline: 2px solid blue;
}
Note
Grouping selectors with commas applies styles to multiple selectors at once. Useful for avoiding repetition. Each selector is independent.
Next
Important declaration: [!important](/css/important).