Amarnath Balasubramanian
Amarnath Balasubramanian

Reputation: 9460

Text with Transparent Background infront of an image

HTML


<article id="articlebottom">
    <div class="products">
        <div class="imgWrap">
            <img src="http://t0.gstatic.com/images?q=tbn:ANd9GcTA1uJ8WQ7fiJ2cbdoMph39XIJYQztt6FWoxfkk32gwnOz0qcmjyg" alt="candle" />
            <p class="imgDescription">Camphor</p>
        </div>
        <div class="imgWrap">
            <img src="http://t0.gstatic.com/images?q=tbn:ANd9GcTA1uJ8WQ7fiJ2cbdoMph39XIJYQztt6FWoxfkk32gwnOz0qcmjyg" alt="candle" />
            <p class="imgDescription">Camphor</p>
        </div>
        <div class="imgWrap">
            <img src="http://t0.gstatic.com/images?q=tbn:ANd9GcTA1uJ8WQ7fiJ2cbdoMph39XIJYQztt6FWoxfkk32gwnOz0qcmjyg" alt="candle" />
            <p class="imgDescription">Camphor</p>
        </div>
    </div>
</article>

CSS

#articlebottom {

    width: 980px;
    height: 300px;          
}

.products
{
    width:980px;
    margin:0px auto;
    padding-left:20px;


} 
#articlebottom .imgWrap img {
    margin:0px;
    padding:0px;
    width:295px;
    float:left;
    margin:10px;
    height:200px;
    border:5px solid #000;

}

#articlebottom .imgDescription {
    position: absolute;
    padding-top:35px;
    letter-spacing: 2px;
    background-color: rgba(255, 250, 250, 0.2);
    color: #1b9bff;
    font-weight:bold;
    font-size:18pt;
    width:310px;
    height:50px;
    opacity: 1;
    text-align:center;
    visibility: hidden;
    opacity: 100;
    text-transform:uppercase;
    /*-webkit-transition: visibility opacity 0.2s;*/
}
#articlebottom .imgWrap:hover .imgDescription {
    visibility: visible;

}

Fiddle

What I'm having:

  1. I have three images.
  2. The Text appears on the first image even when i hover on the second image..!

What i need:

  1. When i hover on the image the appropriate text shoould appear over the image with a transparent background.

Problem with my design:

The text appears only on the first image even when i hover on the second image..!!

Upvotes: 1

Views: 217

Answers (2)

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

I just Came up with @kougiland Solution, You need to set transparent background height to image height

CSS

  *{
    padding:0;
    margin:0;
}
#articlebottom {
    width: 980px;
    height: 300px;          
}

.products{
    width:980px;
    margin:0px auto;
    padding-left:20px;
} 
#articlebottom .imgWrap {
    width:295px;
    height:200px;
    position:relative;
    float:left;
    margin:10px;
    background:black;
}
#articlebottom .imgWrap img {
    position:absolute;
    left:5px;
    top:5px;
    width:285px;
    height:190px;
}

#articlebottom .imgDescription {
    position: absolute;
    left:0;
    letter-spacing: 2px;
    background-color: rgba(255, 250, 250, 0.2);
    color: rgba(255, 250, 250, 0.2);
    font-weight:bold;
    font-size:18pt;
    line-height: 50px;
    width:100%;
    height:25%;
    opacity: 0;
    text-align:center;
    text-transform:uppercase;
    transition:opacity 500ms ease-in-out, color 20ms ease-in-out,height 500ms ease-in-out;
}

#articlebottom .imgWrap:hover .imgDescription {
    opacity: 1;
    height:100%;
    color: #1b9bff;
}

Fiddle Demo

Upvotes: 0

Gildas.Tambo
Gildas.Tambo

Reputation: 22643

When you use position absolute make sure that the parent of that element has a relative position:

WORKING DEMO

*{/*reset*/
    padding:0;
    margin:0;
}
#articlebottom {
    width: 980px;
    height: 300px;          
}

.products{
    width:980px;
    margin:0px auto;
    padding-left:20px;
} 
#articlebottom .imgWrap {
    width:295px;
    height:200px;
    position:relative;/* set .imgWrap to relative to be able to use position absolute on childrens*/
    float:left;
    margin:10px;
    border:5px solid #000;
}
#articlebottom .imgWrap img {
    width:100%;
    height:100%;
}

#articlebottom .imgDescription {
    position: absolute;
    top:35px;
    left:0;
    letter-spacing: 2px;
    background-color: rgba(255, 250, 250, 0.2);
    color: #1b9bff;
    font-weight:bold;
    font-size:18pt;
    width:100%;
    height:50px;
    opacity: 1;
    text-align:center;
    visibility: hidden;
    opacity: 100;
    text-transform:uppercase;
    /*-webkit-transition: visibility opacity 0.2s;*/
}
#articlebottom .imgWrap:hover .imgDescription {
    visibility: visible;

}

UPDATE: DEMO

Upvotes: 3

Related Questions