Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display <div> or <span> over image on :hover

I'm looking for a method that would allow a div or span element to appear over a image when you :hover over that image. I'm able to do this using the .image:hover ~ .overlay, but it's not exactly what I'm looking for. The div or span element needs to take the dimensions of the images, since there will be multiple sizes.

Example

<img width="200" height="200"/> would allow you to :hover changing the div or span element from display: none to display: block (doesn't necessarily need to be a block). The element that's being changed from invisible to visible would have to automatically detect the size of the image and match the size of the element to these same dimensions (200x200). However, I could also have a <img width="300" height="400"/> that would need the element to match the size (300x400).

I'm also looking for a super easy way for these elements to be positioned perfectly over the images.

Here's my code pen to show what I've got so far.

like image 533
Matthew Beckman Avatar asked Jan 27 '14 01:01

Matthew Beckman


People also ask

How do you make a div visible on hover?

To display div element using CSS on hover a tag: First, set the div element invisible i.e display:none;. By using the adjacent sibling selector and hover on a tag to display the div element.

How do I display an image in a div?

The key is the container has to be positioned relative and the tag positioned absolute. Show activity on this post. You need to set relative positioning on the container and then absolute on the inner tag div. The inner tag's absolute positioning will be with respect to the outer relatively positioned div.


1 Answers

Similarly to this answer I gave, you could absolutely position the .overlay element relative to the parent element and give it a height and width of 100% this should work for all img sizes given that the parent element will be inline-block (it's the same size as its containing elements).

EXAMPLE HERE

HTML Structure:

<div class="image">
    <img src="..." />
    <div class="overlay">The content to be displayed on :hover</div>
</div>

General CSS:

Hide the .overlay element by default, and use the selector .image:hover .overlay to change the styling on hover. Due to the HTML structure, this works well because .overlay is a descendant element. You can therefore avoid using the the general/adjacent sibling combinators +, ~. Box-sizing is used to calculate the element's dimensions with respect to border, padding.. etc.

.image {
    position:relative;
    display:inline-block;
}
.overlay {
    display:none;
}
.image:hover .overlay {
    width:100%;
    height:100%;
    background:rgba(0,0,0,.5);
    position:absolute;
    top:0;
    left:0;
    display:inline-block;
    -webkit-box-sizing:border-box;
    -moz-box-sizing:border-box;
    box-sizing:border-box;

    /* All other styling - see example */

    img {
        vertical-align:top; /* Default is baseline, this fixes a common alignment issue */
    }
}
like image 56
Josh Crozier Avatar answered Sep 20 '22 18:09

Josh Crozier