Alex Timuş
Alex Timuş

Reputation: 41

Text hover over an image

How can I make a text hover over a image? I want the text box to apear exactly over where the image is positioned, so that the image dissapears completly, and on mouse out the image reapears. I searched everywere but I only found hover effects with different positionig of the hoverbox from where the image is situated...

Upvotes: 0

Views: 6474

Answers (3)

Piotr Elmanowski
Piotr Elmanowski

Reputation: 69

You don't need JavaScript code to do that. In pure html & css it will work well. Below is example with css animation with opacity change.

html


    <div class="hvrbox">
        <img src="https://upload.wikimedia.org/wikipedia/commons/2/22/Bochnia_poland_saltmine.jpg" alt="Salt mine" class="hvrbox-layer_bottom">
        <div class="hvrbox-layer_top">
            <div class="hvrbox-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce porttitor ligula porttitor, lacinia sapien non.</div>
        </div>
    </div>

css


    .hvrbox,
    .hvrbox * {
        box-sizing: border-box;
    }
    .hvrbox {
        position: relative;
        display: inline-block;
        overflow: hidden;
        max-width: 100%;
        height: auto;
    }
    .hvrbox img {
        max-width: 100%;
    }
    .hvrbox .hvrbox-layer_bottom {
        display: block;
    }
    .hvrbox .hvrbox-layer_top {
        opacity: 0;
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.6);
        color: #fff;
        padding: 15px;
        -moz-transition: all 0.4s ease-in-out 0s;
        -webkit-transition: all 0.4s ease-in-out 0s;
        -ms-transition: all 0.4s ease-in-out 0s;
        transition: all 0.4s ease-in-out 0s;
    }
    .hvrbox:hover .hvrbox-layer_top,
    .hvrbox.active .hvrbox-layer_top {
        opacity: 1;
    }
    .hvrbox .hvrbox-text {
        text-align: center;
        font-size: 18px;
        display: inline-block;
        position: absolute;
        top: 50%;
        left: 50%;
        -moz-transform: translate(-50%, -50%);
        -webkit-transform: translate(-50%, -50%);
        -ms-transform: translate(-50%, -50%);
        transform: translate(-50%, -50%);
    }
    .hvrbox .hvrbox-text_mobile {
        font-size: 15px;
        border-top: 1px solid rgb(179, 179, 179); /* for old browsers */
        border-top: 1px solid rgba(179, 179, 179, 0.7);
        margin-top: 5px;
        padding-top: 2px;
        display: none;
    }
    .hvrbox.active .hvrbox-text_mobile {
        display: block;
    }

I described it with many similar examples (with better animations) here http://goo.gl/EECjCm

Upvotes: 0

inorganik
inorganik

Reputation: 25515

css:

.textHover {
    display:none;
    width:100%;
    height:100%;
    position:absolute;
    top:0; left:0;
    text-align:center;
    color:white;
}
.imgContain {
    position:relative;
    display:table;
}
.imgContain:hover .textHover {
    display:block;
}

markup:

<div class="imgContain">
    <img src="http://placehold.it/300x200"/>
    <div class="textHover">My text here</div>
</div>

http://jsfiddle.net/EACxV/

Upvotes: 1

James Donnelly
James Donnelly

Reputation: 128771

No need for JavaScript, unless you're wanting some smooth transition without relying on CSS3. Assuming the image has fixed dimensions, you'd do something like this:

<div>
    <p>Text</p>
    <img src="" alt="" width="100px" height="100px" />
</div>

div { position:relative; z-index:1; height:100px; width:100px;  }
img { position:absolute; top:0; left:0; z-index:2; }
div:hover img { display:none; }

JSFiddle.

Upvotes: 1

Related Questions