Home >Web Front-end >CSS Tutorial >How to Create an Image Overlay with CSS?
When displaying multiple images on a webpage, you may want to provide additional information or functionality when hovering over them. One way to achieve this is by adding an overlay layer containing text, icons, or other elements. In this article, we will explore how to create an image overlay using CSS.
To create an overlay layer, you can utilize the following CSS:
<code class="css">.image-container { position: relative; width: 200px; height: 300px; } .image-container .overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: none; color: #FFF; } .image-container:hover .overlay { display: block; background: rgba(0, 0, 0, .6); }</code>
In the above CSS, we define the image-container class to position the image and its overlay absolutely. Within this container, the overlay class defines the overlay layer's position and visibility. When the mouse hovers over the image container, the display property changes to block, revealing the overlay.
To incorporate the overlay into your HTML, use the following code:
<code class="html"><div class="image-container"> <img src="image.jpg"> <div class="overlay">This is the overlay content.</div> </div></code>
The CSS provided serves as a foundation, but you can customize the overlay to fit your design needs. For example, you can:
Creating an image overlay using CSS is a straightforward process that allows you to add interactivity and information to your images. By understanding the fundamental principles outlined in this article, you can implement overlays to enhance the user experience on your website.
The above is the detailed content of How to Create an Image Overlay with CSS?. For more information, please follow other related articles on the PHP Chinese website!