Blogger Information
Blog 22
fans 1
comment 0
visits 20161
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1226作业-用Flex布局实现二手商品组件和评论回复组件的思路与实现过程-php培训十期线上班
Dseven
Original
837 people have browsed it

1.效果图

2.设计思路

2.1分析示意图

2.2flex布局分析

作业要求为用flex来实现页面效果,flex是弹性容器,特点是具有主轴和交叉轴,项目可以按轴排列。

第一步:合并同类项,由外至内依次划分出不同的弹性容器。

这个组件从外至内大概分为红、蓝、黄、绿四级弹性容器。

按照红色容器的排布来看,是纵向排列,因此其父级容器应该是垂直不换行。

标题行与文字导航区结构相似,里面的内容按样式均可分为两块,为了便于控制,设置span标签。左右间距可以通过margin或者padding来实现,如果用了padding的方法,需要加设box-size来控制盒子尺寸。

最下面的两个蓝色容器呈现左右分区的特点,因此其父级容器设置主轴为横向不换行。

底部左边蓝色区块,平均分布着8个样式相同的绿色区块,因此该容器可以设置为横向换行。其中每个绿色区块还可以细化为图片、介绍和价格标签三块,上中下分布。

底部右边蓝色区块内部,呈现出上下两类的特点,可以将上下两部分成两个容器。

第二步:确定页面尺寸,字体大小、图片大小、容器大小

第三步:按照从上至下,从左往右,依次写出页面的DOM结构

第四步:按照同样的次序写出css样式,同步观察调整

3.页面源码

3.1HTML代码:

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <link rel="stylesheet" href="public_second_hand.css">
  9. <title>public_second_hand</title>
  10. </head>
  11. <body>
  12. <div class="public-headline">
  13. <span>二手交易</span>
  14. </div>
  15. <div class="public-second-hand">
  16. <div class="title1">
  17. <a href="">抢好货</a>
  18. <span>0低价,便捷,安全,快速</span>
  19. </div>
  20. <div class="title2">
  21. <span>热门分类</span>
  22. <a href="">美女写真</a>
  23. <a href="">日本美女</a>
  24. <a href="">美国美女</a>
  25. <a href="">国内美女</a>
  26. <a href="">AV美女</a>
  27. </div>
  28. <div class="goods">
  29. <div class="goods-list">
  30. <div class="img-info">
  31. <a href=""><img src="../../static/images/shop/shop1.jpg" alt=""></a>
  32. <a href="">美女性感写真海报墙艺术装饰画贴画图</a>
  33. <div>
  34. <span>&yen; 333</span>
  35. <span>美女</span>
  36. </div>
  37. </div>
  38. <div class="img-info">
  39. <a href=""><img src="../../static/images/shop/shop2.jpg" alt=""></a>
  40. <a href="">美女性感写真海报墙艺术装饰画贴画图2</a>
  41. <div>
  42. <span>&yen; 333</span>
  43. <span>美女</span>
  44. </div>
  45. </div>
  46. <div class="img-info">
  47. <a href=""><img src="../../static/images/shop/shop3.jpg" alt="" ></a>
  48. <a href="">美女性感写真海报墙艺术装饰画贴画图3</a>
  49. <div>
  50. <span>&yen; 333</span>
  51. <span>美女</span>
  52. </div>
  53. </div>
  54. <div class="img-info">
  55. <a href=""><img src="../../static/images/shop/shop4.jpg" alt="" ></a>
  56. <a href="">美女性感写真海报墙艺术装饰画贴画图4</a>
  57. <div>
  58. <span>&yen; 333</span>
  59. <span>美女</span>
  60. </div>
  61. </div>
  62. <div class="img-info">
  63. <a href=""><img src="../../static/images/shop/shop5.jpg" alt="" ></a>
  64. <a href="">美女性感写真海报墙艺术装饰画贴画图5</a>
  65. <div>
  66. <span>&yen; 333</span>
  67. <span>美女</span>
  68. </div>
  69. </div>
  70. <div class="img-info">
  71. <a href=""><img src="../../static/images/shop/shop6.jpg" alt="" ></a>
  72. <a href="">美女性感写真海报墙艺术装饰画贴画图6</a>
  73. <div>
  74. <span>&yen; 333</span>
  75. <span>美女</span>
  76. </div>
  77. </div>
  78. <div class="img-info">
  79. <a href=""><img src="../../static/images/shop/shop7.jpg" alt="" ></a>
  80. <a href="">美女性感写真海报墙艺术装饰画贴画图7</a>
  81. <div>
  82. <span>&yen; 333</span>
  83. <span>美女</span>
  84. </div>
  85. </div>
  86. <div class="img-info">
  87. <a href=""><img src="../../static/images/shop/shop8.jpg" alt=""></a>
  88. <a href="">美女性感写真海报墙艺术装饰画贴画图8</a>
  89. <div>
  90. <span>&yen; 333</span>
  91. <span>美女</span>
  92. </div>
  93. </div>
  94. </div>
  95. <div class="quick-entry">
  96. <a href=""><img src="../../static/images/ad/1.png" alt=""></a>
  97. <a href=""><img src="../../static/images/ad/2.png" alt=""></a>
  98. <a href=""><img src="../../static/images/ad/3.png" alt=""></a>
  99. <a href=""><img src="../../static/images/ad/4.png" alt=""></a>
  100. <div>
  101. <img src="../../static/images/ad/image.png" alt="">
  102. <img src="../../static/images/ad/ad2.jpg" alt="">
  103. </div>
  104. </div>
  105. </div>
  106. </div>
  107. </body>
  108. </html>

