在 CSS2 标准中更改图像悬停的 alpha 值?

发布于 2024-07-29 03:31:54 字数 234 浏览 3 评论 0原文

我正在尝试为我的图像添加 Alpha 效果。 图像是圆角矩形。 我知道 CSS3 中有可以更改 alpha 的属性,但我正在尝试遵守 w3c 标准,该标准仍然是 CSS2。

抱歉,我没有提前正确地陈述我的问题。 当我将鼠标悬停在使用 CSS2 的图像上时,我试图更改 alpha。 我正在考虑使用“背景图像”作为 100% alpha,并使用 img 标签作为 50% alpha。 有没有更好的方法来做到这一点?

i'm trying to add alpha effect for my image. the image is in rounded corner rectangular shape. i know there is attributes to change the alpha in CSS3, but i'm trying to be compliant with the w3c standard, which is still CSS2.

Sorry i didn't state my question correctly ealier. i was trying to change the alpha when i hover over the image using CSS2. i'm thinking to use the "background-image" for 100% alpha, and use the img tag for the 50% alpha. is there any better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

辞旧 2024-08-05 03:31:54

如果图像是 PNG,则可以直接在图像中包含 Alpha。 当然,这需要 IE6 的 PNG Fix 脚本。

否则,您可以使用 CSS 设置透明度。

编辑:更新为仅在悬停时起作用,请注意,这在 IE6 中不起作用。

img.transparent{
    filter: alpha(opacity=100); /* internet explorer */
    opacity: 1;           /* fx, safari, opera, chrome */
}

img.transparent:hover {
    filter: alpha(opacity=50); /* internet explorer */
    opacity: 0.5;           /* fx, safari, opera, chrome */
}

If the image is a PNG, you can include alpha directly in the image. Of course this would require the PNG Fix script for IE6.

Otherwise, you can use CSS to set the transparency.

Edit: Updated to only work on hover, note that this won't work in IE6.

img.transparent{
    filter: alpha(opacity=100); /* internet explorer */
    opacity: 1;           /* fx, safari, opera, chrome */
}

img.transparent:hover {
    filter: alpha(opacity=50); /* internet explorer */
    opacity: 0.5;           /* fx, safari, opera, chrome */
}
辞取 2024-08-05 03:31:54

Web 开发人员实现透明效果的典型方法是使用部分透明的 PNG 文件作为背景。

div {
  background: #FFF url(img/bg.png) repeat top left;
}

使用 png 将按您的预期工作,但不透明度不会按预期工作:

div {
  filter: alpha(opacity=50); /* IE */
  -moz-opacity: 0.5; /* Firefox */
  -webkit-opacity: 0.5; /* Older Safari, Webkit */
  opacity: 0.5; /* CSS Standard - Always last in the list */
}

这将使 DIV 50% 透明,包括其所有子项、文本等。 您确实需要调整不透明度设置,以确保获得预期的结果。

The typical way a web developer implements the transparent effects is using a partially transparent PNG file as a background.

div {
  background: #FFF url(img/bg.png) repeat top left;
}

Using the png will work as you would expect, however opacity doesn't work as expected:

div {
  filter: alpha(opacity=50); /* IE */
  -moz-opacity: 0.5; /* Firefox */
  -webkit-opacity: 0.5; /* Older Safari, Webkit */
  opacity: 0.5; /* CSS Standard - Always last in the list */
}

This will make DIV 50% transparent, including all of its children, text and all. You will really need to play around with the opacity settings to make sure you get results as you would expect.

作死小能手 2024-08-05 03:31:54

如果您可以忍受 IE6 稍差的用户体验,一个更简单的修复方法是为所有现代浏览器使用 alpha 透明图像,并将不透明(或仅一种颜色)的图像发送到 IE6。 对于那些少数用户来说,看起来有点糟糕,但需要维护的代码要少得多。

An even simpler fix, if you can stand a slightly worse user experience for IE6, is to use an alpha-transparent image for all modern browsers, and send an image with no transparency (or just one-color) to IE6. Looks a little worse for those few users, but is a lot less code to maintain.

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