纯css实现九宫格图片

本篇文章所分享的内容主要涉及到结构伪类选择器,不熟悉的小伙伴可以了解一下,在常用的css选择器中我也有分享相关内容。

话不多说,接下来我们直接上代码:

复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    body {
      height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .box {
      width: 300px;
      height: 300px;
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
      position: relative;
    }
    .item {
      position: relative;
      transition: 0.5s;
      height: 100px;
      width: 100px;
      overflow: hidden;
      /* box-shadow: inset 0 0 0 1px #fff; */
      background-size: 300px 300px;
      background-image: url(./images/sc.jpg);
    }
    .item:nth-child(3n + 1) {
      background-position-x: 0;
      left: -20px;
    }
    .item:nth-child(3n + 2) {
      background-position-x: -100%;
      left: 0;
    }
    .item:nth-child(3n) {
      background-position-x: -200%;
      left: 20px;
    }
    .item:nth-child(n + 7) {
      background-position-y: -200%;
      top: 20px;
    }
    .item:nth-child(-n + 6) {
      background-position-y: -100px;
      top: 0;
    }
    .item:nth-child(-n + 3) {
      background-position-y: 0;
      top: -20px;
    }
    .box:hover .item {
      left: 0;
      top: 0;
    }
  </style>
  <body>
    <div class="box">
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
    </div>
  </body>
</html>

效果展示:

今日寄语:没有什么是不可能的,只是需要你去尝试!

相关推荐
滿2 分钟前
Vue3 父子组件表单滚动到校验错误的位置实现方法
前端·javascript·vue.js
夏梦春蝉1 小时前
ES6从入门到精通:模块化
前端·ecmascript·es6
拓端研究室2 小时前
视频讲解:门槛效应模型Threshold Effect分析数字金融指数与消费结构数据
前端·算法
工一木子3 小时前
URL时间戳参数深度解析:缓存破坏与前端优化的前世今生
前端·缓存
半点寒12W5 小时前
微信小程序实现路由拦截的方法
前端
某公司摸鱼前端5 小时前
uniapp socket 封装 (可拿去直接用)
前端·javascript·websocket·uni-app
要加油哦~5 小时前
vue | 插件 | 移动文件的插件 —— move-file-cli 插件 的安装与使用
前端·javascript·vue.js
小林学习编程6 小时前
Springboot + vue + uni-app小程序web端全套家具商场
前端·vue.js·spring boot
柳鲲鹏6 小时前
WINDOWS最快布署WEB服务器:apache2
服务器·前端·windows
weixin-a153003083167 小时前
【playwright篇】教程(十七)[html元素知识]
java·前端·html