【问题标题】:How to maintain browser compatability with Gradient in css?如何在 CSS 中保持浏览器与 Gradient 的兼容性?
【发布时间】:2013-01-10 05:53:21
【问题描述】:

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

Internet Explorer:

火狐:

我实际上希望它像在 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;
    }

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

【问题讨论】:

    标签: html css cross-browser


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      除了-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.

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

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

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

          【讨论】:

            【解决方案5】:

            这里有一组 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%);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-05-14
              • 2012-02-06
              • 2010-11-17
              • 1970-01-01
              • 2013-08-27
              相关资源
              最近更新 更多