我不想在CSS中从父继承子不透明度。

我有一个div是父div,在第一个div里面有另一个div是子div。

我想在父div中设置不透明度属性,但我不希望子div继承不透明度属性。

我该怎么做呢?


当前回答

以下是对我有用的:

改变了

来自:

opacity: 0.52;
background-color: #7c7c7c;

To:

opacity: 1 !important;
background-color: rgba(124, 124, 124, 0.52) !important;

要将十六进制和不透明度转换为rgba, 使用http://hex2rgba.devoth.com/这样的网站

其他回答

正如其他人在这篇文章和其他类似的文章中提到的,避免这个问题的最好方法是使用RGBA/HSLA或使用透明的PNG。

但是,如果你想要一个荒谬的解决方案,类似于这个线程(这也是我的网站)中另一个答案中的链接,这里有一个全新的脚本,我写了一个自动修复这个问题,称为thatsNotYoChild.js:

http://www.impressivewebs.com/fixing-parent-child-opacity/

基本上,它使用JavaScript从父div中删除所有子元素,然后将子元素重新定位到它们应该在的位置,而不再是该元素的子元素。

对我来说,这应该是最后的手段,但我认为如果有人想这样做的话,写一些这样的东西会很有趣。

我也遇到了同样的问题,在谷歌了一下之后,我找到了一些解决这个问题的方法(3种方法)。 我在这里分享解决方案,你可以尝试其中任何一种。

选项1: 在之前或之后使用伪标记元素作为背景

.parentContainer {
    position: relative;
}
.parentContainer:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #fff;
    opacity: 0.6;
}
.childContent {
    position: relative;
    color: red;
    z-index: 1;
}

选项2: 使用带有alpha值的rgba颜色而不是不透明度。

 <div id="parentContainer" style="background: rgba(255,255,255,0.6);">
    <div id="childContent">
         Content ...
    </div>
 </div>

选项3: 使用背景div,一个元素的绝对位置高于另一个元素。

<div class="parentContainer">
  <div class="childContent">
    Here is the content.
  </div>
  <div class="background"></div>
</div>


.parentContainer {
  position: relative;
}
.childContent {
  position: relative;
  color: White;
  z-index: 5;
}
.background {
  position: absolute;
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100%;
  background-color: Black;
  z-index: 1;
  opacity: .5;
}

其他人试图使一个表(或其他东西)看起来集中在一行使用不透明度。就像@Blowski说的,使用颜色而不是不透明度。看看这个小提琴:http://jsfiddle.net/2en6o43d/

.table:hover > .row:not(:hover)

这个问题没有定义背景是颜色还是图像,但由于@Blowski已经回答了彩色背景,下面有一个针对图像的破解方法:

background: linear-gradient(rgba(0,0,0,.6), rgba(0,0,0,.6)), url('image.jpg');

这样你就可以操纵不透明度的颜色,甚至添加漂亮的渐变效果。

.wrapper { 宽度:630 px; 身高:420 px; 显示:表; 背景:线性渐变( rgba(0, 0, 0, 0。8), rgba(0, 0, 0, 0。8)), url (http://cdn.moviestillsdb.com/sm/35bc3c6a2b791425de6caf8b9391026e/rambo-iii.jpg); } h1 { 显示:表格单元; vertical-align:中间; text-align:中心; 颜色:# fff; } < div class = "包装" > <标题> 5770341 < / h1 >问题 < / div >

在mac上,你可以使用预览编辑器将不透明度应用到你的。png图像上的白色矩形上,然后把它放在你的。css中。1)ImageLogo2)在imagereectanle周围创建一个矩形3)改变背景颜色为白色矩形变成白色4)调整矩形不透明图像