如何让div的背景色变成半透明?

发布于 2024-10-13 00:24:27 字数 372 浏览 8 评论 0原文

我只希望 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦幻的味道 2024-10-20 00:24:27

您可以使用 background-color: rgba() 表示法:

#theIdofYourElement,
.classOfElements {
    background-color: #fff;
    background-color: rgba(255,255,255,0.5);
}


Edited to add the default background-color (for browsers that don't understand the rgba() 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):

哪些浏览器不支持rgba?将来都会吗,这是css3的一部分吗?

我能找到的最好的信息是 CSS Tricks 的 rgba() 浏览器支持表,其中包含演示链接和“更完整”的兼容性表

You can use the background-color: rgba() notation:

#theIdofYourElement,
.classOfElements {
    background-color: #fff;
    background-color: rgba(255,255,255,0.5);
}


Edited to add the default background-color (for browsers that don't understand the rgba() 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):

which browsers don't understand rgba? will they all in the future, is this part of css3?

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.

沫离伤花 2024-10-20 00:24:27

如果你想要跨浏览器的不透明度,你可以在你的CSS定义中处理每个

div
{
    opacity: .50; /* Standard: FF gt 1.5, Opera, Safari, CSS3 */
    filter: alpha(opacity=50); /* IE lt 8 */
    -ms-filter: "alpha(opacity=50)"; /* IE 8 */
    -khtml-opacity: .50; /* Safari 1.x */
    -moz-opacity: .50; /* FF lt 1.5, Netscape */
}

If you want cross-browser opacity, you can handle each within your css definition

div
{
    opacity: .50; /* Standard: FF gt 1.5, Opera, Safari, CSS3 */
    filter: alpha(opacity=50); /* IE lt 8 */
    -ms-filter: "alpha(opacity=50)"; /* IE 8 */
    -khtml-opacity: .50; /* Safari 1.x */
    -moz-opacity: .50; /* FF lt 1.5, Netscape */
}
寂寞笑我太脆弱 2024-10-20 00:24:27

有一个名为 backdrop-filter 的 CSS 属性提供真正的半透明度(与 CSS 中已经提供的透明度相反)。

目前所有现代浏览器都支持。

.my-selector {
   backdrop-filter: blur(5px);
}

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.

.my-selector {
   backdrop-filter: blur(5px);
}
手长情犹 2024-10-20 00:24:27

最简单的方法是创建一个半透明的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文