I want to make a hover effect over a p-tag, when hovered the background should get darker. I used filter:brightness(85%) but this made also the text darker. Here is my css.
:root {
--my-color:193 55% 40%
}
p {
background-color:hsl(var(--my-color));
}
/* This rule is not working */
p:hover {
background-color:hsl(calc(var(--my-color)[2] - 5%)));
}
Is there a possibility to subtract 5% only of the last element of my css variable ? Or do i have to split my variable into hue, saturation and lightness and then use calc on lightness ?
2
Answers
Yes, you can achieve this by breaking down your HSL variable into its individual components and then using calc for the hover effect. In your case, since you want to reduce the lightness by 5%, you would need to split the HSL value into its components.
Here’s how you can do it:
This way, you’re only modifying the –new-lightness variable on hover, keeping the other HSL components unchanged.
Alternatively, you could calculate the new lightness directly in the calc function within the hover rule, like this:
Both approaches achieve the same result, so you can choose the one that fits your preference or coding style.
Define the colour as three separate CSS variables (
h
,s
,l
) then change the local value ofl
on hover: