博客列表 >使用flex实现pc端三列布局与模仿(m.php.cn)首页

使用flex实现pc端三列布局与模仿(m.php.cn)首页

Tlilam的PHP之路
Tlilam的PHP之路原创
2020年08月11日 20:28:541051浏览

flex实现PC三列布局

HTML代码:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>flex实现PC三列布局</title>
  5. </head>
  6. <body>
  7. <header>
  8. <a href=""><img src="https://dgss0.bdstatic.com/5bVWsj_p_tVS5dKfpU_Y_D3/res/r/image/2017-09-27/297f5edb1e984613083a2d3cc0c5bb36.png"></a>
  9. <a href="">首頁</a>
  10. <a href="">熱門</a>
  11. <a href="">推薦</a>
  12. <a href="">推荐</a>
  13. <a href="">登录</a>
  14. </header>
  15. <div class="container">
  16. <aside>左边栏</aside>
  17. <main>中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区中心内容区</main>
  18. <aside>右边栏</aside>
  19. </div>
  20. <footer>
  21. <p>******版权所有 &copy; 备案*********</p>
  22. <p>地址:*****************</p>
  23. </footer>
  24. </body>
  25. </html>

CSS代码:

  1. *{
  2. margin: 0px;
  3. padding: 0px;
  4. box-sizing: border-box;
  5. }
  6. body{
  7. /*自适应 不设宽度 */
  8. display: flex;
  9. flex-flow: column nowrap;
  10. /*border: 1px solid green;*/
  11. }
  12. header{
  13. height: 50px;
  14. display: flex;
  15. background-color: black;
  16. /*header默认是flex-flow:row方向 nowrap不换行*/
  17. /*设置交叉轴的对齐方向 居中*/
  18. align-items: center;
  19. }
  20. /*设置a链接样式*/
  21. a{
  22. text-decoration: none;
  23. color: white;
  24. }
  25. /*设置header下的A项目 基本属性:增长比例 缩减比例 默认大小 */
  26. header > a{
  27. /*设置项目 不可以增长 可以缩减 宽度100px*/
  28. flex: 0 1 100px;
  29. /*文本内容居中*/
  30. text-align: center;
  31. }
  32. /*设置logo图片大小*/
  33. header > a >img{
  34. width: 60%;
  35. }
  36. /*将logo撑开 是导航元素向右推*/
  37. header > a:first-of-type{
  38. /* 方法一:margin-right 把导航元素向右推 */
  39. /*margin-right: 50px;*/
  40. /* 方法二:给flex 不增长 不缩减 基础宽度定200px*/
  41. flex: 0 0 200px;
  42. }
  43. /*margin-left:auto 将“登陆按钮”向最右边推*/
  44. header > a:last-of-type{
  45. margin-left: auto;
  46. }
  47. /*排除第一个A元素的鼠标经过效果*/
  48. header > a:hover:not(:first-of-type){
  49. color: orange;
  50. }
  51. /*主题区设置*/
  52. .container{
  53. /*设置flex*/
  54. display: flex;
  55. /*最小高度支撑显示*/
  56. min-height: 400px;
  57. /*主轴的对齐方式:两端对齐*/
  58. justify-content: space-between;
  59. }
  60. /*左右栏 */
  61. .container > aside{
  62. background-color: green;
  63. /*设置不增长 不缩减 基本宽度200px*/
  64. flex: 0 0 200px;
  65. }
  66. /*设置中心内容区*/
  67. .container > main {
  68. /*开启不增长 不缩减 基本宽度600px*/
  69. flex:0 0 600px;
  70. }
  71. /*底部设置*/
  72. footer{
  73. height: 100px;
  74. background-color: black;
  75. color: white;
  76. /*设置flex*/
  77. display: flex;
  78. /*设置主轴:column方向 nowarp不换行*/
  79. flex-flow: column nowrap;
  80. /*主轴 水平居中*/
  81. justify-content: center;
  82. /*文本内容居中*/
  83. text-align: center;
  84. /*和上一样 达到居中效果*/
  85. /*align-items: center;*/
  86. }
  87. /*媒体监控 */
  88. /*当屏幕宽度小于900PX,隐藏右边栏,中间内容区自适应*/
  89. @media screen and (max-width: 900px){
  90. aside:last-of-type{
  91. display: none;
  92. }
  93. /* 主体区自适应大小变化 */
  94. .container > main {
  95. flex: auto;
  96. }
  97. }
  98. /*当屏幕小于700px,隐藏导航,左右边栏,页脚*/
  99. @media screen and (max-width: 700px){
  100. footer,aside,header > a:not(:first-of-type):not(:last-of-type){
  101. display: none;
  102. }
  103. }

