CSS selector order for pseudo-classes and pseudo-elements

To style an element’s pseudo-element when you hover over the element, use .box:hover::before. Here :hover checks the state of .box and ::before is the styling target. Where supported, reversing the order to .box::before:hover checks the pseudo-element’s own state.

Identify the element in the state and the target of the style

A pseudo-class describes a condition, such as :hover or :focus-visible. A pseudo-element such as ::before or ::after represents a part associated with the originating element. Setting content on ::before can create visible content without adding an HTML child node. Use two colons for pseudo-elements in new code. Some older pseudo-elements still accept one colon for compatibility. See the generated content specification.

/* Style .box’s ::before when the pointer is over .box */
.box:hover::before {
  color: blue;
}

/* Style .box’s ::before when the box shows keyboard focus */
.box:focus-visible::before {
  color: blue;
}

Try three ways to color a button and its diamond

Save the example as pseudo-order.html and open it. Each button’s text changes to the same color when you hover over it or move keyboard focus to it with Tab. The decorative diamond uses a different color rule on each button. No other files or JavaScript are needed.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Pseudo-element selection and color inheritance</title>
  <style>
    body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
    * { box-sizing: border-box; }
    h1 { font-size: 24px; }
    .sample { --active-color: #1268bd; display: block; width: 100%; max-width: 420px; min-height: 48px;
      margin: 12px 0; padding: 12px; border: 1px solid #8fa5b8;
      background: #f2f6f9; color: #203348; text-align: left; font: inherit; }
    .sample::before { content: "◆"; margin-inline-end: 8px; color: #b13a35; }
    .sample:hover, .sample:focus-visible { color: var(--active-color); }
    .sample:focus-visible { outline: 3px solid #bf5c00; outline-offset: 2px; }
    .explicit:hover::before, .explicit:focus-visible::before { color: var(--active-color); }
    .inherit::before { color: inherit; }
  </style>
</head>
<body>
<h1>Change the color of a pseudo-element</h1>
<button class="sample parent-only" type="button">Change only the button text color</button>
<button class="sample explicit" type="button">Set the diamond’s color directly</button>
<button class="sample inherit" type="button">Let the diamond inherit the button color</button>
<p>Hover over each button or use Tab to focus it.</p>
</body>
</html>

The first diamond stays red because the rule sets a color directly on ::before. The second diamond turns blue because .explicit:hover::before and its focus counterpart style the diamond based on the button’s state. The third sets color: inherit, so it starts with the normal text color and follows the button when its color changes.

Can a pseudo-class follow a pseudo-element?

The Selectors Level 4 draft defines pseudo-element state rules and sub-pseudo-elements. For example, .box::before:hover describes the hover state of the pseudo-element, and a ::before generated as a list item can have a ::marker. Support depends on the combination. Use .box:hover::before for an effect that responds to the originating element.

For a newer combination, @supports selector(.box::before:hover) checks whether the browser accepts the selector. Keep that rule separate from established selectors. This test does not check actual pointer behavior. In an ordinary comma-separated selector list, one unsupported selector can cause the entire rule to be discarded. See the selector support rules.

Find out why a selector has no effect

First confirm that .box matches the originating element and that the expected state applies. If ::before or ::after is missing, check whether its computed content is none. If the diamond is visible but has the wrong color, look for a color declared directly on the pseudo-element and compare the rules that apply there.

Append .parent-only::before { color: inherit; } to the end of the style element. The first diamond now follows the button’s text color at rest and in the active state. Remove the declaration to make it stay red again. Keep the actual button label in HTML and avoid relying on :hover alone for essential information or actions.