Blogger Information
Blog 10
fans 1
comment 0
visits 9803
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
第八课-flex布局案例练习
弋兮
Original
1574 people have browsed it

一、用flex快速搭建手机端通用页面

flex布局可以快速搭建出手机端通用页面的模板样式,代码如下:

  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. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <link rel="stylesheet" href="css/demo.css">
  8. <title>手机端通用页面</title>
  9. </head>
  10. <body>
  11. <header>header</header>
  12. <main>main</main>
  13. <footer>footer</footer>
  14. </body>
  15. </html>

对应的css如下:

  1. *{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body{
  6. display: flex;
  7. flex-flow: column nowrap;
  8. height: 100vh;
  9. }
  10. header,footer{
  11. display: flex;
  12. box-sizing: border-box;
  13. justify-content: center;
  14. align-items: center;
  15. background-color: rgb(238, 241, 190);
  16. }
  17. main{
  18. display: flex;
  19. box-sizing: border-box;
  20. justify-content: center;
  21. align-items: center;
  22. background-color: rgb(247, 188, 237);
  23. /* 让main主题区充满剩余空间 */
  24. flex: 1;
  25. height: 90vh;
  26. }

预览效果如图:

手抄:

二、flex实现圣杯布局

之前学习过如何实现圣杯布局,但是代码比较繁杂,如果用flex来实现会极为简单。代码如下:

  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. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <link rel="stylesheet" href="css/demo1.css">
  8. <title>flex实现圣杯布局</title>
  9. </head>
  10. <body>
  11. <header>header</header>
  12. <main>
  13. <article></article>
  14. <aside></aside>
  15. <aside></aside>
  16. </main>
  17. <footer>footer</footer>
  18. </body>
  19. </html>

对应css如下:

  1. *{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body{
  6. display: flex;
  7. flex-flow: column nowrap;
  8. height: 100vh;
  9. }
  10. main{
  11. display: flex;
  12. box-sizing: border-box;
  13. flex-flow: row nowrap;
  14. flex: 1;
  15. }
  16. article{
  17. flex: 1;
  18. box-sizing: border-box;
  19. background-color: rgb(236, 140, 140);
  20. }
  21. aside:first-of-type{
  22. width: 50px;
  23. background-color: rgb(53, 238, 6);
  24. order: -1;
  25. }
  26. aside:last-of-type{
  27. width: 50px;
  28. background-color: rgb(228, 216, 55);
  29. }
  30. header,footer{
  31. display: flex;
  32. box-sizing: border-box;
  33. justify-content: center;
  34. align-items: center;
  35. height: 60px;
  36. background-color: rgb(176, 230, 106);
  37. }
  38. /*
  39. main>aside:first-of-type{
  40. order: -1;
  41. } */

预览效果如图:

手抄:

三、flex实现登录居中显示

  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. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <link rel="stylesheet" href="css/demo3.css">
  8. <title>flex实现登录居中显示</title>
  9. </head>
  10. <body>
  11. <div class="container">
  12. <h3>用户登录</h3>
  13. <form action="" class="fd" id="form" name="form">
  14. <div><label for="user">用户:</label><input type="text"></div>
  15. <div><label for="password">密码:</label><input type="password"></div>
  16. <div><button>提交</button></div>
  17. </form>
  18. </div>
  19. </body>
  20. </html>

对应css如下:

  1. *{
  2. margin: 0;
  3. padding:0
  4. }
  5. body{
  6. height: 100vh;
  7. display: flex;
  8. flex-flow: column nowrap;
  9. justify-content: center;
  10. align-items: center;
  11. background:linear-gradient(to right bottom,rgb(173, 241, 227),white,rgb(148, 197, 236));
  12. }
  13. .container{
  14. display: flex;
  15. flex-flow: column nowrap;
  16. /* border: 1px solid rgb(7, 10, 17); */
  17. padding: 5px;
  18. width: 300px;
  19. position: relative;
  20. top: -66px;
  21. }
  22. .container>h3{
  23. margin: 10px 0;
  24. font-weight: lighter;
  25. text-align: center;
  26. }
  27. #form{
  28. display: flex;
  29. flex-flow: column nowrap;
  30. padding: 15px;
  31. border: 1px solid lightgreen;
  32. border-radius: 16px;
  33. background:linear-gradient(to left top,rgb(164, 233, 164),white,rgb(77, 148, 77));
  34. }
  35. #form>div{
  36. display: flex;
  37. margin-left: 6px;
  38. margin-top: 12px;
  39. margin-bottom: 10px;
  40. }
  41. #form>div>input{
  42. flex:1;
  43. line-height: 22px;
  44. padding-left: 10px;
  45. border: 0;
  46. border-radius: 6px;
  47. margin-left: 8px;
  48. }
  49. .fd:hover{
  50. background: linear-gradient(to left top, lightcyan, white);
  51. box-shadow: 0 0 5px #888;
  52. }
  53. form:hover{
  54. background-color: rgb(6, 7, 7);
  55. }
  56. #form>div>button{
  57. flex: 1;
  58. margin-top: 6px;
  59. border: 0;
  60. border-radius: 8px;
  61. height: 22px;
  62. letter-spacing: 10px;
  63. }
  64. #form>div>button:hover{
  65. background-color: rgb(196, 240, 75);
  66. }