默认运行结果:

监控max-width:900px:

监控max-width:700px:

flex模仿m.php.cn首页

HTML代码:

  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. <link rel="stylesheet" href="static/font_icon/iconfont.css">
  7. <title>flex模仿m.php.cn</title>
  8. </head>
  9. <body>
  10. <header>
  11. <a href=""><img src="https://img.php.cn/upload/avatar/000/012/411/59832646266b4495.jpg" alt=""></a>
  12. <a href=""><img src="https://m.php.cn/static/images/logo.png" alt=""></a>
  13. <span class="iconfont">&#xe61f;</span>
  14. </header>
  15. <div class="slider">
  16. <img src="https://m.php.cn/static/images/ico/1.jpg" alt="">
  17. </div>
  18. <nav>
  19. <div>
  20. <a href=""><img src="https://m.php.cn/static/images/ico/html.png" alt=""></a>
  21. <a href="">HTML/CSS</a>
  22. </div>
  23. <div>
  24. <a href=""><img src="https://m.php.cn/static/images/ico/html.png" alt=""></a>
  25. <a href="">HTML/CSS</a>
  26. </div>
  27. <div>
  28. <a href=""><img src="https://m.php.cn/static/images/ico/html.png" alt=""></a>
  29. <a href="">HTML/CSS</a>
  30. </div>
  31. <div>
  32. <a href=""><img src="https://m.php.cn/static/images/ico/html.png" alt=""></a>
  33. <a href="">HTML/CSS</a>
  34. </div>
  35. <div>
  36. <a href=""><img src="https://m.php.cn/static/images/ico/html.png" alt=""></a>
  37. <a href="">HTML/CSS</a>
  38. </div>
  39. <div>
  40. <a href=""><img src="https://m.php.cn/static/images/ico/html.png" alt=""></a>
  41. <a href="">HTML/CSS</a>
  42. </div>
  43. <div>
  44. <a href=""><img src="https://m.php.cn/static/images/ico/html.png" alt=""></a>
  45. <a href="">HTML/CSS</a>
  46. </div>
  47. <div>
  48. <a href=""><img src="https://m.php.cn/static/images/ico/html.png" alt=""></a>
  49. <a href="">HTML/CSS</a>
  50. </div>
  51. </nav>
  52. <h2 class="title">推荐课程</h2>
  53. <div class="recommend">
  54. <div class="course-img">
  55. <div><a href=""><img src="https://img.php.cn/upload/course/000/000/001/5d242759adb88970.jpg" alt=""></a></div>
  56. <div><a href=""><img src="https://img.php.cn/upload/course/000/000/001/5d242759adb88970.jpg" alt=""></a></div>
  57. </div>
  58. <div class="course-img">
  59. <a href=""><img src="https://img.php.cn/upload/course/000/000/035/5d2941e265889366.jpg" alt=""></a>
  60. <div>
  61. <h2><a href="">CI框架30分钟极速入门</a></h2>
  62. <span>中级</span>
  63. <span>350887播放</span>
  64. </div>
  65. </div>
  66. <div class="course-img">
  67. <a href=""><img src="https://img.php.cn/upload/course/000/126/153/5aa23f0ded921649.jpg" alt=""></a>
  68. <div>
  69. <h2><a href="">2018前端入门_HTML5</a></h2>
  70. <span>初级</span>
  71. <span>350887播放</span>
  72. </div>
  73. </div>
  74. </div>
  75. <h2>最新更新</h2>
  76. <div class="new">
  77. <div class="new-desc">
  78. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5f155b2f296de744.png" alt=""></a>
  79. <div class="new-info">
  80. <h2><a href="">
  81. 《我的十年撸码生涯》
  82. </a></h2>
  83. <p class="new-info-intro">主题:《十年撸码生涯:聊聊没羞没臊的工作和生活》主讲:西门大官人(php中文网的人肉CPU) 特邀嘉宾: 灭绝小师太(php中文网唯一美女讲师) 时间:2020.7.21 晚20:00 全国直播</p>
  84. <div class="new-info-zy">
  85. <span>初级</span>
  86. <span>350887播放</span>
  87. </div>
  88. </div>
  89. </div>
  90. <div class="new-desc">
  91. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5f155b2f296de744.png" alt=""></a>
  92. <div class="new-info">
  93. <h2><a href="">
  94. 《我的十年撸码生涯》
  95. </a></h2>
  96. <p class="new-info-intro">主题:《十年撸码生涯:聊聊没羞没臊的工作和生活》主讲:西门大官人(php中文网的人肉CPU) 特邀嘉宾: 灭绝小师太(php中文网唯一美女讲师) 时间:2020.7.21 晚20:00 全国直播</p>
  97. <div class="new-info-zy">
  98. <span>初级</span>
  99. <span>350887播放</span>
  100. </div>
  101. </div>
  102. </div>
  103. <div class="new-desc">
  104. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5f155b2f296de744.png" alt=""></a>
  105. <div class="new-info">
  106. <h2><a href="">
  107. 《我的十年撸码生涯》
  108. </a></h2>
  109. <p class="new-info-intro">主题:《十年撸码生涯:聊聊没羞没臊的工作和生活》主讲:西门大官人(php中文网的人肉CPU) 特邀嘉宾: 灭绝小师太(php中文网唯一美女讲师) 时间:2020.7.21 晚20:00 全国直播</p>
  110. <div class="new-info-zy">
  111. <span>初级</span>
  112. <span>350887播放</span>
  113. </div>
  114. </div>
  115. </div>
  116. </div>
  117. <footer>
  118. <a href="">
  119. <span class="iconfont hot">&#xe60c;</span>
  120. <span>首页</span>
  121. </a>
  122. <a href="">
  123. <span class="iconfont hot">&#xe64b;</span>
  124. <span>视频</span>
  125. </a>
  126. <a href="">
  127. <span class="iconfont hot">&#xe602;</span>
  128. <span>社区</span>
  129. </a>
  130. <a href="">
  131. <span class="iconfont hot">&#xe65b;</span>
  132. <span>我的</span>
  133. </a>
  134. </footer>
  135. </body>
  136. </html>

