CSS Change Image on Hover
12/31/2020
Demo
Code
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Change Image on Hover</title>
<link rel="stylesheet" type="text/css" href="https://demo.plantpot.works/assets/css/normalize.css">
<link rel="stylesheet" href="https://use.typekit.net/opg3wle.css">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="container">
<div class="image"></div>
<h1>Change Image on Hover</h1>
</div>
</body>
</html>
CSS
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
background-color: #ffa562;
font-family: futura-pt, sans-serif;
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
#container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
height: 100vh;
}
.image {
display: flex;
justify-content: center;
align-items: center;
width: 200px;
height: 200px;
background-image: url(image_01.png);
background-position: center;
background-size: contain;
background-repeat: no-repeat;
}
.image:hover {
background-image: url(image_02.png);
background-position: center;
background-size: contain;
background-repeat: no-repeat;
}
h1 {
margin: 30px 0 0;
color: #3d3935;
font-size: 2.5rem;
text-align: center;
}
@media screen and (max-width: 480px) {
.image {
width: 100px;
height: 100px;
}
h1 {
margin-top: 20px;
font-size: 1.75rem;
}
}