All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

CSS Hover Effects Image

Last Updated : Mar 27, 2024

CSS Hover Effects Image

In this article we will show you the solution of CSS hover effects image, in CSS, we can apply a hover effect in the image when the user hovers over an image and the effects of the changes on an image.

Changes like a filter, height, width etc. will reflect after the user hovers the mouse in an image.

Step By Step Guide On CSS Hover Effects Image :-

Apply hover effects in image

<html>
<head>
  <title>Talkerscode</title>
  <style>
    .myimg1:hover{
      height:500px;
      width:500px;
      clip-path: circle(50% at center);
      filter: blur(3px);
    }
    .myimg2:hover{
      height:500px;
      width:500px;
      filter: brightness(200%);
    }
    .myimg3:hover{
      height:500px;
      width:500px;
      opacity: 1;
    }
    .myimg4:hover{
      height:500px;
      width:500px;
      filter: blur(5px);
    }
</style>
</head>
<body>
  <img src="demo1.jpg" class="myimg1">
  <img src="demo2.jpg" class="myimg2">
  <img src="demo3.jpg" class="myimg3">
  <img src="demo4.jpg" class="myimg4">
 </body>
</html>
  1. Within the Style tag, we write our CSS code.
  2. Within the myimg1 class, we can assign a hover which means the user hovers the mouse over an image the changes will affect and we assign the height and width of the image.
  3. Within myimg1 class we also assign a clip path as a circle that represents when the user hovers the mouse over the image shape is 50% circle and uses the filter as a blur at 3px so that the image will be blur.
  4. Within the myimg2 class, we can assign a hover which means the user hovers the mouse over an image the changes will affect and we assign the height and width of the image.
  5. Within myimg2 class, we can assign a filter as brightness that represents the image as bright.
  6. Within myimg3 class, we can assign a hover which means the user hovers the mouse over an image the changes will affect we assign the height and width of the image and we assign opacity as 1 which represents the image is completely visible.
  7. Within myimg4 class, we can assign a hover which means the user hovers the mouse over an image the changes will affect and we assign the height and width of the image and we assign a filter as blur it representing the image as blur
  8. Within the body, we can use img src and assign a URL of the image with class myimg1,myimg2,myimg3, and myimg4.

Example 2

<html>
<head>
  <title>Talkerscode</title>
  <style>
    .res {
      position: relative;
      display: inline-block;
    }
    .myimg {
      opacity: 1;
    }
    .color {
      display: none;
      position: absolute;
      top:0;
      left:0;
      width: 100%;
      height: 100%;
      background-color: transparent;
      opacity: 0;
    }
    .res:hover .color {
      display: block;
      opacity: 1;
      background-color: green;
    }
    .res:hover .myimg {
      opacity: 1;
    }
  </style>
</head>
<body>
  <h1>My image</h1>
  <div class="res">
    <img src="demo.jpg" class="myimg">
    <div class="color"></div>
  </div>
</body>
</html>
  1. Within the res class, we assign a position relative that sets the left, top, and bottom properties which means it adjusts the position and assigns the display as an inline-block that set the properties of res such as height, width, etc..
  2. Within myimg class, we assign an opacity 1 which means the image will be fully visible.
  3. Within the color class, we can assign display as none which means initially the color will not display when the user hovers the mouse over the image then color will display.
  4. Within the color class, we can assign position as absolute which fixes the position of the element according to top, bottom, and left. right, margin, etc.
  5. Within the color class, we assign the top as 0, the left as 0, and the height and width will be 100%. And assign the background color as transparent and opacity as 0 which means initially there is no color that will see and opacity 0 indicates that the background color is not visible it is transparent.
  6. Then we can apply Styling in the color class when the res element is hovered.
  7. We can assign opacity as 1 which means the color will be completely visible.
  8. We can assign the background color as green.
  9. Then we can apply Styling in myimg class when the res element has hovered and assign opacity as 1 so that the image will be completely visible.
  10. Within the body, we can assign div class res.
  11. Then we can assign an image URL.
  12. Then we can assign a div class color.
  13. Finally the output represents when the user hovers the mouse over the image then the background color green is displayed in place of the image.

Conclusion :-

In conclusion, we successfully learned how to apply the hover effect in images. We learned two code examples that easily represent the use of the hover effect in images.

I hope this article on CSS hover effects image helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