Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

transparent div over an image but opaque text

Tags:

html

css

I have the following HTML code which simply shows an image with a transparent black overlay containing text.

I don't wan't my text to be transparent. I tried with z-index, but my text is still transparent:

Demo

What's wrong with my code?

This is my HTML:

<div class="leftContainer">
    <div class = "promo">
         <img src="images/soon.png" width="415" height="200" alt="soon event" />

         <div class="hilight">
             <h2>Hockey</h2>
             <p>Sample text</p>
         </div>

     </div>

     ...

</div>

and this is my css:

.hilight h2{
    font-family: Helvetica, Verdana;
    color: #FFF;
    z-index: 200;
}

.promo {
    position: relative;
}
.promo img {
    z-index: 1;
}

.hilight {
    background-color: #000;
    position: absolute;
    height: 85px;
    width: 415px;
    opacity: 0.65;
    font-family: Verdana, Geneva, sans-serif;
    color: #FFF;
    bottom: 0px;
    z-index: 1;
}
like image 459
Pier-Alexandre Bouchard Avatar asked May 21 '13 20:05

Pier-Alexandre Bouchard


People also ask

How do I change the opacity of the background image but not the text?

The percentage of opacity is calculated as Opacity% = Opacity * 100 To set the opacity only to the background and not the text inside it. It can be set by using the RGBA color values instead of the opacity property because using the opacity property can make the text inside it fully transparent element.

How do you make text transparent on a picture?

For having a simple, transparent text on an image, you need to have your image or background image in a <div> element, and the content block in another <div>. Then, use the CSS opacity property to set a transparent image or text. The opacity property is used to adjust the transparency of a text or photo.

How do you make a Div completely transparent?

filter: alpha(opacity=90); opacity: . 9; to make the DIV transparent, but when I add another DIV inside this DIV it makes it transparent also.

How do I make text transparent with an image in CSS?

To set the opacity of a background, image, text, or other element, you can use the CSS opacity property. Values for this property range from 0 to 1. If you set the property to 0, the styled element will be completely transparent (ie. invisible).


2 Answers

change the background of .hilight to rgba(0,0,0,0.65) and remove the opacity.

.hilight {
 background-color: rgba(0,0,0,0.65);
 position: absolute;
 height: 85px;
 width: 415px;
 font-family: Verdana, Geneva, sans-serif;
 color: #FFF;
 bottom: 0px;
 z-index: 1;
}
like image 132
Kerim Incedayi Avatar answered Sep 16 '22 20:09

Kerim Incedayi


You need to set the opacity to the background only, not the entire div and it's contents. You can do this with rgba color selection eg

div {
   background: rgba(0,0,0,0.50);
}

The other way of doing it would be to use a semi-transparent png image with some background-position. This would then be multibrowser compatible

like image 33
Kevin Lynch Avatar answered Sep 19 '22 20:09

Kevin Lynch