如何在css中保持浏览器与Gradient的兼容性?

时间:2013-01-10 05:53:22

标签: html css cross-browser

我正在使用html,css和javascript开发一个网站。每个浏览器中的按钮显示不同。以下是同一页面的不同浏览器的屏幕截图:

Internet Explorer: enter image description here

Firefox: enter image description here

我实际上打算在firefox中显示它。这里有一些我正在使用的css代码:

#button{
float: left;
width: 500px;
height: 50px;
line-height: 50px;
background-color: #06C;
padding-left: 20px;

-moz-border-radius:10px;
-webkit-border-radius:10px;
border-radius:10px;

-webkit-box-shadow: 0px 1px 1px 0px rgba(255,255,255, .2), inset 0px 1px 1px 0px rgb(0,0,0);
-moz-box-shadow: 0px 1px 1px 0px rgba(255,255,255, .2), inset 0px 1px 1px 0px rgb(0,0,0);
box-shadow: 0px 1px 1px 0px rgba(255,255,255, .2), inset 0px 1px 1px 0px rgb(0,0,0);

background-image: -webkit-gradient(
    linear,
    left bottom,
    left top,
    color-stop(0, #60B842),
    color-stop(0.85, #7FD13D)
);
background-image: -moz-linear-gradient(
    center bottom,
    /* change these to change the button colors */
    #B58515 0%,
    #DC9E1F 85%
);

/* change this to change the text color and font type */
color:#fff;
font-family:arial,helvetica,sans-serif;
font-size:17px;
font-weight:bold;
text-shadow:1px 1px 1px #4c9434;
    }

    #button:hover{
background-image: -webkit-gradient(
    linear,
    left bottom,
    left top,
    color-stop(0, #6DD14B),
    color-stop(0.85, #85DB40)
);
background-image: -moz-linear-gradient(
    center bottom,
    /* change these colors to change the mouse hover colors */
    #E17100 0%,
    #FF9326 85%
);
box-shadow:0 2px 0 #5EA839;
    }

使用渐变可能存在一些问题。有人可以建议我进行任何更改或其他方式进行编码,以便网页在不同的浏览器中看起来相同吗?

5 个答案:

答案 0 :(得分:1)

我通常使用Colorzilla's Ultimate CSS Gradient Generator来生成跨浏览器的CSS渐变代码。

答案 1 :(得分:0)

除了-webkit-gradient()-moz-gradient()之外,您还需要为IE和其他浏览器使用前缀。

示例:

#linearBg2 {
  /* fallback */
  background-color: #1a82f7;
  background: url(images/linear_bg_2.png);
  background-repeat: repeat-x;

  /* Safari 4-5, Chrome 1-9 */
  background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#1a82f7), to(#2F2727));

  /* Safari 5.1, Chrome 10+ */
  background: -webkit-linear-gradient(top, #2F2727, #1a82f7);

  /* Firefox 3.6+ */
  background: -moz-linear-gradient(top, #2F2727, #1a82f7);

  /* IE 10 */
  background: -ms-linear-gradient(top, #2F2727, #1a82f7);

  /* Opera 11.10+ */
  background: -o-linear-gradient(top, #2F2727, #1a82f7);
}

Source

Read more here

Note that IE 9 and earlier do not support gradient

答案 2 :(得分:0)

发生的事情是IE完全忽略了你的渐变css。您必须添加“过滤器”才能使渐变出现在IE中。

This page将帮助您创建跨浏览器渐变。

答案 3 :(得分:0)

所有浏览器支持渐变属性 使用这个

 background: -moz-linear-gradient(top, white, #1a82f7);/*Mozila*/
    background: -o-linear-gradient(top, white, #1a82f7); /*opera*/
    background: -webkit-linear-gradient(top, white, #1a82f7);/*Chrome and safari*/
    filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='white',    EndColorStr='#1a82f7'); /*IE*/

答案 4 :(得分:0)

以下是一组CSS渐变,对于跨浏览器兼容性非常有用:

/* IE10 Consumer Preview */
background-image: -ms-linear-gradient(top left, #FFFFFF 0%, #00A3EF 100%);

/* Mozilla Firefox */
background-image: -moz-linear-gradient(top left, #FFFFFF 0%, #00A3EF 100%);

/* Opera */
background-image: -o-linear-gradient(top left, #FFFFFF 0%, #00A3EF 100%);

/* Webkit (Safari/Chrome 10) */
background-image: -webkit-gradient(linear, left top, right bottom, color-stop(0, #FFFFFF), color-stop(1, #00A3EF));

/* Webkit (Chrome 11+) */
background-image: -webkit-linear-gradient(top left, #FFFFFF 0%, #00A3EF 100%);

/* W3C Markup, IE10 Release Preview */
background-image: linear-gradient(to bottom right, #FFFFFF 0%, #00A3EF 100%);