Search code examples
htmlcssstylesopacity

Transparent div with text on image


I want to accomplish the following effect. (link)

The idea is that I want a transparent div on the top of the image and in this div i want to place a text with opacity: 1 (non-transparent)

So far I managed to make the div transparent, but all the text in it is also transparent and I don't want that.


Solution

  • This should help you out.

    HTML:

    <div class="image">
        <img src="images/anyimg.jpg" alt="" />
        <h2>Image text:<br />Image Text</h2>
    </div>
    

    CSS:

    .image {
        position: relative;
        margin-bottom: 20px;
        width: 100%;
    }
    h2 {
        position: absolute;
        top: 200px;
        left: 0;
        width: 100%;
    }
    h2 span {
        color: white;
        font: bold 24px/45px Helvetica, Sans-Serif;
        letter-spacing: -1px;
        background: rgb(0, 0, 0);
        background: rgba(0, 0, 0, 0.7);
        padding: 10px;
    }
    h2 span.spacer {
        padding: 0 2px;
        background: none;
    }