HTML Change Background Image on Hover

HTML Change Background Image on Hover
Code Snippet:Change Background Image on Hover CSS Only
Author: nestor manuel
Published: April 17, 2024
Last Updated: April 17, 2024
Downloads: 73
License: MIT
Edit Code online: View on CodePen
Read More

This HTML code snippet helps you to change the background image on hover. It smoothly transitions between different images. This feature can be used inside web components to add interactive backgrounds.

The figure element initially displays a background image defined in the CSS. Upon hovering over the figure element, a transition effect triggers, smoothly transitioning between different background images defined in keyframe animations. The :hover pseudo-class and keyframe animations specify the sequence and timing of background image changes.

How to Change Background Image on Hover Using HTML and CSS

1. First, create the HTML structure for the element you want to apply the hover effect to. In this example, we’ll use a <div> with a <figure> inside:

<!-- figure is the same size as the image. all images are the same size. Having different sized images fit into the figure will cause warping to the image upon the transition. -->

<div class="wrapper">

2. Next, add CSS styling to define the appearance and behavior of the element and its background images. Here’s the CSS code:

/* fallback */
  background-color: #00563D;
  background-repeat: repeat-y;
  /* Safari 4-5, Chrome 1-9 */
  background: -webkit-gradient(linear, left top, right top, from(#00563D), to(#F7E611));

  /* Safari 5.1, Chrome 10+ */
  background: -webkit-linear-gradient(left, #F7E611, #00563D);

  /* Firefox 3.6+ */
  background: -moz-linear-gradient(left, #F7E611, #00563D);

  /* IE 10 */
  background: -ms-linear-gradient(left, #F7E611, #00563D);

  /* Opera 11.10+ */
  background: -o-linear-gradient(left, #F7E611, #00563D);


  border: 1px solid #ddd;


  width:  640px;
  height: 360px;
  background-image: url('');
  transition: all 1s ease-in-out;
  background-repeat: repeat-n;
  margin: 5px;

  -webkit-animation: switch 5s infinite; /* Chrome, Safari, Opera */ 
    animation: switch 5s infinite;
   background-repeat: repeat-n;

@-webkit-keyframes switch {
    0% {background-image: url('');}
    20% {background-image: url('');}
    40% {background-image: url('');}
    60% {background-image: url('');}
    80% {background-image: url('');}
    100% {background-image: url('');}

Replace the placeholder URLs in the CSS with your desired image URLs. You can also adjust the transition duration and timing function to suit your preference.

Experiment with different background images and transition effects to achieve the desired visual impact. You can also adjust other CSS properties to customize the appearance of the element further.

That’s all! hopefully, you have successfully created a functionality to change the background image on hover. If you have any questions or suggestions, feel free to comment below.

Leave a Comment