两栏布局?

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

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

  1. <!DOCTYPE html>
  2. <html lang="cn">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. #container {
  10. width: 100%;
  11. height: 300px;
  12. background-color: red;
  13. display: flex;
  14. }
  15. #left {
  16. height: 100%;
  17. width: 300px;
  18. background-color: green;
  19. order: 1;
  20. }
  21. #right {
  22. height: 100%;
  23. flex: 1;
  24. background-color: yellow;
  25. order: 2;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="container">
  31. <div id="left"></div>
  32. <div id="right"></div>
  33. </div>
  34. </body>
  35. </html>

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

  1. <!DOCTYPE html>
  2. <html lang="cn">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. #container {
  10. width: 100%;
  11. height: 300px;
  12. background-color: red;
  13. display: flex;
  14. }
  15. #container div:first-child {
  16. height: 100%;
  17. width: 300px;
  18. background-color: green;
  19. }
  20. #container div:last-child {
  21. height: 100%;
  22. flex: 1;
  23. background-color: yellow;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="container">
  29. <div id="right"></div>
  30. <div id="left"></div>
  31. </div>
  32. </body>
  33. </html>

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

  1. <!DOCTYPE html>
  2. <html lang="cn">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. #container {
  10. width: 100%;
  11. height: 300px;
  12. background-color: red;
  13. display: flex;
  14. }
  15. #left {
  16. height: 100%;
  17. width: 300px;
  18. background-color: green;
  19. order: 1;
  20. }
  21. #right {
  22. height: 100%;
  23. flex: 1;
  24. background-color: yellow;
  25. order: 2;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="container">
  31. <div id="left"></div>
  32. <div id="right"></div>
  33. </div>
  34. </body>
  35. </html>

相关文章