0

I'm trying to make a div appear over an image using CSS "hover" event. But it's not working at all. Nothing happens at all... could you check what's going on? Thank you.

.over {

        display: none;
        opacity: 0.6;
        background: #F58634;
        font-family: Montserrat;
        color: white;
        padding: 20px;
        width: 171px;
        height: 114px;
        z-index: 2;

    }

    .over:hover {

        display: block;

    }

    .img {

        z-index: 1;

    }

And here is the HTML...

<td width="171px" >
                <img class="img" src="./img/cat/_MG_7415.JPG" alt="" width="171px" height="114px">

                <div class="over">
                    <table>
                        <tr>
                            <td>PESO</td>
                            <td>0,500gr</td>
                        </tr>
                        <tr>
                            <td>VALIDADE</td>
                            <td>60 DIAS<br><span style="font-size:5px;">APÓS DATA DE FABRICAÇÃO</span></td>
                        </tr>
                        <tr>
                            <td>CAIXA</td>
                            <td>5kg</td>
                        </tr>
                        <tr>
                            <td>TEMPERATURA</td>
                            <td>0 a 5ºC</td>
                        </tr>
                    </table>
                </div>

            </td>

So, I want the div class ".over" to overlay ".img" to show some information mouse is on the image. Is there anything I'm missing?

Thanks!

4
  • Are you using jQuery in your project? Commented Apr 5, 2016 at 0:01
  • Yes. Does it affect? Commented Apr 5, 2016 at 0:03
  • display:none doesn't display the element, so you can't hover over it. Use visibility instead Commented Apr 5, 2016 at 0:04
  • Ah, I see... Thanks. Commented Apr 5, 2016 at 0:05

4 Answers 4

2

.over {
        display:none;
        opacity: 0.6;
        background: #F58634;
        font-family: Montserrat;
        color: white;
        padding: 20px;
        width: 171px;
        height: 114px;
        z-index: 2;
    }
    img.img:hover + .over {
        display:block;
    }
<td width="171px" >
                <img class="img" src="./img/cat/_MG_7415.JPG" alt="" width="171px" height="114px">

                <div class="over">
                    <table>
                        <tr>
                            <td>PESO</td>
                            <td>0,500gr</td>
                        </tr>
                        <tr>
                            <td>VALIDADE</td>
                            <td>60 DIAS<br><span style="font-size:5px;">APÓS DATA DE FABRICAÇÃO</span></td>
                        </tr>
                        <tr>
                            <td>CAIXA</td>
                            <td>5kg</td>
                        </tr>
                        <tr>
                            <td>TEMPERATURA</td>
                            <td>0 a 5ºC</td>
                        </tr>
                    </table>
                </div>

            </td>

CSS solution: Use + selector to select the next element.

.over {
    display:none;
    opacity: 0.6;
    background: #F58634;
    font-family: Montserrat;
    color: white;
    padding: 20px;
    width: 171px;
    height: 114px;
    z-index: 2;
}
img.img:hover + .over {
    display:block;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Here's the solution with jQuery

<script>
    $(function() {
        $('img.img').hover(function() {
            $('div.over').show();
        }, function() {
            $('div.over').hide();
        });
    );
</script>

In this case you will no longer need this:

.over:hover {

    display: block;

}

Comments

1

The div has display:none so it's not being displayed and it is therefore impossible to hover over it.

Try changing it to display:block and instead changing the opacity in the hover.

As mentioned by Jacob Gray in a comment to your post, you can use visibility. So, instead of changing the opacity in the hover, you can set that once and then set visibility initially to hidden and then to visible during the hover.

1 Comment

I thought the same thing. But the OP wants to display the .over on hovering on the previous image. Not the .over itself.
1

https://jsfiddle.net/ahmadasjad/52mmdxyb/

Dear, You have defined hover event on .over whereas it's already hidden. When it's not available, how we can mouse over on that element! What you need is that hover on image then show the content in the div. I have done accordingly. Please check jsfiddle.

Removed:

.over:hover {

        display: block;

    }

And changed it to

.img:hover +  .over {
        display: block;
    }

it means: if mouse over on image, please show content of div having class over

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.