Barry Michael Doyle
Barry Michael Doyle

Reputation: 10628

CSS: Repeat a linear gradient inversely

Is there a way to have every alternating repeat of a gradient background go inverse? At the moment I have the following CSS:

html {
  background: white; /* For browsers that do not support gradients */
  background: -webkit-linear-gradient(blue, white); /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(blue, white); /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(blue, white); /* For Firefox 3.6 to 15 */
  background: linear-gradient(blue, white); /* Standard syntax */

  background-size: cover;
  height: 100%;
}

body {
  height: 100%;
}
<html>
  <head>
  </head>
  <body
    Hello world
  </body>
</html>

Currently it goes from blue to white top to bottom but as I scroll down it repeats again from blue to white Eg. blue->white; blue->white; blue->... . I would like it to go from blue -> white -> blue -> white ->... .

Upvotes: 3

Views: 540

Answers (3)

Madalina Taina
Madalina Taina

Reputation: 1978

I understand you have that background and at some action, you want the inverse effect. For this you can use transform: scaleY(-1). You can use the gradient in a pseudo-element, in :before{} to prevent child element from inheriting parent styles.

div{
  height: 100px;
  width:100%;
  float:left;
  margin-top:20px;
}
.div1 {
  background: white; /* For browsers that do not support gradients */
  background: -webkit-linear-gradient(blue, white); /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(blue, white); /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(blue, white); /* For Firefox 3.6 to 15 */
  background: linear-gradient(blue, white); /* Standard syntax */
  background-size: cover;
}

.div2 {
  -webkit-transform: scaleY(-1);
  transform: scaleY(-1);
}
 <body>
<div class="div1"></div>
<div class="div1 div2"></div>
</body>

Upvotes: 0

Mohammad Usman
Mohammad Usman

Reputation: 39332

You can use repeating-linear-gradient to achieve it as follows:

html {
  background: white;
  background: repeating-linear-gradient(blue, white 100vh, white 100vh, blue 200vh);
  height: 1980px;
}

body {
  height: 100%;
  margin: 0;
}

Upvotes: 3

mosawi
mosawi

Reputation: 20

remove the height:100%; from body and check it

or check this website to make your page prettier http://colorzilla.com/gradient-editor

Upvotes: 0

Related Questions