Blogger Information
Blog 13
fans 0
comment 0
visits 10449
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
flex布局初体验以及媒体查询media的使用方法
微光
Original
1200 people have browsed it

1、用flex布局做一个简洁的PC端页面

css代码:

  1. <style>
  2. /* 初始化 */
  3. *,
  4. ::after,
  5. ::before {
  6. margin: 0;
  7. padding: 0;
  8. box-sizing: border-box;
  9. }
  10. a {
  11. text-decoration: none;
  12. color: darkgrey;
  13. }
  14. .box {
  15. display: flex;
  16. min-width: 40em;
  17. flex-flow: column nowrap;
  18. }
  19. header,
  20. footer {
  21. border: 1px solid;
  22. min-height: 4em;
  23. }
  24. /* 页眉 */
  25. header {
  26. display: flex;
  27. align-items: center;
  28. text-align: center;
  29. font-size: 1.1 rem;
  30. background-color: #000;
  31. color: #ffffff;
  32. }
  33. a > img {
  34. max-height: 4em;
  35. }
  36. header > a {
  37. flex: 0 1 8em;
  38. }
  39. header > a:hover:not(:first-of-type) {
  40. color: red;
  41. }
  42. header > a:first-of-type {
  43. margin-right: 8em;
  44. }
  45. header > a:nth-of-type(5) {
  46. margin-left: auto;
  47. text-align: end;
  48. }
  49. /* 主体 */
  50. .container {
  51. display: flex;
  52. flex-flow: row nowrap;
  53. text-align: center;
  54. min-height: 80vh;
  55. justify-content: center;
  56. }
  57. aside {
  58. flex: 0 0 100px;
  59. border: 1px solid;
  60. border-radius: 1em;
  61. }
  62. aside:first-of-type {
  63. background-color: aquamarine;
  64. }
  65. main {
  66. flex: 0 0 40em;
  67. border: 1px solid;
  68. border-radius: 1em;
  69. margin-left: 0.5em;
  70. margin-right: 0.5em;
  71. }
  72. /* 页脚 */
  73. footer {
  74. text-align: center;
  75. background-color: #000;
  76. color: #ffffff;
  77. }
  78. @media screen and (max-width: 900px) {
  79. aside:last-of-type {
  80. display: none;
  81. }
  82. }
  83. @media screen and (max-width: 700px) {
  84. aside,
  85. header > a:not(:first-of-type):not(:nth-of-type(5)):not(:last-of-type){
  86. display: none;
  87. }
  88. </style>

效果实列:
1、当宽度大于900px时,全显示

2、当宽度大于700px,小于900px时,隐藏右边栏

3、当宽度小于700px时,隐藏导航目录和左右边栏

2、做一个简单的相框

css代码:

  1. <style>
  2. * {
  3. margin: 0%;
  4. padding: 0%;
  5. box-sizing: border-box;
  6. }
  7. :root {
  8. background-color: #33daa0;
  9. }
  10. .continer {
  11. display: flex;
  12. justify-content: center;
  13. align-items: center;
  14. min-height: 100vh;
  15. flex-flow: row wrap;
  16. }
  17. .continer > .box > .content > .logo {
  18. width: 3em;
  19. }
  20. .continer > .box > .content > .tupian {
  21. max-width: 5em;
  22. border-radius: 50%;
  23. }
  24. .box {
  25. width: 15em;
  26. height: 20em;
  27. background-color: #88aa55;
  28. display: flex;
  29. margin: 1em;
  30. }
  31. .content {
  32. display: flex;
  33. justify-content: center;
  34. align-items: center;
  35. flex-flow: column nowrap;
  36. padding: 1em;
  37. }
  38. @media screen and (max-width: 820px) {
  39. p {
  40. color: red;
  41. }
  42. .continer {
  43. justify-content: space-around;
  44. }
  45. }
  46. </style>

效果实列:
1、当宽度大于820px时,居中对齐

2、当宽度小于820px时,分散对齐,字体颜色变成红色

Correcting teacher:天蓬老师天蓬老师

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