我不想在CSS中从父继承子不透明度。
我有一个div是父div,在第一个div里面有另一个div是子div。
我想在父div中设置不透明度属性,但我不希望子div继承不透明度属性。
我该怎么做呢?
我不想在CSS中从父继承子不透明度。
我有一个div是父div,在第一个div里面有另一个div是子div。
我想在父div中设置不透明度属性,但我不希望子div继承不透明度属性。
我该怎么做呢?
当前回答
在mac上,你可以使用预览编辑器将不透明度应用到你的。png图像上的白色矩形上,然后把它放在你的。css中。1)ImageLogo2)在imagereectanle周围创建一个矩形3)改变背景颜色为白色矩形变成白色4)调整矩形不透明图像
其他回答
不要使用不透明度,使用rgba设置背景颜色,其中“a”是透明度级别。
所以不要:
background-color: rgb(0,0,255); opacity: 0.5;
use
background-color: rgba(0,0,255,0.5);
<!--Background opacity--> <style> .container1 { width: 200px; height: 200px; background: rgba(0, 0, 0, .5); margin-bottom: 50px; } </style> <div class="container1"> <div class="box1">Text</div> </div> <!--Before, after, z-index opacity--> <style> .container2 { width: 200px; height: 200px; position: relative; margin-bottom: 100px; } .container2:after { content: ''; display: block; width: 100%; height: 100%; position: absolute; top: 0; left: 0; background: black; opacity: .5; z-index: 1; } .box2 { position: relative; z-index: 2; } </style> <div class="container2"> <div class="box2">Text</div> </div> <!--Outline opacity--> <style> .container3 { width: 200px; height: 200px; outline: 50px solid rgba(0, 0, 0, .5); margin: 50px; } .box3 { position: relative; left: -16px; } </style> <div class="container3"> <div class="box3">Text</div> </div>
我的答案不是关于静态的父子布局,而是关于动画。
I was doing an svg demo today, and i needed svg to be inside div (because svg is created with parent's div width and height, to animate the path around), and this parent div needed to be invisible during svg path animation (and then this div was supposed to animate opacity from 0 to 1, it's the most important part). And because parent div with opacity: 0 was hiding my svg, i came across this hack with visibility option (child with visibility: visible can be seen inside parent with visibility: hidden):
.main.invisible .test {
visibility: hidden;
}
.main.opacity-zero .test {
opacity: 0;
transition: opacity 0s !important;
}
.test { // parent div
transition: opacity 1s;
}
.test-svg { // child svg
visibility: visible;
}
然后,在js中,你用timeout函数删除。invisible类,添加。opaque -zero类,用。style。top之类的东西触发布局;并删除。opaque - 0类。
var $main = $(".main");
setTimeout(function() {
$main.addClass('opacity-zero').removeClass("invisible");
$(".test-svg").hide();
$main.css("top");
$main.removeClass("opacity-zero");
}, 3000);
最好检查这个演示http://codepen.io/suez/pen/54bbb2f09e8d7680da1af2faa29a0aef?editors=011
以下是对我有用的:
改变了
来自:
opacity: 0.52;
background-color: #7c7c7c;
To:
opacity: 1 !important;
background-color: rgba(124, 124, 124, 0.52) !important;
要将十六进制和不透明度转换为rgba, 使用http://hex2rgba.devoth.com/这样的网站
在mac上,你可以使用预览编辑器将不透明度应用到你的。png图像上的白色矩形上,然后把它放在你的。css中。1)ImageLogo2)在imagereectanle周围创建一个矩形3)改变背景颜色为白色矩形变成白色4)调整矩形不透明图像