How to use Slide Overlay Effect In CSS
- Create an HTML document with a container div for the image and overlay.
- Place an image inside the container.
- Define styles for the body, header, and image container and Set dimensions and positioning for the image.
- Implement a sliding overlay effect using CSS pseudo-elements.Initially position the overlay off-screen to the left also utilize transitions for smooth animation.
- Trigger the overlay animation on hover using the
:hover
pseudo-class. - Transition the overlay from off-screen to cover a portion of the image.
Example : The example below shows how to create overlay effects by the slide overlay.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Hover Slide Overlay</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f0f0f0;
}
h1 {
font-family: Arial, sans-serif;
text-align: center;
color: rgb(23, 214, 102);
margin-bottom: 20px;
}
.slide-overlay-container {
position: relative;
display: inline-block;
overflow: hidden;
}
.slide-overlay-container img {
display: block;
width: 400px;
height: auto;
margin: 0 auto;
}
.slide-overlay::before {
content: "";
position: absolute;
top: 0;
left: -100%;
width: 50%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
transition: left 0.3s ease;
}
.slide-overlay-container:hover .slide-overlay::before {
left: 0;
}
</style>
</head>
<body>
<h1>Hover Slide Overlay</h1>
<div class="slide-overlay-container">
<img src=
"https://media.w3wiki.org/wp-content/uploads/20240514123440/image.jpg"
alt="DevOps Roadmap">
<div class="slide-overlay"></div>
</div>
</body>
</html>
Output:
How to Create Different Overlay Effects using CSS?
Overlay effects are a powerful way to enhance the visual appeal and user experience of a website. With CSS, creating various overlay effects can be achieved relatively easily, offering designers a plethora of creative possibilities.
Below are the approaches to create different overlay effects using CSS:
Table of Content
- Overlay with Color
- Using Slide Overlay