CSS (perhaps with Compass): Cross-browser gradient

后端 未结 3 903
梦毁少年i
梦毁少年i 2020-12-29 11:02

I would like to get a gradient in CSS (perhaps through Compass) that works in every major browser, including IE7+. Is there an easy way to do this (without writing a lot of

相关标签:
3条回答
  • 2020-12-29 11:51

    The code I use for all browser gradients..

                background: #0A284B;
                background: -webkit-gradient(linear, left top, left bottom, from(#0A284B), to(#135887));
                background: -moz-linear-gradient(top, #0A284B, #135887);
                background: -o-linear-gradient(#0A284B, #135887);
                filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#0A284B', endColorstr='#135887');
                zoom:1;
    

    You will need to specify a height or zoom:1 to apply hasLayout to the element for this to work in ie

    0 讨论(0)
  • 2020-12-29 11:53

    I just noticed that the current Compass beta (0.11.beta.6) has support for generating IE gradients in the compass/css3/images module (which supersedes the previous gradient module), so you can generate your gradients with a total of two short commands:

    @import "compass/css3/images";
    @import "compass/utilities/general/hacks";  /* filter-gradient needs this */
    
    .whatever {
      /* IE; docs say this should go first (or better, placed in separate IE-only stylesheet): */
      @include filter-gradient(#aaaaaa, #eeeeee);
      /* Fallback: */
      background: #cccccc;
      /* CSS 3 plus vendor prefixes: */
      @include background(linear-gradient(top, #aaaaaa, #eeeeee));
    }
    

    This generates the following slew of CSS:

    .whatever {
      *zoom: 1;
      -ms-filter: "progid:DXImageTransform.Microsoft.gradient(gradientType=0, startColorstr='#FFAAAAAA', endColorstr='#FFEEEEEE')";
      filter: progid:DXImageTransform.Microsoft.gradient(gradientType=0, startColorstr='#FFAAAAAA', endColorstr='#FFEEEEEE');
      background: #cccccc;
      background: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #aaaaaa), color-stop(100%, #eeeeee));
      background: -moz-linear-gradient(top, #aaaaaa, #eeeeee);
      background: -o-linear-gradient(top, #aaaaaa, #eeeeee);
      background: linear-gradient(top, #aaaaaa, #eeeeee);
    }
    

    I guess I would have preferred to have the IE and non-IE gradient code in one call, but since IE's DXImageTransform gradient function is pretty limited, that is probably not possible.

    0 讨论(0)
  • 2020-12-29 11:55

    While gradients are of limited complexity, they're complex enough to require what you would consider "lots of code".

    Consider:

    • starting colour, ending colour and the hexadecimal math required to transition between one and the other
    • The number of "steps"
    • The width/height of each step
    • Since there is no pure CSS way of doing this, it means rendering HTML, one element for each colour/step, without messing up your existing HTML

    So, no, without a plug-in that does all of this for you, it would require a bit of code, or an image.

    0 讨论(0)
提交回复
热议问题