CSS-九宫格

通过定义列表,在CSS中使用margin和padding、float对父子元素对宽高进行定义后,使各元素依次排列。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    ...
  </style>
</head>

<body>
  <div>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
      <li>7</li>
      <li>8</li>
      <li>9</li>
    </ul>
  </div>
</body>
</html>
// Style sheet
*{
      padding: 0;
      margin: 0;
    }
    div {
      height: 190px;
      width: 380px;
      background-color: rgba(147, 158, 235, 0.815);
      border-style: solid;
      margin: 50px auto;
    }
    ul {
      padding-top: 10px;
      padding-left: 20px;
    }
    li {
      list-style: none;
      width: 100px;
      height: 50px;
      float: left;
      margin-right: 20px;
      margin-bottom: 10px;
      background-color:rgb(226, 214, 237);
      text-align: center;
      line-height: 50px;
    }