左右两个盒子, 高度自适应

2020/6/17
面试问的问题
左右个一个盒子, 左边高度变化, 让右边跟着一起等高 , 或者右边高度发生变化左边盒子等高

两种办法
第一种

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title></title>
  <style>
    .left,
    .right {
      padding: 10px;
      /* 将两个盒子设置为表格单元格显示 */
      display: table-cell; 
      border: 1px solid #f40;
    }
  </style>
</head>

<body>
  <div class="wrap">
    <div class="left"> left div 
      <p>123</p>
      <p>123</p>
      <p>123</p>
      <p>123</p>
      <p>123</p>
    </div>
    <div class="right">right div</div>
  </div>
</body>

</html>

第二种

<!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;
  }

  html,
  body {
    /* 将盒子屏幕居中 */
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
   
  }

  .father {
    display: flex;
    justify-content: center;
    width: 200px;
    background-color: pink;
  }

  .left {
    background-color: red;
    /* flex: 1; */
  }


  .center {
    background-color: green;
    /* flex: 1; */
  }

  .right {
    float: right;
    background-color: yellow
  }


  
</style>

<body>
  <div class="father">
    <div class="left">左边
      
    </div>
    <!-- <div class="right">右边</div> -->
    <div class="center">中间

      <div>123</div>
      <div>123</div>
      <div>123</div>
      <div>123</div>
    </div>
  </div>

	

</body>

</html>

你可能感兴趣的:(css部分)