How can I make a page hero image move on page load? - css

I have a banner image loading on my site with this code:
#keyframes animatedBanner {
from {
background-position-y: bottom;
}
to {
background-position-y: top;
}
}
.page-hero {
background-image: url(https://via.placeholder.com/428x100);
background-repeat: no-repeat;
padding-top: 214px;
padding-bottom: 214px;
text-align: center;
animation: 2s ease-out 0s 1 animatedBanner;
background-size: auto;
}
<div class="page-hero">
<div class="inside-page-hero grid-container grid-parent">
<h2>this is an image</h2>
</div>
</div>
This does what I hoped it would (slide the image up when the page loads). But the problem is that I was hoping to have the image span the entire width of the screen regardless of width. I tried using cover, but then the image doesn't scroll at all.
Is there a way to make this design responsive on the image?

background are a bit tricky when it comes to animate the position. You need some specific sizes. I have a detailed answer (Using percentage values with background-position on a linear-gradient) explaining the logic behind the values I am using:
#keyframes animatedBanner {
from {
background-position: 50% 200%;
}
to {
background-position: 50% 50%;
}
}
.page-hero {
background-image: url(https://picsum.photos/id/1068/800/400);
background-repeat: no-repeat;
padding:100px 0;
text-align: center;
animation: 2s ease-out animatedBanner both .5s;
background-size: auto 200%;
}
<div class="page-hero">
<div class="inside-page-hero grid-container grid-parent">
<h2>this is an image</h2>
</div>
</div>
To get the opposite direction
#keyframes animatedBanner {
from {
background-position: 50% -100%;
}
to {
background-position: 50% 50%;
}
}
.page-hero {
background-image: url(https://picsum.photos/id/1068/800/400);
background-repeat: no-repeat;
padding:100px 0;
text-align: center;
animation: 2s ease-out animatedBanner both .5s;
background-size: auto 200%;
}
<div class="page-hero">
<div class="inside-page-hero grid-container grid-parent">
<h2>this is an image</h2>
</div>
</div>
And if you want to avoid the complexity of background-position and background-size, use pseudo-element
#keyframes animatedBanner {
from {
transform: translateY(100%); /* or -100% */
}
}
.page-hero {
padding: 100px 0;
text-align: center;
position: relative;
z-index: 0;
overflow: hidden;
}
.page-hero:before {
content: "";
position: absolute;
z-index: -1;
inset: 0;
background: url(https://picsum.photos/id/1068/800/400) center/cover;
animation: 2s ease-out animatedBanner both .5s;
}
<div class="page-hero">
<div class="inside-page-hero grid-container grid-parent">
<h2>this is an image</h2>
</div>
</div>

Related

#keyframes animation not running [duplicate]

This question already has answers here:
Using percentage values with background-position on a linear-gradient
(2 answers)
Closed 3 years ago.
I can't run linear-gradient animation by #keyframes. I think it is due to background-position property in my code that is causing the problem. However, https://webdevtrick.com/css-gradient-background/'>here, background-position property doesn't cause the problem.
I have compared my code to the code at that site to see what essential property is missing in mine. Here is the CSS code:
body {
margin: 0;
}
.navbar {
background-image: linear-gradient(125deg, #337909, #082a87);
height: 10%;
width: 100%;
position: absolute;
-webkit-animation: animebg 5s infinite;
animation: animebg 5s infinite;
background-size: auto;
}
header {
color: aliceblue;
font-family: cursive;
text-align: center;
}
#-webkit-keyframes animebg {
0% {
background-position: 0% 50%;
}
50% {
background-position: 100% 50%;
}
100% {
background-position: 0% 50%;
}
}
#keyframes animebg {
0% {
background-position: 0% 50%;
}
50% {
background-position: 100% 50%;
}
100% {
background-position: 0% 50%;
}
}
HTML code:
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='theme.css'>
<style>
</style>
</head>
<body>
<header class='navbar'>
<h1>Welcome!</h1>
</header>
<!--<button type="button" class="button1">Yes!</button>-->
</body>
</html>
I think you can't move position with percentage in animation (Use another unit) unless you set background-size to it. Look at the snippet below:
I hope it helps :)
body {
margin: 0;
}
.navbar {
background-image: linear-gradient(90deg, #337909, #082a87, #337909);
background-size: auto;
height: 10%;
width: 100%;
position: absolute;
-webkit-animation: animebg 2.5s linear infinite;
-moz-animation: animebg 2.5s linear infinite;
animation: animebg 2.5s linear infinite;
}
header {
color: aliceblue;
font-family: cursive;
text-align: center;
}
#keyframes animebg {
0% {
background-position: 100vw 50%;
}
100% {
background-position: 0 50%;
}
}
<header class='navbar'>
<h1>Welcome!</h1>
</header>

