Moving Background image using CSS

26 / Jul / 2016 by Poonam Baveja 0 comments

CSS describes how html elements should be render on screen. We can move the background image using CSS3 animation property that gives an illusion of running video. CSS3 animation is supported by all modern browsers.
An animation lets an html element gradually change from one style to other. You need to specify keyframe to use animation. As shown below-

@keyframes backgroundScroll {
	from {background-position: 0 0;}
	to {background-position: -180px 0px;}
}

In order to support animation in different browsers you must mention vender prefix of that browser with keyframe.

@-webkit-keyframes backgroundScroll {
	from {background-position: 0 0;}
	to {background-position: -180px 0px;}
}

Now call this keyframe style in your css-

body  {
    color:#fff;
    text-align:center;
    background-image: url('https://s32.postimg.org/kkh3jlm1h/image.jpg');
    background-repeat: repeat;
    background-attachment: fixed;
    background-size: cover;
    font-size: 12px;
    font-family: 'Dancing Script', cursive;
    -webkit-animation: backgroundScroll 15s linear 1;
    -webkit-animation-fill-mode: forwards;
    animation: backgroundScroll 15s linear 1;
    animation-fill-mode: forwards;
}

Explanation of CSS3 animation-
a) “backgroundScroll” is name of keyframe style or animation name.
b) “15s” defines the animation duration.
c) “linear” is animation timing function. Its value can be ease, ease-in, ease-out or ease-in-out.
d) “1” is the value of animation iteration count. Its value can be infinite as per requirement.
e) “animation-fill-mode: forwards” prevents background image to return back to its original position after completing its specified animation iteration count.

Live Demo : https://jsfiddle.net/poonam_baveja/dnt7vdxo/1/

FOUND THIS USEFUL? SHARE IT

Leave a comment -