Follow Along as I Stumble on the Path to Learning RoR

Posts tagged “text on image

CSS Only Image Overlay

While this isn’t a Rails solution, it was one that was so helpful to me that I wanted to post about it.  I needed a tinted/filtered image overlay with text.  After Binging the internet, I found the solution courtesy of Koren Leslie Cohen.

You can read her post here to see a demo, but you wrap your image, like this:

  <div id=“outer-box”>
    <img src=“cartagena.jpg”>
    <div id=“inner-box”>
      <p>This is the overlay!</p>
    </div>
  </div>
and then add this CSS,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#outer-box {
  width: 300px;
  height: 194px;
  display: block;
  position: relative;
}
#outer-box img {
  width: 300px;
  height: auto;
}
#outer-box #inner-box {
  background: #FD8698;
  height: 100%;
  width: 100%;
  opacity: 0;
  top: 0;
  left: 0;
  position: absolute;
  padding: 0;
  transition: opacity .5s;
}
#outer-box #inner-box p {
  color: #fff;
  lineheight: 150px;
  fontfamily: ‘arial’;
  textalign: center;
}
#outer-box:hover #inner-box {
  opacity: .9;
  transition: opacity .5s;
}

This will show the overlay when the image is hovered over.  For me, I wanted to show the overlay without the hover, so I changed line 33 to be:

#outer-box #inner-box {

Advertisements