Open In App

How to Create an Image Overlay Fade in Box Effect on Hover ?

Last Updated : 20 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The image overlay fade in box effect on hover is a visual effect where a box fades in over an image when the user hovers their mouse over the image. It can be used to display additional information about the image or simply add a stylish touch to your website. preview

Approach

  • Create an HTML document and create the image container element with the class image-overlay.
  • Add an image element with your image source, inside the image-overlay element.
  • Add another element inside the image-overlay element with overlay-box class which will be used for overlay and also add a element with overlay-text class to display the text.
  • Set CSS opacity property to 0 to hide the overlay-box and set opacity to 1 on hover. This will create the fade in effect on hover.
  • Add proper styling such as background color, text color, positioning etc. to the overlay-box and overlay-text element according to your need.

Example: The below example show an image overlay fade in box effect on hover using HTML and CSS.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>Image overlay fade in box effect on hover</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <div class="container">
        <h1>Geeks for Geeks</h1>
        <p>Image overlay fade in box effect on hover</p>
        <div class="image-overlay">
            <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20230913133830/GFG.jpg" 
                 alt="Image">
            <div class="overlay-box">
                <p class="overlay-text">GFG logo</p>
            </div>
        </div>
    </div>
</body>

</html>
CSS
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    min-height: 100vh;
    background-color: rgb(111, 201, 111);
}

.container {
    width: 100%;
    height: 100vh;

    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.container>h1 {
    padding: .5em;
    border-radius: 1.5em;

    font-size: 3rem;

    background-color: green;
    color: white;
}

.container>p {
    margin: 1em 0;

    font-size: 2rem;

    color: white;
}


.image-overlay {
    position: relative;
    outline: 5px solid green;
}

.image-overlay img {
    width: 100%;
    height: 100%;
}

.overlay-box {
    position: absolute;
    inset: 0;

    background-color: rgba(0, 0, 0, 0.801);

    opacity: 0;
    transition: opacity 0.5s ease-in-out;
}

.image-overlay .overlay-box:hover {
    opacity: 1;
}

.overlay-text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

    font-size: 2rem;

    color: rgb(255, 255, 255);
}

Output:

hover



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads