Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
gbcode
2,220 PointsProblem with animated gradient
Hello everyone, hope your Monday is going great!
I've ran into an issue with animated gradients, I generated it from https://www.gradient-animator.com but the code I got seems to be broken or missing something.. At the end of the animation, there's a green/blue box that slides over almost half of the screen. There's no such thing occurring on the generator page. I suspect it's something with background-size, but no matter what value I set it to it keeps getting that green/blue box sliding over.. I believe the effect is made is by "zooming" in the background and then make the keyframes animate it up and down. I tried setting background-repeat to no-repeat, but that just makes the "box" white.. Is there any property I've missed here? Here's the code:
body {
background: linear-gradient(0deg, #2389c3, #29a8ce, #29cec8, #23ce84);
background-size: 600% 600%;
-webkit-animation: gradient-bg 22s ease infinite;
-moz-animation: gradient-bg 22s ease infinite;
animation: gradient-bg 22s ease infinite;
}
@-webkit-keyframes gradient-bg {
0% {
background-position: 50% 0%
}
50% {
background-position: 50% 100%
}
100% {
background-position: 50% 0%
}
}
@-moz-keyframes gradient-bg {
0% {
background-position: 50% 0%
}
50% {
background-position: 50% 100%
}
100% {
background-position: 50% 0%
}
}
@keyframes gradient-bg {
0% {
background-position: 50% 0%
}
50% {
background-position: 50% 100%
}
100% {
background-position: 50% 0%
}
}
Thanks!
7 Answers
Matt Milburn
20,787 PointsYes, the general idea of this solution looks like the code below.
<body>
<div class="content"></div>
<div class="bg"></div>
</body>
.content {
position: relative;
z-index: 2;
}
.bg {
/* insert background image styles here */
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1;
}
Matt Milburn
20,787 PointsHi gbcode,
I put your code into an HTML file and viewed it in the latest versions of Chrome, Firefox, Safari and Internet Explorer on Mac and Windows, respectively. But I was unable to recreate the issue you are describing.
Just as a guess... since you're using -moz and -webkit vendor prefixes on the @keyframes block, have you tried also including a vendor prefixes for the gradient? Also try re-ordering the property values in the animation rules. I'm not sure if this will be an immediate solution to your problem but it's important to rule out as much as possible if you're getting weird or totally unexpected results with such a short amount of code.
body {
/* this is how Bootstrap 3x handles vendor prefixed gradients (except for IE, lol) */
background-image: -webkit-linear-gradient(top, #2389c3, #29a8ce, #29cec8, #23ce84);
background-image: -o-linear-gradient(top, #2389c3, #29a8ce, #29cec8, #23ce84);
background-image: linear-gradient(to bottom, #2389c3, #29a8ce, #29cec8, #23ce84);
background-repeat: none; /* or whatever repeat value works for what you're doing */
-webkit-animation: 22s ease infinite gradient-bg;
-moz-animation: 22s ease infinite gradient-bg;
animation: 22s ease infinite gradient-bg;
}
Matt Milburn
20,787 PointsNow I can see the issue you are describing and I understand it a little better now.
Try this... but read the followup below the code.
html {
height: 100%;
}
body {
min-height: 100%;
}
Basically, the container the gradient is applied to (the body) doesn't have a defined height to work with the percentages in the background position. If I apply the styles above, it looks to me like its doing what you are expecting it to do.
However, I've enountered problems in my own work when I use the code above, so I try to avoid leaning on it as a solution.
If this solution works for you and it DOES turn out to cause you more issues down the road, then don't feel bad about using another <div> just to position the background to the viewport underneath all of the content, that way you don't have to wrap your entire DOM with a html { height: 100% } rule that can potentially code you into a corner.
gbcode
2,220 PointsThanks for the reply!
Didn't you get a green/blue box appearing from the bottom at all after half of the animation? Tried in multiple browsers and computers with the same effect.. I also tried re-ordring the animation properties but no luck there. Even weirder is that if I add the -webkit-linear-gradient the animation doesn't work at all?
I'll link my workspace preview here: https://port-80-331ybypf9w.treehouse-app.com
gbcode
2,220 PointsAh, it does! Although I see why you'd rather recommend using a <div> for the background, the animation is a bit weird compared to without the additional properties. But adding a background <div> with 100% width and height could possibly fix this?
Thank you!
gbcode
2,220 PointsGreat, thank you so much for the assistance! I'll report back tomorrow!
gbcode
2,220 PointsYep, works perfectly! I noticed that it stays on the blue gradient a lot longer than the green... But it's probably fixable with some other colors or animation adjustments(I hope). Thank you so much for helping me out, would've probably given up otherwise, really appreciated!
Matt Milburn
20,787 PointsYes, sometimes you can chalk that type of behavior up to "perceived discrepancies" and you just need to find a configuration that you're happy with :)