Skip to main content

The CSS Universal Selector

The universal selector (*) selects all HTML elements on the page.

Example

This rule affects every element on the page.

Prompt
A web page using the CSS universal selector (*) in a <style> block. Use * to make every element on the page center-aligned with blue text. Include a heading and a couple of paragraphs.
Output: complete HTML page

The CSS Grouping Selector

The grouping selector selects all the HTML elements that share the same style.

When several selectors have the same declarations, grouping them keeps the code shorter.

To group selectors, separate each one with a comma.

Example

Here h1, h2, and p are grouped into one rule.

Prompt
A web page using a grouped CSS selector in a <style> block. Group h1, h2, and p into a single rule (h1, h2, p) that makes them center-aligned with red text. Include one of each element so the shared style is visible.
Output: complete HTML page

Before grouping

The same style written out separately for h1, h2, and p. Grouping them removes this repetition.

styles.css
h1 {
text-align: center;
color: red;
}

h2 {
text-align: center;
color: red;
}

p {
text-align: center;
color: red;
}

All CSS Simple Selectors

A quick summary of the simple selectors:

  • #id — for example #firstname selects the element with id="firstname".
  • .class — for example .intro selects all elements with the class intro.
  • * — selects all elements.
  • element — for example p selects all <p> elements.
  • element,element,.. — for example div, p selects all <div> elements and all <p> elements.