CSS Image Overlay Text on Hover

In this tutorial, I will create an Image Overlay Text on hover with the help of CSS Only. Also, The CSS image over the text block responsive for small devices. The CSS overly is a way of adding opacity color background over an image. This opacity can be 50% which make the picture darkens a little bit.

And then to add a readable white text or read more button over an image. The purpose of doing this to provide a user to quickly access the piece of content on the photo.

Just to make sure — By default — The content or button over the picture is hidden and can only be accessed when a user hovers the image.

There are different ways to create the color overlay for background image and here I will use one of them. All of these methods work with CSS only but the techniques are different.

These techniques are:

  • Using pseudo-element :after & :before
  • Using Opacity & Background Properties.
  • Positioned Element Absolutely.

All of the techniques are good to use but I personally like to use pseudo-element because it’s working well on all kind of browsers and devices.

The second method opacity & the background is also a great way to create dark overlays, but sometimes people don’t want to use an image as background in CSS. so this may not be useful for those people.

The third technique positioned element is also good and works well but It’s your preference you want to use it and can handle on mobile screens.

HTML for Image Overlay Text

Let’s get started to create the effect. We have a div and place an image inside it. We also wrap the image with an anchor link with the class name pseudover.

<div class="overlaythree">
    <a href="#" class="pseudover v2" title="Find out more">
        <img src="img/photo1.jpg" alt="overlay">
    </a>
</div>

CSS for Overlay Text

Now all we need to styling. We will specify the width and height of the container and for the link, I will set its position and add display property.

.overlaythree {
   width:480px;
   height: 360px;
   display: inline-block;
}
.pseudover {
  position: relative;
  display: inline-block;
}

Handling :after

To show the text on the image, We will define the content attribute with the help of CSS :after for overlay. On-hover, We will show it.

.pseudover:after {
  content: attr(title);
  color: rgba(255, 255, 255, 0.75);
  padding: .5em 1em;
  font-family: helvetica;
  position: absolute;
  top: 0;
  right: 0;
  transition: all .25s ease-in-out;
}
.pseudover:hover:after {
  background: rgba(0, 182, 221, 0.8);
  color: #fff;
}

Implement :Before

With the help of :before, We will show the title on hover and position it center of div.

.pseudover.v2:after {
  content: "";
  width: 100%;
  height: 100%;
  box-sizing: border-box;
}
.pseudover.v2:hover:before {
  content: attr(title);
  position: absolute;
  margin-top: -.5em;
  top: 50%;
  left: 0;
  width: 100%;
  color: #fff;
  z-index: 2;
  text-align: center;
  font-family: helvetica;
}

I highly encourage you to play around with the transitions, exploring different methods and techniques.

Feel free to leave any comments or feedback below! Don’t forget to share this with your friends to support us!

You Might Be Interested In:

Ashfaq Ahmed is a freelance WordPress developer and owner of codeconvey.com. I developed modern and highly interactive websites. I always focus on improving my development and design skills.

4 thoughts on “CSS Image Overlay Text on Hover”

  1. Very nice. But if I want to omit the text and have just a color overlay, will this work?

    .pseudover:after {
    color: rgba(255, 255, 255, 0.75);
    transition: all .25s ease-in-out;
    }
    .pseudover:hover:after {
    background: rgba(0, 182, 221, 0.8);
    }

    • Hi Chas Devlin!
      Thanks for the appreciation. Yes it will work, you just need to remove attr(title) in content property.
      Replace the content: attr(title); with content: ""; in order to display only color overly.

  2. Thanks much for this Muhammad Asif!
    May i ask you to convert the commands to html? Its because i’m not used to CSS. Many thanks 🙂

    • Hi there!
      CSS is necessary to style the HTML elements. You can’t style anything without using CSS. Anyhow, you can use inline CSS styles inside HTML tag attributes to style a specific element. Like:

      <div style="width: 100px; height: 100px; background: red; color: white; line-height: 100px; text-align: center"> THIS IS HTML BOX </div>
      

      However, it’s good practice to keep your CSS styles in a separate file.

Comments are closed.