css代码:

  1. /* 初始化设置 */
  2. *{
  3. padding: 0;
  4. margin: 0;
  5. box-sizing: border-box;
  6. }
  7. /* 设置链接的基本样式 */
  8. a{
  9. text-decoration: none;
  10. color: #888888;
  11. }
  12. /* 设置宽高为窗口宽高 */
  13. html{
  14. width: 100vw;
  15. height: 100vh;
  16. color: #888888;
  17. font-size: 12px;
  18. }
  19. /* 将body转为flex 以垂直为主轴 不换行 */
  20. body{
  21. /* 设置最小宽度 */
  22. min-width: 400px;
  23. background-color: #edeff0;
  24. display: flex;
  25. flex-flow: column nowrap ;
  26. }
  27. /* 设置顶部转为flex*/
  28. body > header{
  29. background-color: #2d353c;
  30. width: 100vw;
  31. display: flex;
  32. /* 主轴对齐:两端对齐 */
  33. justify-content: space-between;
  34. /* 交叉轴对齐:居中 */
  35. align-items: center;
  36. padding: 0 10px;
  37. height: 45px;
  38. /* 固定定位在顶部 */
  39. position: fixed;
  40. }
  41. /* 设置右边图标的颜色与大小 */
  42. header > .iconfont{
  43. color: white;
  44. font-size: 1.6rem;
  45. }
  46. /* 设置头像图的圆角,边框,高度 */
  47. header > a:first-of-type > img{
  48. border-radius: 50%;
  49. border: 1px solid #8F9498;
  50. height: 25px;
  51. }
  52. /* 设置LOGO图 的高度 */
  53. header > a:last-of-type > img{
  54. height: 45px;
  55. }
  56. /* 轮播图 */
  57. .slider {
  58. /* 距离顶部45PX 以免给固定的顶部header遮挡 */
  59. margin-top: 45px;
  60. /* 图像高度150px */
  61. height: 150px;
  62. }
  63. /* 设置导航图像100% */
  64. .slider > img{
  65. height: 100%;
  66. width: 100%;
  67. }
  68. /* 导航栏 */
  69. nav{
  70. height: 200px;
  71. /* 设置flex */
  72. display: flex;
  73. /* flex 水平方向,换行 */
  74. flex-flow: row wrap;
  75. /* 项目交叉轴分散对齐 */
  76. align-content: space-around;
  77. /* margin-top: 10px; */
  78. }
  79. /* 设置Nav下的项目 为flex */
  80. nav > div{
  81. display: flex;
  82. width: 25vw;
  83. /* flex 垂直为主轴 不换行 */
  84. flex-flow: column nowrap;
  85. /* 项目交叉轴居中 */
  86. align-items: center;
  87. }
  88. nav > div img{
  89. /* 设置图片大小 */
  90. width: 50%;
  91. }
  92. /* 设置div下第一个a元素图片 文本居中 */
  93. nav > div > a:first-of-type{
  94. text-align: center;
  95. }
  96. /* 栏目标题 */
  97. .title{
  98. padding: 10px;
  99. }
  100. /* 推荐课程 */
  101. .recommend{
  102. display: flex;
  103. flex-flow: column nowrap;
  104. padding: 10px;
  105. }
  106. /* 设置推荐下的图片宽度100% */
  107. .recommend img{
  108. width: 100%;
  109. }
  110. /* recommend下的项目course-img设置flex */
  111. .recommend > .course-img{
  112. display: flex;
  113. padding: 5px;
  114. }
  115. /* 设置第一个div可增长 可收缩 */
  116. .recommend > .course-img:first-of-type >div{
  117. flex:1;
  118. padding: 0 10px 0 0;
  119. }
  120. .recommend > .course-img:not(:first-of-type) > a{
  121. /* 设置图片区 初始化占45% */
  122. flex-basis: 45%;
  123. }
  124. .recommend > .course-img:not(:first-of-type) > div{
  125. /* 设置文本区 初始化占55% */
  126. flex-basis: 55%;
  127. }
  128. /* 设置文本区内的H2样式 */
  129. div > h2{
  130. color: #888888;
  131. font-size: 1.3rem;
  132. margin-bottom: 10px;
  133. font-weight: normal;
  134. }
  135. /* 设置文本区,等级的显示样式 */
  136. .recommend > .course-img > div span:first-of-type{
  137. color: white;
  138. background-color: #595757;
  139. border-radius: 8px;
  140. padding: 3px;
  141. }
  142. /* 最新更新 */
  143. .new{
  144. display: flex;
  145. /* flex 垂直为主轴 不换行 */
  146. flex-flow: column nowrap;
  147. padding: 10px;
  148. /* 后面是footer 增加一个底部margin 支撑内容不被底部栏遮挡 */
  149. margin-bottom: 40px;
  150. }
  151. /* NEW容器下的项目设置为flex */
  152. .new > .new-desc{
  153. display: flex;
  154. margin: 10px;
  155. }
  156. /* 设置new-desc下的图片最大高度不超过100px */
  157. .new > .new-desc > a >img{
  158. border-radius: 10px;
  159. max-height: 100px;
  160. }
  161. /* 设置new-desc下div项目 */
  162. .new > .new-desc >div{
  163. padding: 0 15px;
  164. /* 可增长 可缩减 初始化大小自动 */
  165. flex: auto;
  166. }
  167. /* 设置图片高度100% */
  168. .new img{
  169. height: 100%;
  170. }
  171. /* 设置简介超出用省略号代替 */
  172. .new-info-intro{
  173. width: 200px;
  174. text-overflow: ellipsis;
  175. overflow: hidden;
  176. white-space: nowrap;
  177. flex: 1 0 200px;
  178. }
  179. /* 设置视频等级 样式 */
  180. .new-info-zy > span:first-of-type{
  181. color: white;
  182. background-color: #595757;
  183. border-radius: 8px;
  184. padding: 3px;
  185. }
  186. /* 设置播放数量向右移动 */
  187. .new-info-zy > span:last-of-type{
  188. margin-left: 45%;
  189. }
  190. /* 页脚 */
  191. footer{
  192. /* 设置flex */
  193. display: flex;
  194. /* 绝对定位 bottom:0 定在底部 */
  195. position: fixed;
  196. bottom: 0;
  197. background-color: #edeff0;
  198. height: 55px;
  199. width: 100vw;
  200. /* 主轴对齐方式 分散对齐 */
  201. justify-content: space-around;
  202. /* 交叉轴对齐方式 居中 */
  203. align-items: center;
  204. }
  205. /* footer下的项目 再设置成flex */
  206. footer >a{
  207. display: flex;
  208. /* flex方向以垂直为主轴 */
  209. flex-flow: column;
  210. /* 项目交叉轴对齐方式 居中 */
  211. align-items: center;
  212. }
  213. footer > a:hover{
  214. color:#ff0000;
  215. }
  216. /* 设置字体和图标大小 */
  217. footer >a >span{
  218. font-size: 1.2rem;
  219. }

运行结果:

总结

  1. flex布局使用不是很难,但是flex-basisflex-grow,使用还不够好,窗口宽度变动的时候,图片区与文本区的自适应变动还没有做好
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议