预览效果如图:

手抄:
四、用flex搭建一个简单的网站后台样式

  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. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <link rel="stylesheet" href="http://at.alicdn.com/t/font_1451331_wsqywwwzbq.css">
  8. <link rel="stylesheet" href="css/demo4.css">
  9. <title>简易网站后台</title>
  10. </head>
  11. <body>
  12. <header>
  13. <div class="tb">
  14. <img src="imgs/logo-dark.png" alt="">
  15. </div>
  16. <form action="" class="ss">
  17. <input type="text" placeholder="请输入关键词搜索">
  18. <button><i class="iconfont iconsousuo"></i></button>
  19. </form>
  20. <div class="content">
  21. <div class="xx">
  22. <a href=""><i class="iconfont iconshaixuan"></i>消息</a>
  23. </div>
  24. <dic class="help">
  25. <a href=""><i class="iconfont iconyiwen"></i>Help</a>
  26. </dic>
  27. <div class="user">
  28. <a href=""><i class="iconfont iconyonghuguanli"></i>登录</a>
  29. </div>
  30. </div>
  31. </header>
  32. <main>
  33. <article>
  34. <section>
  35. <div class="s1">
  36. <p>Weekly Overview</p>
  37. <p>date:2019-11-07</p>
  38. </div>
  39. <div class="s2">
  40. <div class="s21">
  41. <i class="iconfont iconjishu-line"></i>
  42. <div class="s211">
  43. <p>1,252</p>
  44. <p>Downloads</p>
  45. </div>
  46. </div>
  47. <div class="s21">
  48. <i class="iconfont iconjishu-line"></i>
  49. <div class="s211">
  50. <p>203</p>
  51. <p>Sales</p>
  52. </div>
  53. </div>
  54. <div class="s21">
  55. <i class="iconfont iconjishu-line"></i>
  56. <div class="s211">
  57. <p>274,678</p>
  58. <p>Visits</p>
  59. </div>
  60. </div>
  61. <div class="s21">
  62. <i class="iconfont iconjishu-line"></i>
  63. <div class="s211">
  64. <p>35%</p>
  65. <p>Conversions</p>
  66. </div>
  67. </div>
  68. </div>
  69. <div class="s3">
  70. <div class="s4">
  71. <table>
  72. <tr class="tr1">
  73. <th>ID</th>
  74. <th>会员名称</th>
  75. <th>已购买货物数量</th>
  76. <th>单价</th>
  77. <th>消费合计</th>
  78. </tr>
  79. <tr>
  80. <td>1</td>
  81. <td>liu</td>
  82. <td>3</td>
  83. <td>1200</td>
  84. <td>3600</td>
  85. </tr>
  86. <tr>
  87. <td>2</td>
  88. <td>liu</td>
  89. <td>3</td>
  90. <td>1200</td>
  91. <td>3600</td>
  92. </tr>
  93. <tr>
  94. <td>3</td>
  95. <td>liu</td>
  96. <td>3</td>
  97. <td>1200</td>
  98. <td>3600</td>
  99. </tr>
  100. <tr>
  101. <td>4</td>
  102. <td>liu</td>
  103. <td>3</td>
  104. <td>1200</td>
  105. <td>3600</td>
  106. </tr>
  107. <tr>
  108. <td>5</td>
  109. <td>liu</td>
  110. <td>3</td>
  111. <td>1200</td>
  112. <td>3600</td>
  113. </tr>
  114. <tr>
  115. <td>6</td>
  116. <td>liu</td>
  117. <td>3</td>
  118. <td>1200</td>
  119. <td>3600</td>
  120. </tr>
  121. </table>
  122. </div>
  123. </div>
  124. </section>
  125. </article>
  126. <aside>
  127. <nav>
  128. <ul>
  129. <li><a href=""><i class="iconfont iconwangzhi"></i>主页</a></li>
  130. <li><a href=""><i class="iconfont iconxinzenghuopin">文件</i></a></li>
  131. <li><a href=""><i class="iconfont iconkehu"></i>用户</a></li>
  132. <li><a href=""><i class="iconfont iconyonghuguanli"></i>设备</a></li>
  133. <li><a href=""><i class="iconfont iconxiaoshou"></i>关于</a></li>
  134. <li><a href=""><i class="iconfont iconpeizhi"></i>设置</a></li>
  135. </ul>
  136. </nav>
  137. </aside>
  138. </main>
  139. <footer><p>Copyright © 2017.Company name All rights reserved.More Templates 模板之家 - Collect from 网页模板</p></footer>
  140. </body>
  141. </html>

