如何让div的背景色变成半透明?
我只希望 div 中的白色背景颜色半透明大约 50%。内容应该是完全不透明的。执行此操作的正确方法是什么?我想象当我查找 背景 CSS 属性 时,我会找到一个不透明度设置,但没有。不关心IE6。
更新:使用下面给出的 rgba 解决方案结合 CSS3PIE 让 rgba 在 IE 浏览器中工作的解决方案。
I only want the background color of white in my div to be translucent roughly 50%. The content should be fully opaque. What's the proper way to do this? I imagined when I looked up the background CSS property, I'd find an opacity setting, but didn't. Don't care about IE6.
UPDATE: solving with the rgba solution given below in conjunction with CSS3PIE's solution for getting rgba to work in IE browsers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
background-color: rgba()
表示法:Edited to add the default
background-color
(for browsers that don't understand thergba()
notation). Albeit I was under the impression that all but IE do understand it (but I could be wrong, and haven't tested to be sure...).感谢@akamike 进行编辑。
Edited to address question from OP (in comments):
我能找到的最好的信息是 CSS Tricks 的
rgba()
浏览器支持表,其中包含演示链接和“更完整”的兼容性表 。You can use the
background-color: rgba()
notation:Edited to add the default
background-color
(for browsers that don't understand thergba()
notation). Albeit I was under the impression that all but IE do understand it (but I could be wrong, and haven't tested to be sure...).Edit with thanks to @akamike.
Edited to address question from OP (in comments):
The best information I could find is the CSS Tricks'
rgba()
browser support table, with a link to a demo and 'more complete' compatibility table.如果你想要跨浏览器的不透明度,你可以在你的CSS定义中处理每个
If you want cross-browser opacity, you can handle each within your css definition
有一个名为
backdrop-filter
的 CSS 属性提供真正的半透明度(与 CSS 中已经提供的透明度相反)。目前所有现代浏览器都支持。
There is a CSS property called
backdrop-filter
providing real translucency (as opposed to transparency, which is already available in CSS).Currently supported by all modern browsers.
最简单的方法是创建一个半透明的 PNG 并将其用作 div 的背景图像。
如果您使用 Photoshop(或类似工具),只需创建一个 10 像素 x 10 像素的全白色图像,然后将不透明度滑块向下拖动到 50%。将其保存为 PNG 格式,您应该会很兴奋!
使用 RGBA 也是一种可能性,但是您不仅会失去 IE6,还有相当多的人使用不支持 alpha 方案的浏览器。
Easiest way is to create a semi-transparent PNG and just use that as your background image for the div.
If you're using Photoshop (or similar tools) just create a 10px by 10px image that is all white -- then drag the opacity slider down to 50%. Save it as a PNG and you should be rockin'!
Using RGBA is also a possibility, but you're not just losing IE6 then -- there are still quite a few people using browsers that don't support the alpha scheme.