Skip to main content

CSS Selectors

Finding (or selecting) the HTML elements you want to style is what CSS selectors do.

They fall into five groups:

  • Simple selectors pick elements by name, id, or class.
  • Combinator selectors look at the relationship between elements.
  • Pseudo-class selectors depend on a certain state.
  • Pseudo-element selectors style just a part of an element.
  • Attribute selectors check for an attribute or its value.

The most basic CSS selectors are the subject of this page.


The CSS element Selector

A tag name is all the element selector needs to pick HTML elements.

Example

Every <p> element on this page gets red, center-aligned text.

Prompt
A web page using the CSS element selector in a <style> block. Select all <p> elements by name and make them center-aligned with red text. Include a few paragraphs.
Output: complete HTML page

The CSS id Selector

To reach one specific element, the id selector relies on the element's id attribute.

Since an id is unique on a page, only a single element is targeted.

The selector itself is a hash (#) and then the id.

Note: The id written after the hash cannot start with a number.

Example

The element with id="para1" receives this rule.

Prompt
A web page using the CSS id selector in a <style> block. Style only the element with id="para1" using #para1, making it center-aligned with red text. Include one paragraph with that id and a couple of other paragraphs without it, so only the targeted one changes.
Output: complete HTML page

The CSS class Selector

Elements carrying a specific class attribute are picked by the class selector.

The selector itself is a period (.) and then the class name.

A class can also be limited to one element type, and one element is free to use several classes.

Note: The name written after the period cannot start with a number.

All elements with a class

Anything with the class center turns red and center-aligned.

Prompt
A web page using the CSS class selector in a <style> block. Use .center to make all elements with class="center" center-aligned with red text. Apply the class to a heading and a paragraph so it works across different element types.
Output: complete HTML page

Class on one element type

The class center takes effect on <p> elements only, turning them red and center-aligned.

Prompt
A web page using a CSS class selector limited to one element type, in a <style> block. Use p.center so only <p> elements with class="center" are center-aligned with red text. Include a heading with class="center" (which should NOT change) and a paragraph with class="center" (which should change).
Output: complete HTML page

More than one class

Two classes, center and large, style this paragraph at once.

Prompt
A web page showing how one element can use more than one CSS class, in a <style> block. Define a .center class (center-aligned red text) and a .large class (large font size). Add a paragraph with class="center large" so both rules apply to it.
Output: complete HTML page