博客列表 >flex布局小案例

flex布局小案例

brait
brait原创
2019年11月07日 23:47:37714浏览

1. 将课堂介绍了三个小案例, 自己动手写一遍, 再抄一遍

DEMO1:手机端通用布局

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>手机端通用布局</title>
  6. <link rel="stylesheet" href="css/style1.css">
  7. </head>
  8. <body>
  9. <header>PHP中文网</header>
  10. <main>主体</main>
  11. <footer>
  12. <a href="">官网首页</a>
  13. <a href="">教学视频</a>
  14. <a href="" class="last-link">工具手册</a>
  15. </footer>
  16. </body>
  17. </html>

CSS部分:

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. a {
  6. text-decoration: none;
  7. color: #555555;
  8. }
  9. body {
  10. height: 100vh;
  11. display: flex;
  12. flex-flow: column nowrap;
  13. }
  14. header, footer {
  15. box-sizing: border-box;
  16. background-color: #ededed;
  17. height: 50px;
  18. display: flex;
  19. flex-flow: row nowrap;
  20. justify-content: center;
  21. align-items: center;
  22. }
  23. main {
  24. box-sizing: border-box;
  25. flex: 1;
  26. background-color: #ffffff;
  27. border-top: 1px solid #cccccc;
  28. border-bottom: 1px solid #cccccc;
  29. }
  30. footer > a {
  31. /*line-height: 50px;*/
  32. /*text-align: center;*/
  33. border-right: 1px solid white;
  34. flex: 1;
  35. display: flex;
  36. justify-content: center;
  37. align-items: center;
  38. }
  39. footer > a:last-of-type {
  40. border-right: none;
  41. }

运行效果:



DEMO2:圣杯布局

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>flex实现圣杯布局</title>
  6. <link rel="stylesheet" href="css/style2.css">
  7. </head>
  8. <body>
  9. <header>头部</header>
  10. <main>
  11. <article>内容区</article>
  12. <aside>左边栏</aside>
  13. <aside>右边栏</aside>
  14. </main>
  15. <footer>底部</footer>
  16. </body>
  17. </html>

CSS部分:

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. height: 100vh;
  7. display: flex;
  8. flex-flow: column nowrap;
  9. }
  10. header, footer {
  11. box-sizing: border-box;
  12. background-color: #ededed;
  13. height: 50px;
  14. }
  15. main {
  16. box-sizing: border-box;
  17. flex: 1;
  18. background-color: #ffffff;
  19. }
  20. main {
  21. display: flex;
  22. }
  23. main > aside {
  24. box-sizing: border-box;
  25. width: 200px;
  26. background-color: wheat;
  27. }
  28. main > article {
  29. box-sizing: border-box;
  30. flex: 1;
  31. background-color: lightblue;
  32. }
  33. main > aside:first-of-type {
  34. order: -1;
  35. }

运行效果:



DEMO3:酷炫的登录界面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>弹性布局实现登录表单</title>
  6. <link rel="stylesheet" href="css/style3.css">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <h3>管理员登陆</h3>
  11. <form action="">
  12. <div>
  13. <label for="email">邮箱:</label>
  14. <input type="email" id="email" name="email" placeholder="example@email.com">
  15. </div>
  16. <div>
  17. <label for="password">密码:</label>
  18. <input type="password" id="password" name="password" placeholder="不少于6位">
  19. </div>
  20. <div>
  21. <button>提交</button>
  22. </div>
  23. </form>
  24. </div>
  25. </body>
  26. </html>

CSS部分

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

运行效果:




2. 自己根据自己情况, 自定义一个小案例, 使用flex实现, 例如网站后台首页

demo:仿网易邮箱登录界面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>仿某易邮箱</title>
  6. <link rel="stylesheet" href="css/style.css">
  7. </head>
  8. <body>
  9. <header>网易邮箱中文邮箱第一品牌</header>
  10. <main>
  11. <div class="log">
  12. <h2>邮箱账号登录</h2>
  13. <form>
  14. <div>
  15. <label for="email">邮箱</label>
  16. <input type="email" id="email" name="email" placeholder="example@163.com">
  17. </div>
  18. <div>
  19. <label for="password">密码</label>
  20. <input type="password" id="password" name="password" placeholder="不少于6位数">
  21. </div>
  22. <div>
  23. <button>登录</button>
  24. </div>
  25. <div>
  26. <a href="">注册新账户</a>
  27. </div>
  28. </form>
  29. </div>
  30. </main>
  31. <footer>
  32. <a href="">网易首页</a>
  33. <a href="">网易严选</a>
  34. <a href="">网易有钱</a>
  35. <a href="">政府公益热线</a>
  36. <a href="">隐私政策</a>
  37. </footer>
  38. </body>
  39. </html>

CSS部分:

  1. *{
  2. padding: 0;
  3. margin: 0;
  4. }
  5. body{
  6. height: 100vh;
  7. /*将整个页面转为弹性盒子FlexBox*/
  8. display: flex;
  9. /*主轴垂直且不换行*/
  10. flex-flow: column nowrap;
  11. }
  12. main{
  13. background: linear-gradient(to right , #0033ff,#0099ff,#0033ff);
  14. display: flex;
  15. flex: 1;
  16. justify-content: center;
  17. align-items: center;
  18. }
  19. main > div {
  20. height : 500px;
  21. width : 400px;
  22. display: flex;
  23. flex-flow: column nowrap;
  24. background-color: white;
  25. position: relative;
  26. right: -300px;
  27. }
  28. main > div > h2{
  29. margin-top:30px;
  30. margin-bottom:30px;
  31. text-align: center;
  32. }
  33. form > div {
  34. display: flex;
  35. padding: 13px;
  36. }
  37. form > div >input {
  38. flex:1;
  39. margin-left: 10px;
  40. }
  41. form > div > button {
  42. flex: 1;
  43. background-color:rgb(0, 102, 255);
  44. color: white;
  45. height: 24px;
  46. letter-spacing: 25px;
  47. border: none;
  48. border-radius: 8px;
  49. }
  50. button:hover{
  51. box-shadow: 0 0 5px #888;
  52. }
  53. form > div:last-of-type {
  54. justify-content: center;
  55. align-items: center;
  56. }
  57. form > div > a{
  58. text-decoration: none;
  59. }
  60. header,footer{
  61. height: 70px;
  62. }
  63. footer{
  64. display: flex;
  65. justify-content: center;
  66. align-items: center;
  67. }
  68. footer > a {
  69. text-decoration: none;
  70. display: flex;
  71. flex: 1;
  72. border-right: 1px solid lightgray;
  73. justify-content: center;
  74. align-items: center;
  75. }
  76. footer > a:link{
  77. color: gray;
  78. }
  79. footer > a:visited{
  80. color: gray;
  81. }
  82. footer > a:last-of-type{
  83. border-right: none;
  84. }

效果对比



总结:吧标签设置成弹性盒子,再对弹性盒子进行定义。这种方法写网页结构要简单一些,最主要的是层层嵌套的盒子不要混淆。最后,网易估计要恨死我了,模仿的那么丑。。。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议