3.2CSS样式

  1. /*引入初始化样式表*/
  2. @import "../reset.css";
  3. @import "../public_headline/public_headline.css";
  4. /*设置最外层div样式*/
  5. .public-second-hand {
  6. display: flex;
  7. flex-flow: column nowrap;
  8. width: 1200px;
  9. margin:auto;
  10. padding: 10px;
  11. box-sizing: border-box;
  12. border-radius: 5px;
  13. background-color: #ffffff;
  14. }
  15. .public-second-hand:hover {
  16. box-shadow: 0 0 5px;
  17. }
  18. /*设置标题1样式*/
  19. .public-second-hand > .title1 {
  20. height: 50px;
  21. border-bottom: 1px solid #888888;
  22. }
  23. .public-second-hand>.title1 a{
  24. font-size:23px;
  25. margin-right: 20px;
  26. }
  27. .public-second-hand>.title1>span {
  28. font-size: 13px;
  29. color: red;
  30. }
  31. /*设置标题2样式*/
  32. .public-second-hand > .title2 {
  33. height: 55px;
  34. }
  35. .public-second-hand > .title2 > span {
  36. font-size: 23px;
  37. color: red;
  38. }
  39. .public-second-hand > .title2 > a {
  40. padding-left: 20px;
  41. }
  42. /*设置商品展示区样式*/
  43. .public-second-hand > .goods {
  44. display: flex;
  45. flex-flow: row nowrap;
  46. height: 440px;
  47. }
  48. .public-second-hand > .goods > .goods-list {
  49. padding: 10px;
  50. display: flex;
  51. flex-basis: 800px;
  52. flex-flow: row wrap;
  53. justify-content: space-between;
  54. }
  55. .public-second-hand > .goods > .goods-list .img-info {
  56. display: flex;
  57. flex-flow: column nowrap;
  58. width: 178px;
  59. height: 200px;
  60. justify-content: space-between;
  61. }
  62. .public-second-hand > .goods >.goods-list >.img-info img {
  63. width: 176px;
  64. height: 120px ;
  65. border-radius: 5px;
  66. border: 1px solid rgb(204 204 204);
  67. }
  68. .public-second-hand > .goods >.goods-list >.img-info div {
  69. display: flex;
  70. justify-content: space-between;
  71. }
  72. .public-second-hand > .goods >.goods-list >.img-info span:first-of-type{
  73. color: red;
  74. }
  75. .public-second-hand > .goods >.goods-list >.img-info span:last-of-type{
  76. color: white;
  77. background-color: lightseagreen;
  78. padding:0 5px;
  79. margin-left: auto;
  80. }
  81. /*设置快速入口样式,弹性盒子,因为父级元素是弹性容器,设置基础宽度400,容器内项目两端对齐,自动换行*/
  82. .public-second-hand > .goods >.quick-entry {
  83. display: flex;
  84. flex-basis: 400px;
  85. padding: 10px;
  86. flex-flow: row wrap;
  87. justify-content: space-between;
  88. }
  89. /*设置图片尺寸*/
  90. .public-second-hand >.goods > .quick-entry img{
  91. width: 190px;
  92. height: 130px;
  93. }
  94. /*将容器设为弹性容器,纵向排列不换行,因为其同时是上一级的弹性项目,需要设置宽度*/
  95. .public-second-hand >.goods > .quick-entry > div {
  96. display: flex;
  97. width: 400px;
  98. flex-flow:column nowrap;
  99. }
  100. /*设置图片尺寸*/
  101. .public-second-hand >.goods > .quick-entry > div img {
  102. width: 393px;
  103. height: 60px;
  104. }

