效果
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>
<title> Document </title>
<link type="text/css" rel="styleSheet" href="index.css" />
</head>
<body>
<div class="img-container">
<div class="img-item"></div>
<div class="img-item"></div>
<div class="img-item"></div>
<div class="img-item"></div>
<div class="img-item"></div>
<div class="img-item"></div>
<div class="img-item"></div>
<div class="img-item"></div>
<div class="img-item"></div>
</div>
</body>
</html>
index.css
body{
width: 500px;
height: 500px;
margin: 0 auto;
margin-top: 120px;
display: flex;
justify-content: center;
align-items: start;
background-color: #171717;
padding-top: 100px;
}
.img-container{
width: 300px;
height: 300px;
display: grid;
grid-template-columns: repeat(3,1fr);
grid-template-rows: repeat(3,1fr);
}
.img-item {
box-shadow: inset 0 0 0 1px #fff;
transition: 0.5s;
background-size: 300px 300px;
background-image: url(./1.png);
position: relative;
}
.img-item:nth-child(3n+1){
background-position-x: 0;
left: -20px;
}
.img-item:nth-child(3n + 2){
background-position-x: -100%;
left: 0;
}
.img-item:nth-child(3n){
background-position-x: -200%;
left: 20px;
}
.img-item:nth-child(n + 7){
background-position-y: -200%;
top: 20px;
}
.img-item:nth-child(-n + 6){
background-position-y: -100%;
top: 0px;
}
.img-item:nth-child(-n+3){
background-position-y: 0;
top: -20px;
}
.img-container:hover .img-item {
left: 0;
top: 0;
box-shadow: inset 0 0 0 0px #fff;
}
1.png