css实现九宫格

1.grid布局

<!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>
    <style>
      .wrapper{
          width: 450px;
          background: #f3f3f3;
          text-align: center;
          display: grid;
          grid-template-columns: 150px 150px 150px;
          grid-template-rows:150px 150px 150px ;
      }
      .item{
          text-align: center;
          border: 1px solid #fff;
          color: #fff;
          font-weight: bold;
          line-height: 150px;
      }
      .item:nth-of-type(1){
          background: #00a0a0;
      }
      .item:nth-of-type(2){
          background: #00a0a0;
      }
      .item:nth-of-type(3){
          background: #a0a0ff;
      }
      .item:nth-of-type(6){
          background: #00a0a0;
      }
      .item:nth-of-type(5){
          background: #a0a0ff;
      }
      .item:nth-of-type(4){
          background: #00a0a0;
      }
      .item:nth-of-type(7){
          background: #a0a0ff;
      }
      .item:nth-of-type(9){
          background: #00a0a0;
      }
      .item:nth-of-type(8){
          background: #a0a0ff;
      }
    </style>
    <script src="./index.js"></script>

</head>
<body>
    <div class="wrapper">
        <div class="item">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
        <div class="item">4</div>
        <div class="item">5</div>
        <div class="item">6</div>
        <div class="item">7</div>
        <div class="item">8</div>
        <div class="item">9</div>
    </div>
</body>
</html>

2.flex

<!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>
    <style>
      .wrapper{
          width: 450px;
          background: #f3f3f3;
          text-align: center;
            display: flex;
            justify-content: center;
            flex-wrap: wrap;

      }
      .item{
          width: 150px;
          text-align: center;
          border: 1px solid #fff;
          color: #fff;
          font-weight: bold;
          line-height: 150px;
          box-sizing: border-box;
      }
      .item:nth-of-type(1){
          background: #00a0a0;
      }
      .item:nth-of-type(2){
          background: #00a0a0;
      }
      .item:nth-of-type(3){
          background: #a0a0ff;
      }
      .item:nth-of-type(6){
          background: #00a0a0;
      }
      .item:nth-of-type(5){
          background: #a0a0ff;
      }
      .item:nth-of-type(4){
          background: #00a0a0;
      }
      .item:nth-of-type(7){
          background: #a0a0ff;
      }
      .item:nth-of-type(9){
          background: #00a0a0;
      }
      .item:nth-of-type(8){
          background: #a0a0ff;
      }
    </style>
    <script src="./index.js"></script>

</head>
<body>
    <div class="wrapper">
        <div class="item">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
        <div class="item">4</div>
        <div class="item">5</div>
        <div class="item">6</div>
        <div class="item">7</div>
        <div class="item">8</div>
        <div class="item">9</div>
    </div>a
</body>
</html>

3.float

<!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>
    <style>
      .wrapper{
          width: 450px;
          height: 450px;
          
          background: #f3f3f3;
          text-align: center;
          overflow: hidden;
        box-sizing: border-box;
      }
      .item{
          width: 150px;
          text-align: center;
          border-top: 1px solid #fff;
          border-right: 1px solid #fff;
          color: #fff;
          font-weight: bold;
          line-height: 150px;
          box-sizing: border-box;
          float: left;
      }
      .item:nth-of-type(1){
          background: #00a0a0;
      }
      .item:nth-of-type(2){
          background: #00a0a0;
      }
      .item:nth-of-type(3){
          background: #a0a0ff;
      }
      .item:nth-of-type(6){
          background: #00a0a0;
      }
      .item:nth-of-type(5){
          background: #a0a0ff;
      }
      .item:nth-of-type(4){
          background: #00a0a0;
      }
      .item:nth-of-type(7){
          background: #a0a0ff;
      }
      .item:nth-of-type(9){
          background: #00a0a0;
      }
      .item:nth-of-type(8){
          background: #a0a0ff;
      }
    </style>
    <script src="./index.js"></script>

</head>
<body>
    <div class="wrapper">
        <div class="item">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
        <div class="item">4</div>
        <div class="item">5</div>
        <div class="item">6</div>
        <div class="item">7</div>
        <div class="item">8</div>
        <div class="item">9</div>
    </div>
</body>
</html>

4.table

<!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>
    <style>
        table{
            border-collapse: collapse;
            text-align: center;
            width: 450px;
            height: 450px;
        }
        tr{
                height: 150px;
        }
        th{
            width: 150px;
        }
        tr:nth-of-type(1){
            background: chartreuse;
        

        }
        tr:nth-of-type(1) th:nth-of-type(1){
            background: darkblue;
        }
        tr:nth-of-type(1) th:nth-of-type(2){
            background: deeppink;
        }
        tr:nth-of-type(2){
            background: cyan;
        }
        tr:nth-of-type(2) th:nth-of-type(2){
            background: darkkhaki;
        }
        tr:nth-of-type(3){
            background: chocolate;
        }
        tr:nth-of-type(3) th:nth-of-type(3){
            background: darkseagreen;
        }
        tr:nth-of-type(3) th:nth-of-type(1){
            background: deepskyblue;
        }
    </style>
</head>
<body>
    <table>
       <tr>
           <th>1</th>
           <th>2</th>
           <th>3</th>
       </tr>
       <tr>
           <th>1</th>
           <th>2</th>
           <th>3</th>
       </tr>
       <tr>
           <th>1</th>
           <th>2</th>
           <th>3</th>
       </tr>
    </table>
</body>
</html>
posted @ 2021-04-10 10:38  abcdefgab  阅读(138)  评论(0)    收藏  举报