Using something like LESS or SASS, this is fairly easy to do legitimately. Create a mixin like this (robust version):
.auto-gradient(@color) {
/* Use any of the built in functions like saturate() or spin() */
@topcolor: lighten(@color, 20);
@bottomcolor: darken(@color, 20);
background: @color;
background: -webkit-gradient(linear, 0 0, 0 bottom, from(@topcolor), to(@bottomcolor));
background: -webkit-linear-gradient(@topcolor, @bottomcolor);
background: -moz-linear-gradient(@topcolor, @bottomcolor);
background: -ms-linear-gradient(@topcolor, @bottomcolor);
background: -o-linear-gradient(@topcolor, @bottomcolor);
background: linear-gradient(@topcolor, @bottomcolor);
/* If using PIE.htc for IE */
-pie-background: linear-gradient(@topcolor, @bottomcolor);
behavior: url(pie.htc);
}
Usage:
.my-button {
.auto-gradient(darkviolet);
}
This will compile to valid CSS(3), it should be something like this:
.my-button {
background:darkviolet;
background:-webkit-gradient(linear,0 0,0 bottom,from(#c43aff),to(#4c006d));
background:-webkit-linear-gradient(#c43aff,#4c006d);
background:-moz-linear-gradient(#c43aff,#4c006d);
background:-ms-linear-gradient(#c43aff,#4c006d);
background:-o-linear-gradient(#c43aff,#4c006d);
background:linear-gradient(#c43aff,#4c006d);
}
Note: I use lessphp myself, and the version I'm using now seems to choke on named colors like DarkViolet
being passed to lighten/darken unless they are lowercase.