IE 中的对角渐变

发布于 2024-11-09 06:43:51 字数 263 浏览 3 评论 0原文

有没有办法在 IE 中实现对角渐变?在 Chrome 中我可以做这样的事情:

body{
    background-image:-webkit-gradient(
    linear,
    left top,
    right bottom,
    color-stop(0%,#f00),
    color-stop(50%,#0f0),
    color-stop(100%,#00f));
}

但这在 IE 中不起作用。

Is there a way to have a diagonal gradient in IE? In Chrome I could do something like this:

body{
    background-image:-webkit-gradient(
    linear,
    left top,
    right bottom,
    color-stop(0%,#f00),
    color-stop(50%,#0f0),
    color-stop(100%,#00f));
}

but this doesn't work in IE.

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

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

发布评论

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

评论(2

轮廓§ 2024-11-16 06:43:51

是的,这是可能的!尽管它在其他浏览器中的效果不如真正的对角渐变。

该解决方案有两个重要方面使其发挥作用:

  • 两个具有相同位置和不同 z-index 值(一个在另一个顶部/前面)和不同渐变方向(一个水平,一个垂直)
  • 渐变中的透明/半透明颜色(您可以在 CSS3 透明度 + 渐变 中阅读相关内容)

只需将具有垂直渐变的 div 位于具有水平渐变的 div 后面(反之亦然,这并不重要),并确保最上面的渐变的颜色不是不透明的。

结果如下所示(Internet Explorer 8):

several rows of divs (紫色垂直背景,交替红色/蓝色水平前景) Larger example

和 CSS:

//left sample
.back
{
    filter: progid:DXImageTransform.Microsoft.gradient(GradientType="0", startColorstr='#880088', endColorstr='#110011');
    z-index:0;
}

.front
{
    filter: progid:DXImageTransform.Microsoft.gradient(GradientType="1", startColorstr='#55ffa885', endColorstr='#55330000');
    z-index:1;
}

//right sample
.diaggradientback
{
    position:absolute;
    left:0;
    top:0;
    width:100%;
    height:100%;
    overflow:hidden;
    filter: progid:DXImageTransform.Microsoft.gradient(GradientType='1', startColorstr='#ffa885', endColorstr='#330000');
}

.diaggradientfront
{
    position:absolute;
    left:0;
    top:0;
    width:100%;
    height:100%;
    overflow:hidden;
    filter: progid:DXImageTransform.Microsoft.gradient(GradientType='0', startColorstr='#bbffa885', endColorstr='#bb330000');
}

更新

有关此过滤器的文档确实表明可以一起应用多个过滤器。然而,事实证明,应用多个梯度过滤器会导致仅应用最后一个过滤器,因此简单地将两个过滤器应用到一层是行不通的,需要两层。

Yes, it is possible! Although it does not work as well as a real diagonal gradient in other browswers.

There are two important aspects of this solution that make it work:

  • Two divs with the same position and different z-index values (one on top of/in front of the other) and different gradient directions (one horizontal, one vertical)
  • Transparent/translucent colors in gradients (you can read about this in CSS3 Transparency + Gradient)

Simply place the div with the vertical gradient behind the div with the horizontal gradient (or vice-versa, it doesn't really matter), and make sure the coloring of the topmost gradient is not opaque.

The result looks like this (Internet Explorer 8):

several rows of divs (purple vertical backdrop, alternating red/blue horizontal forground) Larger sample

And the CSS:

//left sample
.back
{
    filter: progid:DXImageTransform.Microsoft.gradient(GradientType="0", startColorstr='#880088', endColorstr='#110011');
    z-index:0;
}

.front
{
    filter: progid:DXImageTransform.Microsoft.gradient(GradientType="1", startColorstr='#55ffa885', endColorstr='#55330000');
    z-index:1;
}

//right sample
.diaggradientback
{
    position:absolute;
    left:0;
    top:0;
    width:100%;
    height:100%;
    overflow:hidden;
    filter: progid:DXImageTransform.Microsoft.gradient(GradientType='1', startColorstr='#ffa885', endColorstr='#330000');
}

.diaggradientfront
{
    position:absolute;
    left:0;
    top:0;
    width:100%;
    height:100%;
    overflow:hidden;
    filter: progid:DXImageTransform.Microsoft.gradient(GradientType='0', startColorstr='#bbffa885', endColorstr='#bb330000');
}

Update:

The documention on this filter does say that multiple filters may be applied together. However, as it turns out, applying more than one gradient filter results in only the last one being applied, so simply applying both filters to one layer doesn't work, and two layers are necessary.

满地尘埃落定 2024-11-16 06:43:51

不幸的是,简短的答案是,不,你可以't. Microsoft 的渐变过滤器是二进制的 - 仅从左到右或从上到下。

但是,您可以使用 CSS3 PIE 来执行您想要的操作。请记住,PIE 在 IE9 中对渐变的支持有些粗略,即使 IE7 和 8 可以工作,也可能工作或不工作(请参阅 他们的论坛了解更多信息)。

The short answer is, unfortunately, no, you can't. Microsoft's gradient filter is binary - only left to right or top to bottom.

You might, however, be able to use CSS3 PIE to do what you want. Keep in mind that PIE's support for gradients in IE9 is somewhat sketchy, though, and may or may not work, even if IE7 and 8 do (see their forums for some more info).

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