当叠加在图像上时,使文本显示为白色并具有半透明黑色背景
我有一个简单的 CSS/HTMl 问题。我在 div 中有一张图像和一些文本。我使用 z 索引将文本放置在图像顶部。
文本为白色,黑色背景。我调整了文本 div 的不透明度,以便其下方的图像可见。看起来不错。
问题是文本显示为灰色而不是白色,因为不透明度降低了。如何使文本显示为白色,并且周围仍然有半透明的黑色背景?
<style type="text/css">
.wrap {
position:relative;
float:left;
clear:none;
overflow:hidden;
}
.wrap img {
position:relative;
z-index:1;
}
.wrap .desc {
display:block;
position:absolute;
width:166px;
top:20px;
left:20px;
z-index:2;
color: #FFFFFF;
padding: 10px;
background-color: #000000;
border-radius: 5px 5px 5px 5px;
-moz-border-radius: 5px 5px 5px 5px;
-webkit-border-radius: 5px 5px 5px 5px;
/*For IE*/
filter: alpha(opacity=60);
opacity: 0.60;
}
</style>
<div class="wrap">
<img src="path/to/pic.png" />
<h6 class="desc">This is my text about my image</h3>
</div>
有什么建议吗?
I've got a simple CSS/HTMl question. I've got an image and some text in a div. I've got the text positioned on top of the image using the z-index.
The text is white with a black background. I adjusted the text's div's opacity, so that the image beneath it is visible. It looks good.
The problem is that the text appears gray instead of white, because the opacity is lowered. How can I make the text appear white, and still have a semi-transparent black background around it?
<style type="text/css">
.wrap {
position:relative;
float:left;
clear:none;
overflow:hidden;
}
.wrap img {
position:relative;
z-index:1;
}
.wrap .desc {
display:block;
position:absolute;
width:166px;
top:20px;
left:20px;
z-index:2;
color: #FFFFFF;
padding: 10px;
background-color: #000000;
border-radius: 5px 5px 5px 5px;
-moz-border-radius: 5px 5px 5px 5px;
-webkit-border-radius: 5px 5px 5px 5px;
/*For IE*/
filter: alpha(opacity=60);
opacity: 0.60;
}
</style>
<div class="wrap">
<img src="path/to/pic.png" />
<h6 class="desc">This is my text about my image</h3>
</div>
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样怎么样:
CSS
HTML
Plnkr
http://plnkr.co/edit/aSd9rO?p=preview
How about like this:
CSS
HTML
Plnkr
http://plnkr.co/edit/aSd9rO?p=preview
根据您的浏览器支持要求,您可能可以将不透明度保留为 100%,并使用
rgba
颜色:颜色为红色、绿色、蓝色(每种颜色为 0-255)通过阿尔法(0-1.0)。
如果您需要在旧版浏览器中进行回退,通常可以使用:
对于旧版浏览器,这将默认为黑色,对于新版浏览器,默认为半透明。它避免了额外的下载(平铺图像),并在文本文件中保留更多样式(更易于版本控制、维护和查找)。
Depending on your browser support requirements, you might be able to get away with leaving opacity at 100%, and using an
rgba
color:The colors are Red, Green, Blue, (0-255 each) followed by Alpha (0-1.0).
If you need a fallback in older browsers, you can usually use:
This will default to Black for older browsers, and semi-transparent for newer ones. It avoids an extra download (of a tiling image), as well as keeping more of your styling in the text file (easier to version, maintain, and find).
我会在描述之前创建另一个具有相同高度和宽度的 div,将该 div 的不透明度设置为透明,添加背景,然后将描述放在另一个没有背景的 div 中。如果两者都具有绝对地位,那么后者应该排在前者之上。
请参阅此处的演示。
I would create another div before the description with the same height and width, set that div's opacity to transparent, add a background, then put the description in another div, without a background. If they both have absolute position, then the latter should go on top of the former.
See the demo here.
您可以在元素的背景中放置半透明图像。实际图像可能非常小,您可以重复它以覆盖整个背景。
下面是一个示例: http://jsfiddle.net/f6XS6/1/
You can put a semi-transparent image in the background of the element instead. The actual image can be very small and you can repeat it to cover the whole background.
Here's an example of what this could look like: http://jsfiddle.net/f6XS6/1/