Should you use rgba(0, 0, 0, 0) or rgba(255, 255, 255, 0) for transparency in CSS?

前端 未结 4 810
梦谈多话
梦谈多话 2021-02-11 15:29

Should you use rgba(0, 0, 0, 0) or rgba(255, 255, 255, 0) for transparency in CSS?

What are the pros and cons of each?

4条回答
  •  萌比男神i
    2021-02-11 15:55

    The last parameter to the rgba() function is the "alpha" or "opacity" parameter. If you set it to 0 it will mean "completely transparent", and the first three parameters (the red, green, and blue channels) won't matter because you won't be able to see the color anyway.

    With that in mind, I would choose rgba(0, 0, 0, 0) because:

    1. it's less typing,
    2. it keeps a few extra bytes out of your CSS file, and
    3. you will see an obvious problem if the alpha value changes to something undesirable.

    You could avoid the rgba model altogether and use the transparent keyword instead, which according to w3.org, is equivalent to "transparent black" and should compute to rgba(0, 0, 0, 0). For example:

    h1 {
        background-color: transparent;
    }
    

    This saves you yet another couple bytes while your intentions of using transparency are obvious (in case one is unfamiliar with RGBA).

    As of CSS3, you can use the transparent keyword for any CSS property that accepts a color.

提交回复
热议问题