Reputation: 2064
I often visit this website, to create a nice gradient for my html elements.
Example of CSS that is created:
myElement {
background: rgb(30,87,153); /* Old browsers */
background: -moz-linear-gradient(top, rgba(30,87,153,1) 0%, rgba(41,137,216,1) 50%, rgba(32,124,202,1) 51%, rgba(125,185,232,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(30,87,153,1)), color-stop(50%,rgba(41,137,216,1)), color-stop(51%,rgba(32,124,202,1)), color-stop(100%,rgba(125,185,232,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* IE10+ */
background: linear-gradient(to bottom, rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8',GradientType=0 ); /* IE6-9 */
}
Now when I have multiple different gradients on my elements you can imagine that the CSS file gets quite messy.
Is there a way that I can define these gradients as constants or some other way to create a simple tidy solution?
So all I would have to do was something like this
myElement {
background:GRADIENT_BLUE_GREEN;
}
Thanks! Any help is appreciated.
Upvotes: 0
Views: 284
Reputation: 7463
Like stated in other answers, declaring variables is not possible with pure css.
you can however, work around that and still achieve the desired tidy result using pure css - by using css animations.
you can define a static animation that will run once very fast and keep the last frame, and use the animation name as a variable for your elements for a tidy css:
.myElement {
width: 200px;
height: 100px;
display: block;
animation: GRADIENT_BLUE_GREEN 1ms 1 forwards;
}
/* you can define those in a separate file and link it where needed */
@-webkit-keyframes GRADIENT_BLUE_GREEN {
to {
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, rgba(30, 87, 153, 1)), color-stop(50%, rgba(41, 137, 216, 1)), color-stop(51%, rgba(32, 124, 202, 1)), color-stop(100%, rgba(125, 185, 232, 1)));
}
}
@-moz-keyframes GRADIENT_BLUE_GREEN {
to {
background: -moz-gradient(linear, left top, left bottom, color-stop(0%, rgba(30, 87, 153, 1)), color-stop(50%, rgba(41, 137, 216, 1)), color-stop(51%, rgba(32, 124, 202, 1)), color-stop(100%, rgba(125, 185, 232, 1)));
}
}
/* define more keyframes per browser as needed */
<div class="myElement"></div>
Upvotes: 0
Reputation: 2207
You can make it easy on yourself with CSS Preprocessors
but the actual file size is not going to change.
Upvotes: 1
Reputation: 407
You can't do that with pure CSS, but fortunately you can use a-built-on CSS
softwares like LESS and many other alternatives
Upvotes: 0