Application of the box model

To achieve such a layout

The initial implementation of the code is like this:

<!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>
    * {
      margin: 0;
      padding: 0;
    }
    .box {
      display: flex;
      flex-wrap: wrap;
    }
    .item {
      width: 50%;
    }
  </style>
</head>
<body>
  <div class="box">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
  </div>
  <script lang="ts">
    
  </script>
</body> 
</html>

Add to item: border: 1px solid red; after

Because 1px border occupies space, width: 50% + 1px causes one row and two columns to fit, and they are squeezed down. At this time, you need to change the box model of each item, box-sizing: border-box;

Complete code:
 

<!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>
    * {
      margin: 0;
      padding: 0;
    }
    .box {
      display: flex;
      flex-wrap: wrap;
    }
    .item {
      width: 50%;
      border: 1px solid red;
      box-sizing:  border-box;
    }
  </style>
</head>
<body>
  <div class="box">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
  </div>
  <script lang="ts">
    
  </script>
</body> 
</html>

 

 

Guess you like

Origin blog.csdn.net/Luckyzhoufangbing/article/details/108823293