4.评论回复区的页面分析图与源码

4.1页面效果图

4.2页面分析图

4.3分析思路

基本思路与上个页面相同,需要注意的是:

“我要评论”不能用行内元素的标签,只能用块元素标签,因为要设置上面的那个分隔条。

文本区域可以设置为flex:1或者flex:auto或者flex-grow:1,让其自动扩展。

在弹性盒子嵌套时,思路要清晰注意两点,一是轴向,二是区分容器与项目。

4.4源码

html代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <link rel="stylesheet" href="../../static/font/iconfont.css">
  6. <link rel="stylesheet" href="public_comment_reply.css">
  7. <title>评论回复区</title>
  8. </head>
  9. <body>
  10. <div class="public-comment-reply">
  11. <div class="comment-title">
  12. <h3>我要评论</h3>
  13. <div class="comment">
  14. <label for="comment-area" ><img src="../../static/images/user.png" alt=""></label>
  15. <input type="textarea" class="comment-area" id="comment-area" name="comment-area">
  16. </div>
  17. <button class="submit" id="submit">发表评论</button>
  18. </div>
  19. <div class="reply">
  20. <h3>最新回复</h3>
  21. <div class="userreply">
  22. <img src="../../static/images/user.png" alt="">
  23. <div>
  24. <span>用户昵称</span>
  25. <span>留言内容:PHP中文网,是一个有温度,有思想的学习平台</span>
  26. <div class="time">
  27. <span>2019-12-12 15:34:23发表</span>
  28. <span><i class="iconfont icon-dianzan"></i>回复</span>
  29. </div>
  30. </div>
  31. </div>
  32. <div class="userreply">
  33. <img src="../../static/images/user.png" alt="">
  34. <div>
  35. <span>用户昵称</span>
  36. <span>留言内容:PHP中文网,是一个有温度,有思想的学习平台</span>
  37. <div class="time">
  38. <span>2019-12-12 15:34:23发表</span>
  39. <span><i class="iconfont icon-dianzan"></i>回复</span>
  40. </div>
  41. </div>
  42. </div>
  43. <div class="userreply">
  44. <img src="../../static/images/user.png" alt="">
  45. <div>
  46. <span>用户昵称</span>
  47. <span>留言内容:PHP中文网,是一个有温度,有思想的学习平台</span>
  48. <div class="time">
  49. <span>2019-12-12 15:34:23发表</span>
  50. <span><i class="iconfont icon-dianzan"></i>回复</span>
  51. </div>
  52. </div>
  53. </div>
  54. <div class="userreply">
  55. <img src="../../static/images/user.png" alt="">
  56. <div>
  57. <span>用户昵称</span>
  58. <span>留言内容:PHP中文网,是一个有温度,有思想的学习平台</span>
  59. <div class="time">
  60. <span>2019-12-12 15:34:23发表</span>
  61. <span><i class="iconfont icon-dianzan"></i>回复</span>
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. </body>
  68. </html>

