两栏布局?

x33g5p2x  于2022-04-21 转载在 其他  
字(2.4k)|赞(0)|评价(0)|浏览(380)

一、写在前面
pdd前端面试,问了一个两栏布局的问题,接下来就直接写出来了,觉得很简单。

<!DOCTYPE html>
<html lang="cn">

<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: 300px;
      background-color: red;
      display: flex;
    }

    #left {
      height: 100%;
      width: 300px;
      background-color: green;
      order: 1;
    }

    #right {
      height: 100%;
      flex: 1;
      background-color: yellow;
      order: 2;
    }
  </style>
</head>

<body>

  <div id="container">
    <div id="left"></div>
    <div id="right"></div>
  </div>

</body>

</html>

接下来面试官改变了<div id='left'></div><div id='right'></div>的位置,然后说这样就不一样了。那还想要两者一样此时该怎么办。然后我就改我的代码为:

<!DOCTYPE html>
<html lang="cn">

<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: 300px;
      background-color: red;
      display: flex;
    }

    #container div:first-child {
      height: 100%;
      width: 300px;
      background-color: green;
    }

    #container div:last-child {
      height: 100%;
      flex: 1;
      background-color: yellow;
    }
  </style>
</head>

<body>

  <div id="container">
    <div id="right"></div>
    <div id="left"></div>
  </div>

</body>

</html>

然后面试官说,我让你用flex布局的目的不是让你这样写的,flex中存在一个属性,可以不改变位置,你在想想。
我:有印象,但是忘记是哪个属性了,面试完后就查了一下。order属性

<!DOCTYPE html>
<html lang="cn">

<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: 300px;
      background-color: red;
      display: flex;
    }

    #left {
      height: 100%;
      width: 300px;
      background-color: green;
      order: 1;
    }

    #right {
      height: 100%;
      flex: 1;
      background-color: yellow;
      order: 2;
    }
  </style>
</head>

<body>

  <div id="container">
    <div id="left"></div>
    <div id="right"></div>
  </div>

</body>

</html>

相关文章