【问题标题】:IE9 Rounded Corners & CSS Background Gradients Living Together?IE9 圆角和 CSS 背景渐变一起生活?
【发布时间】:2011-09-02 01:56:46
【问题描述】:

我在 IE9 中遇到了一件奇怪的事情,试图让背景渐变显示出来。

基本上我将多个类应用于容器对象。

<div class="gradient corners"></div>

使用这个 CSS。

.gradient {
background-color: #96A7C5;
background-image: -webkit-gradient(
linear,
left bottom,
left top,
color-stop(0.19, #6C86AD),
color-stop(0.6, #96A7C5)
);
background-image: -moz-linear-gradient(
center bottom,
#75A33A 19%,
#8DC447 60%
);

.corners {
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}

为了在 IE 中获得渐变,我将过滤垃圾应用到我的 .gradient 类。

filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#8DC447', endColorstr='#75A33A');

这样,渐变效果有效,但我的圆角消失了。

所以我尝试为过滤器声明添加一个条件。

<!--[if IE]>
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#8DC447', endColorstr='#75A33A');
<![endif]-->

这让我的角落恢复了,但渐变消失了。

【问题讨论】:

标签: html css internet-explorer-9 conditional-comments


【解决方案1】:

只需使用包装 div(圆形和溢出隐藏)来裁剪 IE9 的半径。简单,跨浏览器工作。 SVG 或 JS 是不必要的。

<div class="ie9roundedgradient"><div class="roundedgradient">text or whatever</div></div>

.ie9roundedgradient { 
display:inline-block; overflow:hidden; -webkit-border-radius: 8px; -moz-border-radius: 8px; border-radius: 8px; 
}
.roundedgradient { 
-webkit-border-radius: 8px; -moz-border-radius: 8px; border-radius: 8px; 
/* use colorzilla to generate your cross-browser gradients */ 
}

【讨论】:

    【解决方案2】:

    对于 IE9 渐变与角的问题,这是一个非常可靠的解决方法。 它使用 js 动态生成 SVG - 而且速度很快。

    http://abouthalf.com/2011/10/04/updated-ie9-gradients-with-rounded-corners/

    【讨论】:

    • 虽然 svg 支持很广泛,但您将依赖 js 进行样式设置 - 如果非 js 版本对项目很重要,这可能很重要
    【解决方案3】:

    圆角和滤镜不能同时使用。对于 IE,我总是包含 http://css3pie.com 并使用它在 IE 中进行边框半径和渐变。示例 CSS 如下所示:

    .someElement {
        behavior: url(assets/js/PIE.htc);
        border-radius: 10px;
        background: #8cb2d1;
        background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzhjYjJkMSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjQyJSIgc3RvcC1jb2xvcj0iIzQwODBiMyIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgPC9saW5lYXJHcmFkaWVudD4KICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWQtdWNnZy1nZW5lcmF0ZWQpIiAvPgo8L3N2Zz4=);
        background: -moz-linear-gradient(top,  #8cb2d1 0%, #4080b3 42%);
        background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#8cb2d1), color-stop(42%,#4080b3));
        background: -webkit-linear-gradient(top,  #8cb2d1 0%,#4080b3 42%);
        background: -o-linear-gradient(top,  #8cb2d1 0%,#4080b3 42%);
        background: -ms-linear-gradient(top,  #8cb2d1 0%,#4080b3 42%);
        background: linear-gradient(top,  #8cb2d1 0%,#4080b3 42%);
        -pie-background: linear-gradient(top, #8cb2d1 0%,#4080b3 42%);
    }
    

    步骤:

    1. 通过行为包含 PIE.htc
    2. 照常添加边框半径(border-radius: 10px;)
    3. 添加特殊的 -pie-background 属性:(-pie-background: linear-gradient(top, #8cb2d1 0%,#4080b3 42%);)

    【讨论】:

    • 他说的是 IE9,它支持开箱即用的圆角,是的,它们可以与渐变滤镜一起使用(我现在正在这样做),尽管有一些警告。正如@Pavot 所说,背景渐变将开箱即用。如果您定义了一些边界和足够的边界半径,您会注意到圆角仍然存在(我知道这是一个老问题;我刚刚删除了这个作为回复 - 这是我两年前写的 - 并将其作为评论)。
    【解决方案4】:

    渐变会在 IE9 中用于圆角,所以目前最好的解决方案是添加一个额外的 div:

     <div class="corners"><div class="gradient"></div></div>
    

    并隐藏 .corners 的溢出

    .corners {
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;
    
    overflow: hidden;
    }
    

    我推荐这个类似 Photoshop 的工具来创建跨浏览器渐变:http://www.colorzilla.com/gradient-editor/

    而这个创建边界半径: http://border-radius.com/

    【讨论】:

      【解决方案5】:

      您不需要 if IE 块。

      只需将所有 3(如果您同时包含 IE 则包含 4 个)规则放在样式声明中,浏览器只会选择他们理解的那些。

      一个例子:

      .gradient {
      background-image: -moz-linear-gradient(top, #81a8cb, #4477a1); /* Firefox 3.6 */
      background-image: -webkit-gradient(linear,left bottom,left top,color-stop(0, #4477a1),color-stop(1,#81a8cb)); /* Safari & Chrome */
      filter:  progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr='#81a8cb', endColorstr='#4477a1'); /* IE6 & IE7 */
      -ms-filter: "progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr='#81a8cb', endColorstr='#4477a1')"; /* IE8 */
      }
      

      还应注意,IE 中的渐变仅在元素 hasLayout 时才有效(参见:http://reference.sitepoint.com/css/haslayout

      【讨论】:

      • 从技术上讲,它是五个。 Webkit 更改了他们的渐变代码,以更符合新的 W3C 推荐草案。
      猜你喜欢
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多