In order to use Tailwind CSS for styling the input slider's track and thumb, I have to rely on two different pseudo elements (WebKit and Mozilla), which results in code duplication.
::-webkit-slider-runnable-trackfor Chrome, Edge, Opera, Safari::-moz-range-track- for Firefox::-webkit-slider-thumbfor Chrome, Edge, Opera, Safari::-moz-range-thumb- for Firefox
However, since these selectors are not standardized, it may be necessary to apply different styles for each system separately.
It is possible to implement this with arbitrary values, but the code becomes very long and unreadable.
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
<input
type="range"
class="
m-5 appearance-none w-[20em] h-[2em] outline-none overflow-hidden rounded
/* Track for webkit */
[&::-webkit-slider-runnable-track]:bg-slate-300
[&::-webkit-slider-runnable-track]:h-full
[&::-webkit-slider-runnable-track]:rounded
/* Track for moz */
[&::-moz-range-track]:bg-slate-300
[&::-moz-range-track]:h-full
[&::-moz-range-track]:rounded
/* Thumb for webkit */
[&::-webkit-slider-thumb]:h-full
[&::-webkit-slider-thumb]:w-[8px]
[&::-webkit-slider-thumb]:bg-blue-500
[&::-webkit-slider-thumb]:[box-shadow:_-20em_0_0_20em_black]
/* Custom for webkit */
[&::-webkit-slider-thumb]:appearance-none
/* Thumb for moz */
[&::-moz-range-thumb]:h-full
[&::-moz-range-thumb]:w-[8px]
[&::-moz-range-thumb]:bg-blue-500
[&::-moz-range-thumb]:[box-shadow:_-20em_0_0_20em_black]
/* Custom for moz */
[&::-moz-range-thumb]:border-0
[&::-moz-range-thumb]:rounded-none
"
/>
I tried creating a custom variant, but it seems it doesn't work:
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
<style type="text/tailwindcss">
@custom-variant slider-track (&::-webkit-slider-runnable-track, &::-moz-range-track));
</style>
<input
type="range"
class="
m-5 appearance-none w-[20em] h-[2em] outline-none overflow-hidden rounded
/* Track for webkit & moz */
slider-track:bg-slate-300
slider-track:h-full
slider-track:rounded
/* Thumb for webkit */
[&::-webkit-slider-thumb]:h-full
[&::-webkit-slider-thumb]:w-[8px]
[&::-webkit-slider-thumb]:bg-blue-500
[&::-webkit-slider-thumb]:[box-shadow:_-20em_0_0_20em_black]
/* Custom for webkit */
[&::-webkit-slider-thumb]:appearance-none
/* Thumb for moz */
[&::-moz-range-thumb]:h-full
[&::-moz-range-thumb]:w-[8px]
[&::-moz-range-thumb]:bg-blue-500
[&::-moz-range-thumb]:[box-shadow:_-20em_0_0_20em_black]
/* Custom for moz */
[&::-moz-range-thumb]:border-0
[&::-moz-range-thumb]:rounded-none
"
/>
How can I make the variants in class names more readable, and how can I eliminate code duplication this way?