Skip to content

Tailwind's arbitrary selectors can style the progress element's browser pseudo-elements

April 26, 2023

Styling the HTML <progress> element means styling its browser-specific pseudo-elements. Tailwind’s arbitrary selectors (a [...] variant with any CSS selector in it) can target them right in the markup:

<progress
  class="[&::-webkit-progress-bar]:bg-slate-300
    [&::-webkit-progress-value]:bg-violet-400
    [&::-moz-progress-bar]:bg-violet-400"
  value="60"
  max="100"
/>

The pseudo-elements:

  • ::-webkit-progress-bar, the track (Chrome/Safari)
  • ::-webkit-progress-value, the filled portion (Chrome/Safari)
  • ::-moz-progress-bar, the filled portion (Firefox)

If you need it more than once, you can register custom variants in a plugin:

plugin(function ({ addVariant }) {
  addVariant("progress-unfilled", ["&::-webkit-progress-bar", "&"]);
  addVariant("progress-filled", [
    "&::-webkit-progress-value",
    "&::-moz-progress-bar",
  ]);
});

Then the markup gets shorter:

<progress
  class="progress-unfilled:bg-slate-300 progress-filled:bg-violet-400"
  value="60"
  max="100"
/>

—

Back to TIL

Next: An xml-stylesheet instruction makes browsers render an Atom feed as HTML

Phil-Bastian Berndt

Phil-Bastian Berndt
Tech Lead at Naymspace