How to make a gradient background in CSS?

3

How do I make CSS with and by the end blue , being the first from top to bottom, the second from left to right and the third radial? Preferably a code that is valid in most browsers.

HTML:

<div class='conjunto'>
    <div></div>
    <div></div>
    <div></div>
</div>
    
asked by anonymous 08.03.2015 / 22:56

2 answers

5

Here's a chance:

.conjunto div:nth-child(1) {
  background: -webkit-linear-gradient(green, yellow, blue); /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(green, yellow, blue); /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(green, yellow, blue); /* For Firefox 3.6 to 15 */
  background: linear-gradient(green, yellow, blue); /* Standard syntax */
}
.conjunto div:nth-child(2) {
  background: -webkit-linear-gradient(left, green, yellow, blue); /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(right, green, yellow, blue); /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(right, green, yellow, blue); /* For Firefox 3.6 to 15 */
  background: linear-gradient(right, green, yellow, blue); /* Standard syntax */
}
.conjunto div:nth-child(3) {
  background: -webkit-radial-gradient(green, yellow, blue); /* For Safari 5.1 to 6.0 */
  background: -o-radial-gradient(green, yellow, blue); /* For Opera 11.1 to 12.0 */
  background: -moz-radial-gradient(green, yellow, blue); /* For Firefox 3.6 to 15 */
  background: radial-gradient(green, yellow, blue); /* Standard syntax */
}

.conjunto div {
    width: 400px;
    height: 200px;
}

jsFiddle: link

By chance, w3schools has a good page about this , #, but in the background the syntax is:

  

background: linear-gradient (direction, color_1, color_2, ...);

    
08.03.2015 / 23:13
3

This tool ( Ultimate CSS Gradient Color Generator ) is a hand in the wheel when it comes to generating these gradients. The code it generates is compatible with various browsers.

An example of the code generated with what you asked:

.conjunto div {
  width: 150px;
  height: 100px;
  margin-left: 5px;
  float: left;
}

.conjunto div:nth-child(1) {
    background: #00ff00; /* Old browsers */
    background: -moz-linear-gradient(top,  #00ff00 0%, #ffff00 50%, #0000ff 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#00ff00), color-stop(50%,#ffff00), color-stop(100%,#0000ff)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  #00ff00 0%,#ffff00 50%,#0000ff 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  #00ff00 0%,#ffff00 50%,#0000ff 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  #00ff00 0%,#ffff00 50%,#0000ff 100%); /* IE10+ */
    background: linear-gradient(to bottom,  #00ff00 0%,#ffff00 50%,#0000ff 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00ff00', endColorstr='#0000ff',GradientType=0 ); /* IE6-9 */;
}

.conjunto div:nth-child(2) {
    background: #0000ff; /* Old browsers */
    background: -moz-linear-gradient(left,  #0000ff 0%, #ffff00 50%, #00ff00 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, right top, color-stop(0%,#0000ff), color-stop(50%,#ffff00), color-stop(100%,#00ff00)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(left,  #0000ff 0%,#ffff00 50%,#00ff00 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(left,  #0000ff 0%,#ffff00 50%,#00ff00 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(left,  #0000ff 0%,#ffff00 50%,#00ff00 100%); /* IE10+ */
    background: linear-gradient(to right,  #0000ff 0%,#ffff00 50%,#00ff00 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#0000ff', endColorstr='#00ff00',GradientType=1 ); /* IE6-9 */;
}

.conjunto div:nth-child(3) {
    background: #0000ff; /* Old browsers */
    background: -moz-radial-gradient(center, ellipse cover,  #0000ff 0%, #ffff00 50%, #00ff00 100%); /* FF3.6+ */
    background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%,#0000ff), color-stop(50%,#ffff00), color-stop(100%,#00ff00)); /* Chrome,Safari4+ */
    background: -webkit-radial-gradient(center, ellipse cover,  #0000ff 0%,#ffff00 50%,#00ff00 100%); /* Chrome10+,Safari5.1+ */
    background: -o-radial-gradient(center, ellipse cover,  #0000ff 0%,#ffff00 50%,#00ff00 100%); /* Opera 12+ */
    background: -ms-radial-gradient(center, ellipse cover,  #0000ff 0%,#ffff00 50%,#00ff00 100%); /* IE10+ */
    background: radial-gradient(ellipse at center,  #0000ff 0%,#ffff00 50%,#00ff00 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#0000ff', endColorstr='#00ff00',GradientType=1 ); /* IE6-9 fallback on horizontal gradient */;
}
<div class='conjunto'>
    <div></div>
    <div></div>
    <div></div>
</div>
    
08.03.2015 / 23:19