简体   繁体   中英

Transparent Text Box - Image Overlay

How can you overlay text over an image with a background, like this

例

<div id="slider" class="nivoSlider">
<img src="images/slide1.jpg"/>

</div>

Like this http://codepen.io/anon/pen/jaBho

 <div>
  <h4>Lorem ipsum dolor sit amet, consectetur adipisicing elit. In, placeat!</h4>
  <img src="http://rack.0.mshcdn.com/media/ZgkyMDE0LzA4LzI0LzVkL21pbGV5ZG9nLmEzMDVjLmpwZwpwCXRodW1iCTk1MHg1MzQjCmUJanBn/1750ef37/ade/miley-dog.jpg">
</div>

css

div{position: relative;
  margin: 35px;
  width: 500px;
  height: 300px;
  overflow: hidden;
}

h4{
  background-color: rgba(174, 0, 0, 0.51);
  position: absolute;
  bottom: 30px;
  left: 30px;
  width: 300px;
  padding: 20px;
  display: block;
  color: white;
  font-size: 24px;
}

You mean something like this demo ?

Important things:

The parent div has to have position: relative and the text div position: absolute . Then you position it on the image with top: ?; left: ?; bottom: ? top: ?; left: ?; bottom: ? and text-align: center .

Here is the HTML :

<div id="slider">
    <img src="http://www.primolo.de/archiv/BrAlIbKe/hp_bilder/luftballons2_v-gallery_1_.jpg"/>
    <div id="slider-text">This is awesome</div>
</div>

And the CSS :

#slider {
  position: relative;
  width: 400px;
}

#slider-text {
  position: absolute;
  width: 200px;
  color: #FFF;
  font-weight: bold;
  bottom: 5%;
  left: 30px;
  padding: 30px 0;
  text-align: center;
  background-color: rgba(200, 50, 0, 0.8);
}

Most of the stuff was not necessary, but I think it looks pretty nice.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM