简体   繁体   English

IE9 圆角和 CSS 背景渐变共同生活?

[英]IE9 Rounded Corners & CSS Background Gradients Living Together?

I came across a weird thing in IE9 trying to get a background gradient to display.我在 IE9 中遇到了一个奇怪的事情,试图让背景渐变显示。

Basically I'm applying multiple classes to a container object.基本上我将多个类应用于容器 object。

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

Using this CSS.使用这个 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;
}

To get the gradient in IE, I apply the filter garbage to my.gradient class.为了在 IE 中获得渐变,我将过滤垃圾应用到 my.gradient class。

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

With that, the gradient works but my rounded corners go away.这样,渐变就可以了,但是我的圆角 go 消失了。

So I tried putting in a conditional for the filter declaration.所以我尝试为过滤器声明添加一个条件。

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

That brings back my corners but the gradient goes away.这带回了我的角落,但渐变消失了。

Gradient will go out for rounded corners in IE9, so the best solution for now to add one extra div: Gradient 将 go 用于 IE9 中的圆角,因此现在最好的解决方案是添加一个额外的 div:

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

and hide overflow for.corners并隐藏溢出 for.corners

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

overflow: hidden;
}

I recomend this Photoshop-like tool for creating cross-browser gradients: http://www.colorzilla.com/gradient-editor/我推荐这个类似 Photoshop 的工具来创建跨浏览器渐变: http://www.colorzilla.com/gradient-editor/

And this one to create border-radius: http://border-radius.com/而这个创建边界半径: http://border-radius.com/

This is a pretty solid workaround for the IE9 gradients vs corners issue.对于 IE9 渐变与角落问题,这是一个非常可靠的解决方法。 It uses js to generate an SVG on the fly - and it's fast.它使用 js 即时生成 SVG - 而且速度很快。

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

you don't need the if IE block.你不需要if IE块。

just put all 3(4 if you include both IEs) rules in the style declaration, the browsers will only pick up the ones that they understand.只需将所有 3(如果您同时包含 IE,则为 4)规则放在样式声明中,浏览器只会选择他们理解的规则。

an example:一个例子:

.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 */
}

It should also be noted that gradients in IE will only work if the element hasLayout (See: http://reference.sitepoint.com/css/haslayout )还需要注意的是,IE 中的渐变只有在元素hasLayout时才会起作用(参见: http://reference.sitepoint.com/css/haslayout

rounded corners and filter don't go together.圆角和过滤器不要 go 在一起。 for IE i always include http://css3pie.com and use it to do border-radius and gradients in IE.对于 IE,我总是包含http://css3pie.com并用它在 IE 中做边界半径和渐变。 a sample css looks like this:示例 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%);
}

The steps:步骤:

  1. include PIE.htc via behavior通过行为包含 PIE.htc
  2. add border radius as usual (border-radius: 10px;)像往常一样添加边框半径(边框半径:10px;)
  3. add the special -pie-background attribute: (-pie-background: linear-gradient(top, #8cb2d1 0%,#4080b3 42%);)添加特殊的 -pie-background 属性: (-pie-background: linear-gradient(top, #8cb2d1 0%,#4080b3 42%);)

Just use a wrapper div (rounded & overflow hidden) to clip the radius for IE9.只需使用包装器 div(圆形和溢出隐藏)来裁剪 IE9 的半径。 Simple, works cross-browser.简单,跨浏览器工作。 SVG or JS are unnecessary.不需要 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 */ 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM