Basic Selectors and the Cascade

W3C CSS Snapshot 2026

The browser first finds rules that match an element. It then chooses the winning value when rules set the same property. Finally, some properties can take a value from a parent.

On this page

Select elements by tag, class, or ID

A selector is a pattern for matching elements. p matches every paragraph. A dot followed by a class name matches elements with that class. A hash followed by an ID matches the element with that id. Use classes for styles you plan to reuse.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Select elements by tag, class, or ID</title>
  <style>
    p { color: teal; }
    .notice { font-weight: bold; }
    #catalog-title { color: purple; }
  </style>
</head>
<body>
<h1 id="catalog-title">Product catalog</h1>
<p class="notice">Your order is ready.</p>
<p>Your package is on the way.</p>
</body>
</html>

What to expect Both paragraphs are teal, the first is bold, and the heading is purple.

Select an element inside another element

A space between selectors means the element on the right must be somewhere inside an element matching the selector on the left. A greater-than sign narrows that relationship to a direct child.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Select an element inside another element</title>
  <style>
    .card p { color: blue; }
  </style>
</head>
<body>
<div class="card"><p>Your order is ready.</p></div>
<p>Your package is on the way.</p>
</body>
</html>

What to expect Only “Your order is ready.” becomes blue because it is the paragraph inside the card.

How does the cascade choose?

The cascade is the procedure for resolving competing declarations. In this example, the two rules have the same selector and set the same property. Other priorities are equal, so the rule written later wins. The border from the first rule still applies because the second rule does not set border.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>How does the cascade choose?</title>
  <style>
    .notice { color: teal; border: 1px solid; }
    .notice { color: purple; }
  </style>
</head>
<body>
<p class="notice">Your order is ready.</p>
<p>Your package is on the way.</p>
</body>
</html>

What to expect The first paragraph is purple and still has a border. Reverse the two rules to make it teal.

Which selector has greater specificity?

Specificity compares matching selectors after higher-priority cascade rules tie. An ID selector is stronger than a class selector, and a class selector is stronger than a type selector such as p. Source order breaks a tie when their priorities and specificity match.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Which selector has greater specificity?</title>
  <style>
    .notice { color: purple; }
    p { color: teal; }
  </style>
</head>
<body>
<p class="notice">Your order is ready.</p>
<p>Your package is on the way.</p>
</body>
</html>

What to expect The first paragraph is purple even though the p rule comes later. The second paragraph is teal.

How does inheritance work?

Inheritance means an element can get a computed property value from its parent. Properties such as color normally inherit. A property like border does not normally inherit; the parent and child each have a separate box.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>How does inheritance work?</title>
  <style>
    .orders { color: teal; border: 2px solid; }
    .notice { color: purple; }
  </style>
</head>
<body>
<div class="orders">
<p class="notice">Your order is ready.</p>
<p>Your package is on the way.</p>
</div>
</body>
</html>

What to expect The container has one border. “Your order is ready” is purple, and “Your package is on the way” inherits teal.

Related lessons

Official references