15
kiki
3y

css quick maffs

so, you want to make a css gradient from a certain color into transparent. The logical way would be doing it like this:

linear-gradient(#112233, transparent)

however, this will cause a blurred black stripe to appear in safari. This is due to safari-specific algorithms (that also make it the quickest browser, especially on arm-powered macs).

stackoverflow and other boubas will suggest doing this:

linear-gradient(#112233, rgba(255, 255, 255, 0))

this is better, but instead of a black stripe, a half-transparent white stripe will appear.

To finally make this gradient render consistently across different browsers, do this:

linear-gradient(#112233, #11223300)

Now, you're only changing alpha. See, CSS is a declarative language, so you should be telling it EXACTLY what you want to achieve. You don't want to change one color into another (in that case, "#112233" into "transparent", yes, they are distinct colors that are totally different. CSS doesn't treat "transparent" in some special way like we do) but to only change the alpha channel of #112233.

Feel free to use rgba notation if you want to support older browsers:

linear-gradient(rgba(11, 12, 13, 1), rgba(11, 12, 13, 0))

aight bye

Comments
  • 3
    I see, I see! The same story with `border-color` animation: don't just fade from arbitrary color to `transparent` and vice versa, since it's the black 0 alpha.
  • 0
    A program written in a declarative language which is designed specifically for the problem domain doing the wrong thing because of programmer error, almost as if a model perfectly suited to the problem domain still allowed the programmer to specify any behavior including those subtly different from the desired one.

    Bickering aside, I thought this is common knowledge. I like to play around with the color channels eg. to have the opaque side fade into the colour of the picture or background before it fades into transparent. My clients love it and it feels like the fade belongs to the picture rather than being an overlain foil. It can also be used to align the overlay to the website's background color, which "sinks" the picture into the website or tilts it depending on the colours used.

    CSS gradients are an art of their own.
Add Comment