对应css如下:

  1. *{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body{
  6. display: flex;
  7. height: 100vh;
  8. width: 100vw;
  9. flex-flow: column nowrap;
  10. }
  11. header,footer{
  12. height: 60px;
  13. background-color: rgb(229, 230, 221);
  14. display: flex;
  15. /* justify-content: space-between; */
  16. align-items: center;
  17. }
  18. main{
  19. display: flex;
  20. flex: 1;
  21. background-color: rgb(218, 241, 176);
  22. }
  23. article{
  24. flex: 1;
  25. background-color: rgb(210, 221, 221);
  26. height: auto;
  27. }
  28. aside{
  29. width: 150px;
  30. height: auto;
  31. order: -1;
  32. background-color: rgb(171, 202, 241);
  33. }
  34. .ss{
  35. margin: auto 15px;
  36. }
  37. .ss>input{
  38. line-height: 22px;
  39. border: 0;
  40. border-radius: 15px;
  41. padding-left: 6px;
  42. }
  43. .ss>button{
  44. margin-left: -20px;
  45. border: 0;
  46. height: auto;
  47. color:white
  48. padding:5px
  49. }
  50. .tb{
  51. margin: auto 15px;
  52. }
  53. a{
  54. text-decoration: none;
  55. }
  56. .xx{
  57. margin-right: 15px;
  58. /* margin-left: 50vw; */
  59. }
  60. .help{
  61. }
  62. main>aside>nav>ul{
  63. list-style-type: none;
  64. }
  65. aside{
  66. display: flex;
  67. box-sizing: border-box;
  68. flex-flow: column nowrap;
  69. width: 186px;
  70. }
  71. nav{
  72. display: flex;
  73. box-sizing: border-box;
  74. flex-flow: column nowrap;
  75. flex: 1;
  76. margin-top: 25px;
  77. }
  78. nav>ul{
  79. flex: 1;
  80. }
  81. nav>ul{
  82. text-align: center;
  83. /* display: flex; */
  84. box-sizing: border-box;
  85. /* flex-flow: column nowrap;
  86. justify-content: center;
  87. align-content: center; */
  88. }
  89. main>aside>nav>ul>li{
  90. /* flex: 1; */
  91. margin-bottom: 20px;
  92. /* box-sizing: border-box; */
  93. display: block;
  94. height: 33px;
  95. background-color: rgb(171, 202, 241);
  96. text-align: center;
  97. }
  98. main>aside>nav>ul>li:hover,main>aside>nav>ul>li:active{
  99. background-color: rgb(222, 243, 210);
  100. box-shadow: oldlace;
  101. }
  102. main>aside>nav>ul>li>a{
  103. line-height:33px ;
  104. }
  105. header{
  106. border-bottom:1px solid lightslategray;
  107. box-shadow:lightsteelblue;
  108. }
  109. .content{
  110. display: flex;
  111. flex: 1;
  112. justify-content: flex-end;
  113. padding-right: 25px;
  114. }
  115. .user{
  116. margin-left:22px
  117. }
  118. article{
  119. padding: 25px;
  120. display: flex;
  121. }
  122. article>section{
  123. background-color: rgb(235, 235, 235);
  124. height: auto;
  125. width: 100%;
  126. display: flex;
  127. flex-flow: column nowrap;
  128. }
  129. .s1{
  130. display: flex;
  131. flex-flow: column nowrap;
  132. margin: 18px 0 15px 18px;
  133. }
  134. .s1>p:first-of-type{
  135. margin-bottom: 10px;
  136. }
  137. .s2{
  138. display: flex;
  139. flex-flow: row nowrap;
  140. margin-left: 20px;
  141. }
  142. .s21{
  143. display: flex;
  144. width: 250px;
  145. height: 100px;
  146. border: 1px solid rgb(85, 87, 85);
  147. border-radius: 20px;
  148. margin: 10px auto;
  149. padding: 6px;
  150. align-items: center;
  151. justify-content: space-between;
  152. }
  153. .s21>*{
  154. margin-left: 30px;
  155. }
  156. .s211{
  157. margin-right: 30px;
  158. }
  159. footer{
  160. justify-content: center;
  161. }
  162. .s3{
  163. flex: 1;
  164. /* background-color: rgb(109, 99, 99); */
  165. padding: 6px;
  166. display: flex;
  167. justify-content: center;
  168. align-items: center;
  169. }
  170. .s4{
  171. display: flex;
  172. justify-content: center;
  173. align-items: center;
  174. flex: 1;
  175. }
  176. table{
  177. flex: 1;
  178. box-sizing: border-box;;
  179. /* border: 1px solid black; */
  180. width: 100%;
  181. height: 300px;
  182. background: linear-gradient(to top,rgb(229, 233, 221),rgb(234, 236, 236));
  183. /* flex: 1; */
  184. /* display: flex; */
  185. padding: 12px;
  186. /* justify-content: center; */
  187. /* align-items: center; */
  188. font-size: 20px;
  189. letter-spacing: 2px;
  190. border-collapse:collapse;
  191. margin: 15px auto;
  192. justify-content: space-evenly;
  193. }
  194. tr{
  195. /* display: flex; */
  196. /* flex: 1; */
  197. /* margin: 10px; */
  198. /* flex: 1; */
  199. /* background-color: rgb(71, 82, 59); */
  200. }
  201. th,td{
  202. text-align:center;
  203. }

预览效果如图:

五、总结

本节课学习了flex布局的三个小案例,应用到了一下几个css属性:
1.盒模型的实现:display:flex;
2.弹性项目的属性:justify-content、align-items,这两个属性可以快速使弹性元素居中始终居中显示;
3.弹性元素属性:flex-flow(该属性是一种简写有两个值,分别表示弹性元素排列方式与是否换行)、flex(该属性是一种简写,有三个值,分别表示是否增长、是否缩减、基准尺寸)。
最后,应用所学知识,用flex做了一个简单的网站后台页面样式,受益匪浅。

Correction status:qualified

Teacher's comments:你的页面看上去超酷
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
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!