1

I’m creating a simple webpage for Halloween. I have a background JPG of a starry night sky with a moon in the center. Background 500px by 500px. Moon area would be roughly 100px by 100px center on the image. I have a gif I made of a witch that fly’s across the entire night sky of the background on hover (in front of the moon). Works fine.

I just want the hover area (mouse in/mouse out) to be the moon area itself instead of the whole night sky. The witch gif is triggered if you hover only over the moon but still fly across the whole width of the night sky background. Whenever I contain the hover area to just the moon, the gif is only visible in the moon area instead of the whole night sky. Can you have a gif area 500px by 500px but the hover area to trigger it is only 100px by 100px center? I’ve searched the site and didn’t quite find what I was looking for. Any help is appreciated. Thanks!

This is the simple working HTML code below:

.backgrounds {
  height: 500px;
  width: 500px;
  background-image: url('Background_moon.jpg');
}

.backgrounds:hover {
  cursor: pointer;
  background: url('flying_witch.gif'), url('Background_moon.jpg');
}
<div class="backgrounds"></div>
Calvin Nunes
  • 6,376
  • 4
  • 20
  • 48
Robert
  • 63
  • 6

1 Answers1

0

You can create a div for the moon area and another div for the witch inside the .backgrounds element. The witch element must be after the moon area element:

<div class="backgrounds">
    <div class="moon-area"></div>
    <div class="witch"></div>
</div>

First, let's centralize the .moon-area element.
You can centralize it in 2 different ways.

1) Flexbox: using on the parent display: flex, justify-content: center (horizontally) and aling-items: center (vertically), like this:

.backgrounds {
  background-image: url('Background_moon.jpg');
  height: 500px;
  width: 500px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.moon-area {
  width: 100px;
  height: 100px;
  z-index: 1;
}

2) Auto margin: using position: relative on the parent, and position: absolute on the .moon-area with some positioning (left, right, top, bottom) and margin: auto properties:

.backgrounds {
  background-image: url('Background_moon.jpg');
  height: 500px;
  width: 500px;
  position: relative;
}

.moon-area {
  width: 100px;
  height: 100px;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  z-index: 1;
}

We have also the .witch CSS:

.witch {
   position: absolute;
   left: 0;
   top: 0;
   height: 100%;
   width: 100%;
}

So, you can apply the hover effect on .moon-area element. We can use the very uself ~ selector to select the next sibling.

.moon-area:hover ~ .witch {
  cursor: pointer;
  background: url('flying_witch.gif'), url('Background_moon.jpg');
}

Also, don't forget to set z-index: 1 to the .moon-area element and position: relative to the .backgrounds element.

Azametzin
  • 5,223
  • 12
  • 28
  • 46
  • I appreciate the quick response. I tried both codes you gave. Unfortunately its giving me the same issue I have when I tried something similar to this earlier. The gif animation of the witch only appears within the bounds of the 100px by 100px hover area instead of the whole 500px by 500px background. Cant seem to have the gif size show larger than the hover size. its cropped to the hover area and you only get a glimpse of the witch flying within that hover box. Perhaps I'm missing a step? Again, thanks for the assistance... – Robert Aug 26 '19 at 17:06
  • Ah, I see. This is easy to solve. I edited the answer. Please, test it. I sorry about my lack of attention. Now I saw you commented about it in your question. Actually, it is on title. I feel silly. – Azametzin Aug 26 '19 at 17:34
  • That seemed to have worked! Thanks. I went ahead and added temporary border line to see where my hover is relative to the moon. My moon is not quite centered. I also may move the moon for artistic reasons as I go so I added Left and Top under ".moon-area". it did not budge the hover area. I noticed the Align items and justify-content on center so removed but that didn't help. How can I move the hover position around a bit? – Robert Aug 26 '19 at 19:07
  • Nice. So, you can remove `margin: auto`, bottom and right. Then you position it using only top and left, with percentage unit. For example: `top: 40%` and `left: 45%` till the position you want. Or you can use pixels as well, since you have fixed dimensions. – Azametzin Aug 26 '19 at 19:13
  • I couldn't get it to work at first until I added "position: absolute;". Now I can make finite adjustment to center the hover on moon. This will work! Now my only remaining bit is to add my "witch _laugh.mp3" under moon area rollover and I should be set... Thanks man! – Robert Aug 26 '19 at 19:59
  • You're right. I forgot `position: absolute` there (position: relative would have worked as well for `.moon-area` in this case). You're welcome, I'm glad it worked! – Azametzin Aug 26 '19 at 20:02
  • Do you happen know if there is another file format CSS can play besides GIF? Looking for something smoother like a movie clip. Just wondering. Thanks! – Robert Sep 04 '19 at 21:47
  • 1
    With transparent background and animated, there is only GIF to use. But another way I recommend would be to use a PNG, since it can also have a transparent background. and then animate it using `animation`. When hovering, an `animation` occurs. Inside animation keyframes you use `transform`, you know. Combining `translate`, `rotate`, and so on. Search for it, I think it's worth it. – Azametzin Sep 04 '19 at 22:56