CSS代码:

  1. /*引入初始化样式*/
  2. @import url(../reset.css);
  3. /*将入口类设置为flex,纵向排列*/
  4. .public-comment-reply{
  5. display: flex;
  6. flex-direction: column;
  7. padding: 15px;
  8. box-sizing: border-box;
  9. }
  10. /*将回复区设置为flex,纵向排列*/
  11. .public-comment-reply > .comment-title {
  12. display: flex;
  13. flex-direction: column;
  14. }
  15. /*设置标题格式,此处不能用span,因为它是内联块,加上边沿时不能横向扩展到整个页面,用h3是块元素,加上边框可以扩展*/
  16. .public-comment-reply > .comment-title > h3 {
  17. border-top: 1px solid #cccccc;
  18. padding: 20px 0;
  19. box-sizing: border-box;
  20. }
  21. /*设置图像和文本框区域样式,弹性盒子,项目横向排列*/
  22. .public-comment-reply > .comment-title > .comment {
  23. display: flex;
  24. padding: 10px;
  25. }
  26. /*设置图像格式,图片加上block可以去除下面的空白区*/
  27. .public-comment-reply > .comment-title > .comment img{
  28. width: 60px;
  29. height: 60px;
  30. border-radius: 5px;
  31. justify-content: center;
  32. display: block;
  33. }
  34. /*设置文本框样式,flex:1与flex:auto效果相同,可以自动增长宽度*/
  35. .public-comment-reply > .comment-title > .comment .comment-area {
  36. height: 200px;
  37. margin-left: 20px;
  38. flex: 1;
  39. }
  40. /*设置鼠标滑过文本框的样式*/
  41. .public-comment-reply > .comment-title > .comment .comment-area:hover{
  42. box-shadow:0 0 5px #cccccc;
  43. }
  44. /*设置按钮的样式,此时的主轴为纵轴,所以交叉轴为横轴,可以用align-self来将按钮安排在最右侧,
  45. 还可以用margin-left:auto的方法达到同样党的效果*/
  46. .public-comment-reply > .comment-title > .submit {
  47. font-size: 14px;
  48. background-color: red;
  49. color: white;
  50. height: 40px;
  51. width: 150px;
  52. /*align-self: flex-end;*/
  53. margin-left: auto;
  54. border: none;
  55. }
  56. .public-comment-reply > .comment-title > .submit:hover {
  57. background-color: #178cee;
  58. }
  59. /*设置评论区样式*/
  60. .reply{
  61. display: flex;
  62. flex-direction: column;
  63. }
  64. .reply > div {
  65. display: flex;
  66. margin-top: 30px;
  67. }
  68. .reply img{
  69. width: 60px;
  70. height: 60px;
  71. border-radius: 5px;
  72. align-self: center;
  73. display: block;
  74. }
  75. .reply > div > div{
  76. display: flex;
  77. flex: 1;
  78. flex-direction: column;
  79. margin-left: 20px;
  80. height: 90px;
  81. justify-content: space-between;
  82. }
  83. .reply > div > div >div {
  84. display: flex;
  85. justify-content: space-between;
  86. }
  87. /*将点赞图标设置颜色和大小,注意大小的单位*/
  88. .reply > div > div i{
  89. color: red;
  90. font-size: 1rem;
  91. }
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:有了前面的页面详细分析, 这样写起来就事半功倍了, 相信你已经体会到了... 这里再推荐一个小技巧, 就是写css的时候, 先把页面中可能用到的所有选择器尽可能的行写出来, 然后写上注释, 在样式规则中先写上关键代码, 实现最基本功能, 再慢慢调试和完善, 最终完成作品, 我就是这样做的
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