Adding image background size animation effect to element in pure CSS

Why I am not able to add animation of changing background image size of div on load? I know this is doable using JS by adding a class to the body or element but how we can achieve this in pure CSS?
html,body {
height: 100%;
width:100%;
}
div {
height: 100%;
width:100%;
background-image: url(https://upload.wikimedia.org/wikipedia/commons/d/da/Internet2.jpg);
background-size: 100%;
background-repeat: no-repeat;
background-position: 50% 50%;
animation-name: animate;
transition: all 3s;
}
.animate {
from {background-size: 100%}
to {background-size: 110%}
}
<div></div>
html, body { height: 100%; width: 100%; }
div {
height: 100%;
width: 100%;
background-image: url(https://upload.wikimedia.org/wikipedia/commons/d/da/Internet2.jpg);
background-size: 100%;
background-repeat: no-repeat;
background-position: 50% 50%;
animation: animate 3s ease forwards;
transition: all 3s;
}
#keyframes animate {
from { background-size: 100%; }
to { background-size: 150%; }
}
<div></div>

animate background-position to continuously move horizontally [duplicate]

This question already has answers here:
Background-position not working with CSS animation and linear gradient
(2 answers)
Use CSS3 transitions with gradient backgrounds
(19 answers)
Closed 4 years ago.
I want to move my background using css horizontally.
I've looked at this demo, but they're using an actual image. I want to be able to use rgba / linear-gradient instead.
This is my code:
.chat {
width: 490px;
float: left;
background: #F2F5F8;
color: #434651;
position:absolute;
overflow:hidden;
}
#keyframes animatedBackground {
from { background-position: 0 0; }
to { background-position: 100% 0; }
}
.chat .chat-header {
/* padding: 20px; */
border-bottom: 2px solid white;
background-image: linear-gradient(135deg, #FF5572, #FF7555);
width:1000px; /*make bigger in order to move */
overflow:hidden;
background-position: 0px 0px;
background-repeat: repeat-x;
animation: animatedBackground 4s linear infinite;
}
<div class="chat">
<div class="chat-header clearfix">
<div class="chat-about">
hi
</div>
</div>
</div>
I want it to animate in the same way as in the demo. How would i achieve that?
You can use background position to translate it through the image background like the example..
.chat {
width: 490px;
float: left;
background: #F2F5F8;
color: #434651;
position:absolute;
overflow:hidden;
}
#keyframes animatedBackground {
0% {
background-position: 0% 0%
}
50% {
background-position: 0% 100%
}
100% {
background-position: 0% 0%
}
}
.chat .chat-header {
/* padding: 20px; */
border-bottom: 2px solid white;
background-image: linear-gradient(180deg, #FF5572, blue, #FF5572);
width:1000px; /*make bigger in order to move */
overflow:hidden;
background-position: 0px 0px;
background-repeat: repeat-x;
animation: animatedBackground 4s linear infinite;
margin:0;
height:400px;
background-size: 100% 400%;
}
<div class="chat">
<div class="chat-header clearfix">
<div class="chat-about">
hi
</div>
</div>
</div>
.chat {
background: linear-gradient(134deg, #ff5572, #ff7555);
background-size: 400% 400%;
animation: Move 4s ease infinite;
}
#keyframes Move {
from { background-position: 0 0; }
to { background-position: 100% 0; }
}
There's a great tool for playing around with gradient animations at https://www.gradient-animator.com/

Material ripple style background animation

I have found this fiddle, but the animation works only if I click and hold.
#parent {
height: 200px;
width: 400px;
background-color: lightgray;
}
#circle {
background-image: url("http://upload.wikimedia.org/wikipedia/commons/0/0e/Ski_trail_rating_symbol-green_circle.svg");
background-position: center center;
background-repeat: no-repeat;
background-size: 0 0;
height: 200px;
width: 400px;
transition: .3s ease-in;
}
#parent:active #circle {
background-size: 600px 600px;
}
<div id="parent">
<div id="circle"></div>
</div>
Would it be possible to make the animation to load when the page loads?
Just want some background load in animation similarly to Android Lollipop ripple / Firefox OS for TV.
You can use CSS3 animation for the transition to start automatically.
Learn more about animation here:
https://css-tricks.com/almanac/properties/a/animation/
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Animations/Using_CSS_animations
http://www.w3schools.com/css/css3_animations.asp
Updated fiddle: Fiddle
Snippet:
#parent {
height: 200px;
width: 400px;
background-color: lightgray;
}
#circle {
background-image: url("http://upload.wikimedia.org/wikipedia/commons/0/0e/Ski_trail_rating_symbol-green_circle.svg");
background-position: center center;
background-repeat: no-repeat;
background-size: 0 0;
height: 200px;
width: 400px;
/*transition: .3s ease-in;*/
animation: example 1s ease-in infinite;
}
#keyframes example {
0% {
background-size: 0px 0px;
}
50% {
background-size: 600px 600px;
}
100% {
background-size: 0px 0px;
}
}
<div id="parent">
<div id="circle"></div>
</div>

