Is it possible to change only the alpha of a rgba background colour on hover?

后端 未结 14 2287
离开以前
离开以前 2020-11-29 07:04
14条回答
  •  我在风中等你
    2020-11-29 07:39

    You could do various things to avoid having to hard code the numbers if you want to. Some of these methods only work if you use a plain white background as they're really adding white on top rather than reducing opacity. The first one should work fine for everything provided:

    • you aren't already using the psuedo-element for something; and
    • you can set position to relative or absolute on the
      tag

    Option 1: ::before psuedo-element:

    .before_method{
      position:relative;
    }
    .before_method:before{
      display:block;
      content:" ";
      position:absolute;
      z-index:-1;
      background:rgb(18, 176, 41);
      top:0;
      left:0;
      right:0;
      bottom:0;
      opacity:0.5;
    }
    .before_method:hover:before{
      opacity:1;
    }
    

    Option 2: white gif overlay:

    .image_method{
      background-color: rgb(118, 76, 41);
      background-image: url(https://upload.wikimedia.org/wikipedia/commons/f/fc/Translucent_50_percent_white.png)
    }
    .image_method:hover{
      background-image:none;
    }
    

    Option 3: box-shadow method:

    A variation of the gif method, but may have performance issues.

    .shadow_method{
      background-color: rgb(18, 176, 41);
      box-shadow:inset 0 0 0 99999px rgba(255,255,255,0.2);
    }
    .shadow_method:hover{
      box-shadow:none;
    }
    

    CodePen examples: http://codepen.io/chrisboon27/pen/ACdka

提交回复
热议问题