[英]CSS opacity issue on hover
This is my fiddle: http://jsfiddle.net/Vbtts/2624/ This is the html: 这是我的小提琴: http : //jsfiddle.net/Vbtts/2624/这是html:
<a href="#" class="col-xs-6 col-sm-4 big-container with-background">
<div class="bottom-align">
<label class="uppercase">preséntation</label>
<h2>Une entreprise<br> intégrée pour<br> contrôler la qualité<br> de toutes les étapes<br> des projets></h2>
</div>
</a>
and the css: 和css:
.with-background{width:100%;background:url(https://cask.scotch.io/2015/04/scotch-box-sidebar.png);display:block;height:200px;}
.with-background:hover{opacity:0.7}
What i want to achieve on hover state is to do an opacity on the background image, but NOT on the text (on label and h2). 我想要在悬停状态下实现的是在背景图像上进行不透明,但不在文本上(在标签和h2上)。 How can i do that ? 我怎样才能做到这一点 ? Thx 谢谢
You can't. 你不能。 You have got two different solutions: 您有两种不同的解决方案:
.background {
background: url(your-semitransp.png) #000;
}
.background:hover {
background-color: transparent;
}
With this mode you can emulate the opacity to 100% with the #000
background color, and in hover remove it to make the background semitransparent. 使用此模式,您可以使用#000
背景颜色将不透明度模拟为100%,并在悬停时将其移除以使背景变为半透明。
If you write a similar structure: 如果你写一个类似的结构:
<div class="wrap">
<div class="background"></div>
<div class="text"></div>
</div>
You can put background in absolute positioning and text above it. 您可以将背景放在绝对定位和文本上方。
.wrap { position : relative; }
.background {
position :absolute;
top: 0;
left: 0;
z-index: 0;
}
.text {
position :absolute;
top: 0;
left: 0;
z-index: 1;
}
.wrap:hover .background{
opacity: 0.7;
}
::after
or ::before
solution: ::after
或::before
解决方案: I can achieve that with your fiddle: 我可以用你的小提琴来达到这个目的:
http://jsfiddle.net/Vbtts/2632/ http://jsfiddle.net/Vbtts/2632/
But I hate negative z-index, so I can't recommend you to make that. 但我讨厌负z-index,所以我不建议你这样做。
I updated your jsfiddle, use a before or after element 我更新了你的jsfiddle,使用了before或after元素
.with-background{
width:100%;
display:block;
height:200px;
position: relative;
}
.with-background:before{
width:100%;
background:url(https://cask.scotch.io/2015/04/scotch-box-sidebar.png);
display:block;
height:100%;
content: " ";
position: absolute;
top:0;
left:0;
z-index: -1;
}
.with-background:hover:before{opacity:0.2}
http://jsfiddle.net/Vbtts/2635/ http://jsfiddle.net/Vbtts/2635/
if however you what to avoid negative z-index you may also do this: http://jsfiddle.net/Vbtts/2640/ 如果你要避免负z-index,你也可以这样做: http : //jsfiddle.net/Vbtts/2640/
There is no CSS property background-opacity, but you can fake it by inserting a pseudo element with regular opacity the exact size of the element behind it. 没有CSS属性background-opacity,但你可以通过插入一个具有常规不透明度的伪元素来伪造它,后面是元素的确切大小。 When using this method, altering the HTML is not needed. 使用此方法时,不需要更改HTML。
.with-background::after {
content: "";
background: url(https://cask.scotch.io/2015/04/scotch-box-sidebar.png);
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -1;
}
.with-background:hover::after {
opacity: 0.7;
}
I updated your jsfiddle here: https://jsfiddle.net/Vbtts/2625/ 我在这里更新了你的jsfiddle: https ://jsfiddle.net/Vbtts/2625/
Reference: https://css-tricks.com/snippets/css/transparent-background-images/ 参考: https : //css-tricks.com/snippets/css/transparent-background-images/
Opacity affects children, so what you want is difficult to achieve with opacity. 不透明度影响孩子,所以你想要的是不透明度难以实现的。
A workaround would be to add a gradient with rgba to the background image. 解决方法是将带有rgba的渐变添加到背景图像中。 This is effectively an invisible image overlay that is shown on hover. 这实际上是悬停时显示的不可见图像叠加。
Invisible layer: 隐形层:
.with-background {
background: linear-gradient(
rgba(255, 255, 255, 0),
rgba(255, 255, 255, 0) ),
url('https://cask.scotch.io/2015/04/scotch-box-sidebar.png');
}
Opacity added on hover: 悬停时添加了不透明度:
.with-background:hover {
background: linear-gradient(
rgba(255, 255, 255, 0.4),
rgba(255, 255, 255, 0.4)),
url('https://cask.scotch.io/2015/04/scotch-box-sidebar.png') ;
}
Fiddle - http://jsfiddle.net/5gnk28ah/1/ 小提琴 - http://jsfiddle.net/5gnk28ah/1/
You should use absolute positioning. 你应该使用绝对定位。 Using it you could place the image behind the text without wrapping it. 使用它你可以将图像放在文本后面而不包装它。
I've change your fiddle as follow: 我改变你的小提琴如下:
<a href="#" class="col-xs-6 col-sm-4 big-container">
<div class="with-background"></div>div>
<div class="bottom-align">
<label class="uppercase">preséntation</label>
<h2>Une entreprise<br> intégrée pour<br> contrôler la qualité<br> de toutes les étapes<br> des projets></h2>
</div>
</a>
CSS: CSS:
.with-background{width:100%;background:url(https://cask.scotch.io/2015/04/scotch-box-sidebar.png);display:block;height:200px; position: absolute; }
.with-background:hover{opacity:0.7}
.bottom-align {position: absolute; }
I hope that it shows you the way how do it :) 我希望它能告诉你如何做到这一点:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.