布局:左侧固定宽度,右侧自适应

196 阅读1分钟

[toc]

概述

闲来无事,翻看MDN,看到clac()函数,工作多年,却未使用过。所以,拿来练手一下。

方案一:css calc()

<!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>
      .container {
        width: 100%;
        height: 200px;
        background-color: antiquewhite;
      }
      .left {
        width: 200px;
        height: 200px;
        background-color: blueviolet;
        float: left;
      }
      .right {
        width: calc(100% - 200px);
        height: 200px;
        background-color: brown;
        float: left;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="left"></div>
      <div class="right"></div>
    </div>
  </body>
</html>

方案二: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>
      .container {
        width: 100%;
        height: 200px;
        background-color: cornsilk;
        display: flex;
        flex-direction: row;
      }
      .left {
        width: 200px;
        background-color: crimson;
      }
      .right {
        width: 100%;
        background-color: darkgreen;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="left"></div>
      <div class="right"></div>
    </div>
  </body>
</html>