0

I’ve been playing around with html, css and JS lately. Right now I have a div that has a background gif. This is what I did:

<div class = "mydiv" style = "background-image: url(play.gif);"></div>

So first, it fills the entire space by repeating the image over and over side by side. I was wondering if there was a way to only have the image show up once and at the center of the div.

Another question I have is if it is possible to “pause” the gif at the beginning and only play it when the user hovers over the div and when they hover-off the gif goes back and stays at the beginning. If this is not possible for gifs, is it possible for videos (.mp4 .webm etc.)

Allen Huang
  • 346
  • 2
  • 13

2 Answers2

1

To the first part of your question, yes you can have it show up once in the center. Here is an example.

.mydiv{width:100%;
      height:100vh;
  background-image:url(http://bestanimations.com/Animals/Birds/Penguins/animated-penguin-gif-5.gif);
  background-repeat:no-repeat;
  background-position: center center;}
<div class="mydiv">
</div>
Rachel Gallen
  • 25,819
  • 19
  • 69
  • 75
0

You could use something like this:

.mydiv {
    background-image: url(static.gif);
    background-repeat: no-repeat;
    background-position: center;
}

.mydiv:hover {
    background-image: url(play.gif);
}

where static.gif would be just the first frame from your gif.

Schlaus
  • 15,686
  • 10
  • 31
  • 62