【问题标题】:Opacity !important doesn't work不透明度 !important 不起作用
【发布时间】:2023-04-03 04:30:01
【问题描述】:

这是我的代码:

<tr style="opacity: 0.5;"><td><img style="opacity: 1.0 !important;" src="/img/crap.png"/><div>Some text, etc...</td></tr>

我不想以完全不透明度显示图像,其余的应该只有 50% 的不透明度,我也尝试使用 !important 属性,但它不起作用。我还尝试将这些样式移到 css 文件中的类中。图像始终具有 50% 的不透明度。

我该如何解决这个问题?

【问题讨论】:

标签: html css opacity


【解决方案1】:

如果表格行的不透明度为0.5,则将&lt;img&gt; 的不透明度设置为0.51.0(或100%),如果它是祖先,则为不透明度。

您需要将表格行的不透明度设置为 1.0 才能完成这项工作。

【讨论】:

  • 但我不希望表格行的背景有 50% 的不透明度。
  • 那么&lt;img&gt; 必须在&lt;tr&gt; 之外;否则它将始终(正确地)具有与其祖先相同的不透明度。
  • 嗯,是的,但这不是答案。我知道我必须将表格行不透明度设置为 1.0 才能使这项工作,所以你的解决方案(找不到任何顺便说一句。)没有帮助。
  • 这是一个答案。 &lt;img&gt; 必须在 &lt;tr&gt; 之外。在桌子周围的容器上使用position:absolute;&lt;img&gt;position:relative。将&lt;img&gt; 放入容器中并调整其lefttop 以将其准确放置在您想要的位置。
【解决方案2】:

存在与您的相同问题的问题。

链接在这里:Set div's background transparent but not the borders

策略是使用带有颜色和不透明度的 CSS“背景”属性:

background-color: rgba(0,255,255,0.4)

一篇关于 rgba 的好文章可以在这里找到:CSS RGBA

希望对您有所帮助。 :)

【讨论】:

    【解决方案3】:

    克莱夫的回答很好地解释了这一点。

    this SO answer 中解释了解决此问题的方法

    【讨论】:

      【解决方案4】:

      不透明度与父元素的不透明度有关(很遗憾)。因此,通过在 tr 上设置 0.5 的不透明度,每个子元素的不透明度都将达到 0.5... 除非您使用 rgba:

      tr {
        background: rgb(0, 0, 0) transparent; /* Fallback for web browsers that doesn't support RGBa */
        background: rgba(0, 0, 0, 0.5); /* RGBa with 0.5 opacity */
        filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000); /* For IE 5.5 - 7*/
        -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000)"; /* For IE 8*/
      }
      

      那么上面的所有东西都会有你设置的不透明度。

      【讨论】:

      • 它失败了,因为我想在某些表格行上使用我自己的背景颜色:/
      猜你喜欢
      • 2012-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 2013-01-21
      • 2011-07-28
      • 1970-01-01
      相关资源
      最近更新 更多