Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Opacity

Besides transparent colors, CSS has the opacity property. It makes a whole element see-through, together with everything in it: its text, its background and all the elements inside it. Its value is a number from 0 (invisible) to 1 (fully opaque, the default):

h1 {
	opacity: 0.5;
}

How is it different from a color with an alpha? An alpha affects only the one color we write it in. Compare these two examples. The first has a magenta background with an alpha of 0.5 and white text; the second has a magenta background, white text and opacity: 0.5:

background-color: rgba(255, 0, 229, 0.5)

opacity: 0.5

In the first example, only the background is transparent and the text stays bright white. In the second one, the text has faded together with the background.

So when you want only the background (or only the text) to be see-through, use a color with an alpha. When you want to fade a whole element, use opacity. It is often used for effects with :hover, which you know from the lesson on selectors. We will fade the list of breeds a little, and each breed will light up when you move the mouse pointer over it.

Instructions

  1. 1

    Create a rule for the li elements that sets opacity to 0.6.

  2. 2

    Create a rule for li:hover that sets opacity back to 1. Run the code and move the mouse pointer over the breeds.

Back

8/9

  1. 1. Named Colors
  2. 2. Quiz: Named Colors
  3. 3. Background Color
  4. 4. RGB Colors
  5. 5. Hexadecimal Colors
  6. 6. HSL Colors
  7. 7. Transparent Colors
  8. 8. Opacity
  9. 9. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.