CSS way of looping a background image with cover or contain sizing

Say you are trying to animate a tilable background like this:
.container {
width: 160px;
height: 91px;
}
.bg {
width: 100%;
height: 100%;
background: url(http://i60.tinypic.com/2j2fhjm.jpg) repeat-x left center;
background-size: contain;
-webkit-animation: displace 2s linear infinite;
animation: displace 2s linear infinite;
}
#-webkit-keyframes displace {
from {
background-position: 0 center;
}
to {
background-position: 160px center;
}
}
#keyframes displace {
from {
background-position: 0 center;
}
to {
background-position: 160px center;
}
}
<div class="container">
<textarea class="bg"></textarea>
</div>
As soon as you change the dimensions of the container, the looping animation breaks!
Is there any way to make this responsive without JS?
The problem is that, to make it responsive, you need to set the animated background-position using percentages.
But, when you set background-size as cover or contain, in some cases the width is adjusted to 100%. In this case, background-position using percentages is useless (won't move it).
The only way that I have found to manage this is moving the image to a pseudo element, and moving it. To keep the continuity, though, we will need two pseudo elements.
But that won't work on a textarea.
You didn't said anything about textarea being a requirement, so I am posting this. To show that it works on resize, hover it.
.container {
width: 160px;
height: 100px;
position: relative;
border: solid 1px black;
display: inline-block;
}
.container:nth-child(2) {
width: 220px;
}
.bg {
position: absolute;
width: 100%;
height: 100%;
overflow: hidden;
}
.bg:before, .bg:after {
content: "";
position: absolute;
width: 100%;
height: 100%;
background-image: url(http://i.stack.imgur.com/wBHey.png);
background-size: 100%;
animation: move 2s infinite linear;
}
.bg:before {
right: 100%;
}
#keyframes move {
from {transform: translateX( 0%);}
to {transform: translateX(100%);}
}
<div class="container">
<div class="bg"></div>
</div>
<div class="container">
<div class="bg"></div>
</div>
I was able to make it work by making the background twice as big.
I know this isn't the perfect solution, but maybe you can do a trick with the image size or something to make it look the way you wanted it to.
.container {width: 160px;height: 91px;}
.bg {
width: 100%;
height: 100%;
background: url(http://i60.tinypic.com/2j2fhjm.jpg) repeat-x left center;
background-size: 200%;
-webkit-animation: displace 2s linear infinite;
animation: displace 2s linear infinite;
}
#-webkit-keyframes displace {
from { background-position: left center;
} to { background-position: 200% center; }
}
#keyframes displace {
from { background-position: left center;
} to { background-position: 200% center; }
}
<div class="container"><textarea class="bg"></textarea></div>
The problem you have is with the height resizing and your different settings. To get it to work with your actual settings, the displace would have to be 2x the height of the image (since the image has a ration of 2:1). I don't think you can do only by css.
But you have different options. I'm not sure exactly which part of you display you want to keep, so here are three ways to get your translation working on resize. None have exactly your settings, because the ones you have make it hard (impossible?) to set the proper displacement. Since you're stretching the width depending on the height, it's hard to follow. Via JS it would be easy, but css has limited access to these values.
The solutions proposed keep the height constant so you can keep your displace value constant.
First one, the background-size is the size of the container in px.
Second solution, you resize textarea only on x axis.
Third you repeat on y axis and keep height and width fixed.
There are probably other workarounds that are maybe more suited to your specific needs.
.container {
width: 160px;
height: 80px;
margin: 10px;
}
.bg {
width: 100%;
height: 100%;
background: url(http://i60.tinypic.com/2j2fhjm.jpg) repeat-x left center;
background-size: 160px 80px;
padding: 0px;
-webkit-animation: displace 2s linear infinite;
animation: displace 2s linear infinite;
}
.bg2 {
width: 100%;
height: 100%;
background: url(http://i60.tinypic.com/2j2fhjm.jpg) repeat-x left center;
background-size: contain;
padding: 0px;
resize: horizontal;
-webkit-animation: displace 2s linear infinite;
animation: displace 2s linear infinite;
}
.bg3 {
width: 100%;
height: 100%;
background: url(http://i60.tinypic.com/2j2fhjm.jpg) repeat left center;
background-size: 160px 80px;
padding: 0px;
-webkit-animation: displace 2s linear infinite;
animation: displace 2s linear infinite;
}
#-webkit-keyframes displace {
from {
background-position: 0 center;
}
to {
background-position: 160px center;
}
}
#-keyframes displace {
from {
background-position: 0 center;
}
to {
background-position: 160px center;
}
}
<div class="container">
<textarea class="bg"></textarea>
</div>
<div class="container">
<textarea class="bg2"></textarea>
</div>
<div class="container">
<textarea class="bg3"></textarea>
</div>
This is similar to Ilpo's answer, except it doesn't resize the image (background-size: auto;). The animation is smooth throughout, regardless of container size.
The downside is that the width (200px) of the image needs to be known beforehand.
Since you said tilable, and since this image looks to be tilable in both directions, I also made it repeat in both dimensions. As a result, your textarea is filled with the tiled background image, animated to scroll horizontally. If you only want to tile in 1 direction, you can change repeat back to repeat-x and change the vertical position from top to whatever you need.
.container {width: 160px;height: 91px;}
.bg {
width: 100%;
height: 100%;
background: url(http://i60.tinypic.com/2j2fhjm.jpg) repeat left top;
background-size: auto;
-webkit-animation: displace 2s linear infinite;
animation: displace 2s linear infinite;
}
#-webkit-keyframes displace {
from { background-position: 0px top; }
to { background-position: 200px top; }
}
#keyframes displace {
from { background-position: 0px top; }
to { background-position: 200px top; }
}
<div class="container"><textarea class="bg"></textarea></div>
For each background-position: 200px bottom; you need to add 1second for example if you want add 2S, you add another 200px so you
make it background-position: 400px bottom; I think.
Here is the code:
.container {
width: 160px;
height: 91px;
}
.bg {
width: 100%;
height: 100%;
background: url(http://i60.tinypic.com/2j2fhjm.jpg) fixed;
-webkit-animation: displace 1s linear infinite;
animation: displace 1s linear infinite;
}
#-webkit-keyframes displace {
from { background-position: 0 bottom; }
to { background-position: 200px bottom; }
}
#keyframes displace {
from { background-position: 0 bottom; }
to { background-position: 200px bottom;}
}
<div class="container">
<textarea class="bg"></textarea>
</div>
I hope it works for your case, Let me know if you have any question.
.container {
border: 1px solid black;
display: inline-block;
background: url(http://i60.tinypic.com/2j2fhjm.jpg) repeat-x left center;
-webkit-animation: displace 2s linear infinite;
animation: displace 2s linear infinite;
background-size: 160px 100%;
}
.bg {
float: left;
border: 0;
margin: 10px;
width: 160px;
height: 91px;
background-color: rgba(255, 255, 255, .5);
}
#-webkit-keyframes displace {
from {
background-position: 0 center;
}
to {
background-position: 160px center;
}
}
#keyframes displace {
from {
background-position: 0 center;
}
to {
background-position: 160px center;
}
}
<div class="container">
<textarea class="bg"></textarea>
</div>

Resources