Blogger Information
Blog 9
fans 0
comment 7
visits 6021
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Chapter9 flex布局
无关
Original
548 people have browsed it

布局案例

1.1

1.2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>基于浮动的二列布局</title>
  7. <style>
  8. header,
  9. footer,
  10. aside,
  11. main{
  12. background-color: cornflowerblue;
  13. }
  14. header,
  15. footer{
  16. height: 50px;
  17. }
  18. aside,
  19. main{
  20. min-height: 400px;
  21. }
  22. .wrap{
  23. width: 1000px;
  24. border: 1px solid #000;
  25. overflow: hidden;
  26. margin: 10px auto;
  27. }
  28. aside{
  29. width: 200px;
  30. float: left;
  31. }
  32. main{
  33. width: 790px;
  34. float: right;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <h2>NO1.基于浮动的二列布局</h2>
  40. <header>页眉</header>
  41. <div class="wrap">
  42. <aside>侧边栏</aside>
  43. <main>主体区</main>
  44. </div>
  45. <footer>页脚</footer>
  46. </body>
  47. </html>

2.1

2.2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>基于定位的二列布局</title>
  7. <style>
  8. header,
  9. footer,
  10. aside,
  11. main{
  12. background-color: cornflowerblue;
  13. }
  14. header,
  15. footer{
  16. height: 50px;
  17. }
  18. .wrap{
  19. width: 1000px;
  20. min-height:500px;
  21. border: 1px solid #000;
  22. margin: 10px auto;
  23. /* 定位父级 */
  24. position: relative;
  25. }
  26. aside{
  27. width: 200px;
  28. min-height: inherit;
  29. position: absolute;
  30. top:0;
  31. left:0;
  32. }
  33. main{
  34. width: 790px;
  35. min-height: inherit;
  36. background-color:lightblue;
  37. position: absolute;
  38. top:0;
  39. right:0;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <h2>NO2.基于定位的二列布局</h2>
  45. <header>页眉</header>
  46. <div class="wrap">
  47. <aside>侧边栏</aside>
  48. <main>主体区</main>
  49. </div>
  50. <footer>页脚</footer>
  51. </body>
  52. </html>

3.1

3.2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>内容的多栏显示</title>
  7. <style>
  8. 初始化
  9. *{
  10. padding: 0;
  11. margin: 0;
  12. box-sizing: border-box;
  13. }
  14. :root {
  15. font-size: 16px;
  16. color: lightsalmon;
  17. }
  18. div{
  19. border:1px solid lightblue;
  20. padding: 1rem;
  21. width: 60rem;
  22. margin:10px auto;
  23. }
  24. div{
  25. /* 分列显示 */
  26. column-count:3;
  27. /* column-rule-style:solid; */
  28. /* column-rule-width: 1px; */
  29. /* column-rule-color: red; */
  30. column-rule:1px solid red;
  31. }
  32. header{
  33. color:blue;
  34. font-size: 1.3rem;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <header>NO3.内容的多栏显示</header>
  40. <div>
  41. 【环球网报道】《港区国安法》正式生效后,香港特区维护国家安全委员会及警务处国家安全处随即成立。香港《东方日报》3日消息称,香港保安局局长李家超表示,国安处要面对国家级对手,故由副处长领导,职级是警队六处中最高,凸显其重要地位。李家超透露,一旦处理涉及危害国家安全的恐怖主义活动,国安处可动用其他部门配合,包括“飞虎队”及拆弹专家等,保安局则负责统筹及协调政府各部门及各纪律部队的国安工作,包括海关及入境处,严防危险品及目标人物进入香港。
  42. </div>
  43. </body>
  44. </html>

4.1

4.2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>三列布局</title>
  7. <style>
  8. header,
  9. footer{
  10. height: 40px;
  11. background-color: lightsalmon;
  12. }
  13. .container{
  14. width: 1000px;
  15. margin:10px atuo;
  16. border:1px solid red;
  17. /* 分为三列 */
  18. column-count: 3;
  19. }
  20. aside{
  21. background-color: lightseagreen;
  22. min-height: 600px;
  23. }
  24. main{
  25. background-color: limegreen;
  26. min-height: 600px;
  27. }
  28. main{
  29. column-span:2;
  30. min-height: 600px;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <h2>NO4.三列布局</h2>
  36. <header>header</header>
  37. <div class="container">
  38. <aside>left</aside>
  39. <main>main</main>
  40. <aside>right</aside>
  41. </div>
  42. <footer>footer</footer>
  43. </body>
  44. </html>

5.1

5.2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>flexbox弹性盒子布局快速预览</title>
  7. <style>
  8. .container{
  9. width: 300px;
  10. /* 如果这个容器中的内容要使用lexbox布局,第一步就是必须将此容器转为弹性盒子 */
  11. /* 弹性容器 */
  12. display: flex;
  13. }
  14. .container > .item{
  15. /* 一旦将父元素转为弹性容器,内部的子元素会自动成为弹性项目 */
  16. /* 不管这个项目标签是什么类型,统统转为“行内块” */
  17. /* 行内:全部一排显示 */
  18. /* 块:可以摄制组宽,高 */
  19. /* flex:auto; */
  20. /* width: 60px; */
  21. /* width: 50; */
  22. width:120px;
  23. }
  24. /* 换行显示 ,多行元素*/
  25. .container{
  26. flex-wrap: wrap;
  27. }
  28. /* 主轴,交叉轴(又名侧轴,列轴,辅助轴) */
  29. </style>
  30. </head>
  31. <body>
  32. <h5>NO5.flexbox弹性盒子布局快速预览</h5>
  33. <div class="container">
  34. <div class="item">1</div>
  35. <span class="item" >2</span>
  36. <a class="item">3</a>
  37. <a class="item">4</a>
  38. <a class="item">5</a>
  39. </div>
  40. </body>
  41. </html>

6.1

6.2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>flexbox弹性盒子多行容器</title>
  7. <style>
  8. .container{
  9. width: 300px;
  10. /* 如果这个容器中的内容要使用lexbox布局,第一步就是必须将此容器转为弹性盒子 */
  11. /* 弹性容器 */
  12. display: flex;
  13. }
  14. .container > .item{
  15. /* 一旦将父元素转为弹性容器,内部的子元素会自动成为弹性项目 */
  16. /* 不管这个项目标签是什么类型,统统转为“行内块” */
  17. /* 行内:全部一排显示 */
  18. /* 块:可以摄制组宽,高 */
  19. /* flex:auto; */
  20. /* width: 60px; */
  21. /* width: 50; */
  22. width:120px;
  23. }
  24. /* 换行显示 ,多行元素*/
  25. /* 显示,火狐页面右键-查看元素-查看器-flex */
  26. .container{
  27. flex-wrap: wrap;
  28. }
  29. /* 主轴,交叉轴(又名侧轴,列轴,辅助轴) */
  30. </style>
  31. </head>
  32. <body>
  33. <h5>NO6.flexbox弹性盒子多行容器</h5>
  34. <div class="container">
  35. <div class="item">1</div>
  36. <div class="item">2</div>
  37. <div class="item">3</div>
  38. <div class="item">4</div>
  39. <div class="item">5</div>
  40. </div>
  41. </body>
  42. </html>

7.1

7.2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>单行容器中的项目对齐方式</title>
  7. <style>
  8. .container {
  9. width: 300px;
  10. /* 显示,火狐页面右键-查看元素-查看器-flex */
  11. display: flex;
  12. /* justify-content: 控制所有项目在主轴上的对齐方式; */
  13. /* 本质是:设置容器中的剩余空间在所有“项目之间”的分配方案 */
  14. /* 1.容器剩余空间在所有项目“二边”的如何分配,将全部项目视为一个整体 */
  15. justify-content: flex-start;
  16. justify-content: flex-end;
  17. justify-content: center;
  18. /* 2.容器剩余空间在所有项目“之间”的如何分配 ,将项目视为一个个独立的个体*/
  19. /* 两端对齐: */
  20. justify-content: space-between;
  21. /* 分散对齐:剩余空间在所有项目的两侧平均分配,中间间距是两端间距的2倍*/
  22. justify-content: space-around;
  23. /* 所有项目平均分配,空间数=项目数+1 */
  24. justify-content: space-evenly;
  25. }
  26. .container > .item{
  27. width: 60px;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <h5>NO.7 单行容器中的项目对齐方式</h5>
  33. <div class="container">
  34. <div class="item">1</div>
  35. <div class="item">2</div>
  36. <div class="item">3</div>
  37. </div>
  38. </body>
  39. </html>

8.1

8.2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>多行容器中的项目对齐方式</title>
  7. <style>
  8. .container {
  9. width: 300px;
  10. /* 显示,火狐页面右键-查看元素-查看器-flex */
  11. display: flex;
  12. /* 多行显示 */
  13. flex-wrap: wrap;
  14. height: 120px;
  15. /* align-content: 设置多行容器中的项目排列; */
  16. /* 1.容器剩余空间在所有项目“二边”的如何分配,将全部项目视为一个整体 */
  17. align-content:stretch;
  18. align-content: flex-start;
  19. align-content: flex-end;
  20. align-content: center;
  21. /* 2.容器剩余空间在所有项目“之间”的如何分配 ,将项目视为一个个独立的个体*/
  22. align-content: space-between;
  23. align-content: space-around;
  24. align-content: space-evenly;
  25. }
  26. .container > .item{
  27. width: 60px;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <h5>NO.8 多行容器中的项目对齐方式</h5>
  33. <div class="container">
  34. <div class="item">1</div>
  35. <div class="item">2</div>
  36. <div class="item">3</div>
  37. <div class="item">11</div>
  38. <div class="item">12</div>
  39. <div class="item">13</div>
  40. <div class="item">21</div>
  41. </div>
  42. </body>
  43. </html>

9.1

9.2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>NO.9 主轴垂直时的项目排列</title>
  7. <style>
  8. .container {
  9. width: 300px;
  10. height: 150px;
  11. /* 显示,火狐页面右键-查看元素-查看器-flex */
  12. display: flex;
  13. /* 主轴的方向,默认为行 */
  14. flex-direction: row;
  15. /* 主轴的方向为列 */
  16. flex-direction: column;
  17. /* 项目两边分配 */
  18. justify-content: space-between;
  19. justify-content: space-around;
  20. justify-content: space-evenly;
  21. /* 项目之间分配 */
  22. /* align-content: space-between; */
  23. /* align-content: space-around; */
  24. align-content: space-evenly;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <h5>NO.9 主轴垂直时的项目排列</h5>
  30. <div class="container">
  31. <div class="item">1</div>
  32. <div class="item">2</div>
  33. <div class="item">3</div>
  34. </div>
  35. </body>
  36. </html>

10.1

10.2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>NO10.交叉轴的项目排列</title>
  7. <style>
  8. .container {
  9. width: 300px;
  10. height: 150px;
  11. display:flex;
  12. /* 项目在交叉轴上是自动伸缩的 */
  13. align-items:stretch ;
  14. align-items:normal;
  15. align-items:flex-start;
  16. align-items:flex-end;
  17. align-items:center;
  18. }
  19. .container > .item{
  20. width: 60px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <h5>NO10.交叉轴的项目排列</h5>
  26. <div class="container">
  27. <div class="item">1</div>
  28. <div class="item">2</div>
  29. <div class="item">3</div>
  30. </div>
  31. </body>
  32. </html>

11.1

11.2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>NO11.主轴方向与项目排列的简写</title>
  7. <style>
  8. .container {
  9. width: 300px;
  10. height: 50px;
  11. display:flex;
  12. /* 默认值,不用写出来 */
  13. /* flex-direction: row; */
  14. /* flex-wrap: nowrap; */
  15. /* flex-flow: row nowrap; */
  16. flex-flow: column wrap;
  17. flex-flow: column nowrap;
  18. }
  19. .container > .item{
  20. width: 60px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <h5>NO11.主轴方向与项目排列的简写</h5>
  26. <div class="container">
  27. <div class="item">1</div>
  28. <div class="item">2</div>
  29. <div class="item">3</div>
  30. <div class="item">1</div>
  31. <div class="item">2</div>
  32. </div>
  33. </body>
  34. </html>

12.1

12.2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>NO12.flex容器属性实战:快速来一个主导航</title>
  7. <style>
  8. *{
  9. padding: 0;
  10. margin: 0;
  11. box-sizing: border-box;
  12. }
  13. a {
  14. text-decoration: none;
  15. color:red;
  16. }
  17. nav{
  18. height: 40px;
  19. background-color: lightblue;
  20. padding: 0 50px;
  21. /* 转为弹性盒布局 */
  22. display:flex;
  23. }
  24. nav a{
  25. height: inherit;
  26. line-height: 40px;
  27. padding: 0 15px;
  28. }
  29. nav a:hover{
  30. background-color: seagreen;
  31. color:white;
  32. }
  33. /* 登录注册放在最右边 */
  34. nav a:last-of-type{
  35. margin-left:auto;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <h5>NO12.flex容器属性实战:快速来一个主导航</h5>
  41. <header>
  42. <nav>
  43. <a href="">主页</a>
  44. <a href="">教学视频</a>
  45. <a href="">社区问答</a>
  46. <a href="">资源下载</a>
  47. <a href="">登录/注册</a>
  48. </nav>
  49. </header>
  50. </body>
  51. </html>

13.1

13.2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>NO.13 项目属性:order控制项目顺序</title>
  7. <style>
  8. .container {
  9. width: 300px;
  10. display: flex;
  11. }
  12. .container > .item{
  13. width: 60px;
  14. }
  15. .container> .item:last-of-type{
  16. /* order:默认是0 */
  17. order:3;
  18. }
  19. .container> .item:last-of-type{
  20. /* order:默认是0 */
  21. order:6;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <h5>NO.13 项目属性:order控制项目顺序</h5>
  27. <div class="container">
  28. <div class="item">1</div>
  29. <div class="item">2</div>
  30. <div class="item">3</div>
  31. <div class="item">4</div>
  32. </div>
  33. </body>
  34. </html>

14.1

14.2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>NO.14 order案例:调整元素顺序</title>
  7. <style>
  8. .container {
  9. width: 300px;
  10. display: flex;
  11. flex-direction: column;
  12. /* position: relative; */
  13. }
  14. .container > .item{
  15. border: 1px solid #000;
  16. padding: 10px;
  17. display: flex;
  18. position: relative;
  19. }
  20. .container> .item>div{
  21. display: flex;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <h5>NO.14 order案例:调整元素顺序</h5>
  27. <div class="container">
  28. <div class="item">
  29. image-1.jpg &nbsp;&nbsp;&nbsp;
  30. <div>
  31. <button onclick="up(this)">up</button>
  32. <button onclick="down(this)">down</button>
  33. </div>
  34. </div>
  35. <div class="item">
  36. image-2.jpg &nbsp;&nbsp;&nbsp;
  37. <div>
  38. <button onclick="up(this)">up</button>
  39. <button onclick="down(this)">down</button>
  40. </div>
  41. </div>
  42. <div class="item">
  43. image-3.jpg &nbsp;&nbsp;&nbsp;
  44. <div>
  45. <button onclick="up(this)">up</button>
  46. <button onclick="down(this)">down</button>
  47. </div>
  48. </div>
  49. </div>
  50. <script>
  51. let up=(ele)=>(ele.offsetParent.style.order -= 1);
  52. let down= (ele)=>(ele.offsetParent.style.order += 1);
  53. </script>
  54. </body>
  55. </html>
Correcting teacher:GuanhuiGuanhui

Correction status:qualified

Teacher's comments:写的很好!flex是页面布局的一大利器,另外有的代码没有用代码块框起来,注意一下!
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
1 comments
2020-07-08 18:59:57
好的,改过来了。 谢谢老师!
1 floor
Author's latest blog post