• <tfoot id='Ge498'></tfoot>
    <i id='Ge498'><tr id='Ge498'><dt id='Ge498'><q id='Ge498'><span id='Ge498'><b id='Ge498'><form id='Ge498'><ins id='Ge498'></ins><ul id='Ge498'></ul><sub id='Ge498'></sub></form><legend id='Ge498'></legend><bdo id='Ge498'><pre id='Ge498'><center id='Ge498'></center></pre></bdo></b><th id='Ge498'></th></span></q></dt></tr></i><div id='Ge498'><tfoot id='Ge498'></tfoot><dl id='Ge498'><fieldset id='Ge498'></fieldset></dl></div>
  • <small id='Ge498'></small><noframes id='Ge498'>

      <bdo id='Ge498'></bdo><ul id='Ge498'></ul>
        <legend id='Ge498'><style id='Ge498'><dir id='Ge498'><q id='Ge498'></q></dir></style></legend>

        是否可以在悬停时仅更改 rgba 背景颜色的 alpha?

        时间:2023-11-01
              <bdo id='F5cLE'></bdo><ul id='F5cLE'></ul>
              <legend id='F5cLE'><style id='F5cLE'><dir id='F5cLE'><q id='F5cLE'></q></dir></style></legend>

                <small id='F5cLE'></small><noframes id='F5cLE'>

                  <tbody id='F5cLE'></tbody>
                • <i id='F5cLE'><tr id='F5cLE'><dt id='F5cLE'><q id='F5cLE'><span id='F5cLE'><b id='F5cLE'><form id='F5cLE'><ins id='F5cLE'></ins><ul id='F5cLE'></ul><sub id='F5cLE'></sub></form><legend id='F5cLE'></legend><bdo id='F5cLE'><pre id='F5cLE'><center id='F5cLE'></center></pre></bdo></b><th id='F5cLE'></th></span></q></dt></tr></i><div id='F5cLE'><tfoot id='F5cLE'></tfoot><dl id='F5cLE'><fieldset id='F5cLE'></fieldset></dl></div>

                • <tfoot id='F5cLE'></tfoot>

                  本文介绍了是否可以在悬停时仅更改 rgba 背景颜色的 alpha?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一组 <a> 标签,它们的 rgba 背景颜色不同,但 alpha 相同.是否可以编写一个只改变 rgba 属性的不透明度的 CSS 样式?

                  I have a set of <a> tags with differing rgba background colours but the same alpha. Is it possible to write a single css style that will change only the opacity of the rgba attribute?

                  代码的简单示例:

                   <a href="#"><img src="" /><div class="brown">Link 1</div></a>
                   <a href="#"><img src="" /><div class="green">Link 2</div></a> 
                  

                  还有样式

                  a {display: block; position: relative}
                  .brown {position: absolute; bottom: 0; background-color: rgba(118,76,41,.8);}
                  .green {position: absolute; bottom: 0; background-color: rgba(51,91,11,.8);}
                  

                  我想做的是编写一个样式,当 <a> 悬停在上面时会改变不透明度,但保持颜色不变.

                  What I would like to do is write a single style that would change the opacity when the <a> is hovered over, yet keep the colour unchanged.

                  类似

                  a:hover .green, a:hover .brown {background-color: rgba(inherit,inherit,inherit,1);}
                  

                  推荐答案

                  现在可以使用自定义属性:

                  This is now possible with custom properties:

                  .brown { --rgb: 118, 76, 41; }
                  .green { --rgb: 51, 91, 11; }
                  
                  a { display: block; position: relative; }
                  div { position: absolute; bottom: 0; background-color: rgba(var(--rgb), 0.8); }
                  a:hover div { background-color: rgba(var(--rgb), 1); }
                  

                  要了解其工作原理,请参阅 如何将不透明度应用于 CSS 颜色变量?

                  To understand how this works, see How do I apply opacity to a CSS color variable?

                  如果自定义属性不是一个选项,请参阅下面的原始答案.

                  If custom properties are not an option, see the original answer below.

                  很遗憾,不,您必须为每个单独的类再次指定红色、绿色和蓝色值:

                  Unfortunately, no, you'll have to specify the red, green and blue values again for each individual class:

                  a { display: block; position: relative; }
                  
                  .brown { position: absolute; bottom: 0; background-color: rgba(118, 76, 41, 0.8); }
                  a:hover .brown { background-color: rgba(118, 76, 41, 1); }
                  
                  .green { position: absolute; bottom: 0; background-color: rgba(51, 91, 11, 0.8); }
                  a:hover .green { background-color: rgba(51, 91, 11, 1); }
                  

                  您只能单独使用 inherit 关键字作为属性的值,即使这样使用 inherit 在这里也不合适.

                  You can only use the inherit keyword alone as a value for the property, and even then the use of inherit isn't appropriate here.

                  这篇关于是否可以在悬停时仅更改 rgba 背景颜色的 alpha?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 :hover 修改另一个类的css? 下一篇:悬停在子级上而对父级没有悬停效果

                  相关文章

                  <i id='wggRL'><tr id='wggRL'><dt id='wggRL'><q id='wggRL'><span id='wggRL'><b id='wggRL'><form id='wggRL'><ins id='wggRL'></ins><ul id='wggRL'></ul><sub id='wggRL'></sub></form><legend id='wggRL'></legend><bdo id='wggRL'><pre id='wggRL'><center id='wggRL'></center></pre></bdo></b><th id='wggRL'></th></span></q></dt></tr></i><div id='wggRL'><tfoot id='wggRL'></tfoot><dl id='wggRL'><fieldset id='wggRL'></fieldset></dl></div>

                  <small id='wggRL'></small><noframes id='wggRL'>

                        <bdo id='wggRL'></bdo><ul id='wggRL'></ul>
                      <tfoot id='wggRL'></tfoot>
                    1. <legend id='wggRL'><style id='wggRL'><dir id='wggRL'><q id='wggRL'></q></dir></style></legend>