Blogger Information
Blog 6
fans 0
comment 0
visits 6331
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
CSS flex 弹性布局和 grid 网格布局
J
Original
1477 people have browsed it

1.flex 弹性布局

flex 弹性盒子布局是一种按行或按列布局元素的方法,元素可以膨胀或者收缩来适应空间

  • 使用 flex 布局实现 PC 端三列布局
  1. <body>
  2. <!--页眉-->
  3. <header>
  4. <a href="">LOGO</a>
  5. <a href="">新闻</a>
  6. <a href="">视频</a>
  7. <a href="">地图</a>
  8. <a href="">贴吧</a>
  9. <a href="">学术</a>
  10. <a href="">更多</a>
  11. <a href="">登录</a>
  12. </header>
  13. <!--主体-->
  14. <div class="container">
  15. <!--左边栏-->
  16. <aside>左边栏</aside>
  17. <!--主题内容区-->
  18. <main>主体</main>
  19. <!--右边栏-->
  20. <aside>右边栏</aside>
  21. </div>
  22. <!--页脚-->
  23. <footer>
  24. <div class="about">
  25. <a href="">关于我们</a>
  26. <a href="">常见问题</a>
  27. <a href="">反馈意见</a>
  28. </div>
  29. <p>Copyright © 2017-2020 xxxx 版权所有</p>
  30. </footer>
  31. </body>

CSS 样式如下:

  1. /*初始化*/
  2. * {
  3. padding: 0;
  4. margin: 0;
  5. box-sizing: border-box;
  6. }
  7. a {
  8. text-decoration: none;
  9. color: #666;
  10. }
  11. body {
  12. width: 1050px;
  13. /*转化为flex弹性盒子*/
  14. display: flex;
  15. /*主轴垂直*/
  16. flex-flow: column nowrap;
  17. margin: auto;
  18. background-color: white;
  19. }
  20. header,
  21. footer {
  22. height: 50px;
  23. background-color: lightblue;
  24. }
  25. header {
  26. /*页头转换为flex容器*/
  27. display: flex;
  28. /*水平对齐*/
  29. /*justify-content: center;*/
  30. /*垂直对齐*/
  31. align-items: center;
  32. }
  33. header > a {
  34. /*禁止放大,允许缩小*/
  35. flex: 0 1 100px;
  36. /*文本居中*/
  37. text-align: center;
  38. }
  39. header > a:hover:not(:first-of-type) {
  40. color: red;
  41. }
  42. header > a:first-of-type {
  43. margin-right: 50px;
  44. }
  45. header > a:last-of-type {
  46. margin-left: auto;
  47. }
  48. .container {
  49. display: flex;
  50. min-height: 500px;
  51. justify-content: space-between;
  52. /*flex-flow: row nowrap;*/
  53. }
  54. .container > aside {
  55. flex: 0 0 200px;
  56. background-color: lightyellow;
  57. }
  58. .container > main {
  59. flex: 0 0 600px;
  60. background-color: wheat;
  61. }
  62. footer {
  63. display: flex;
  64. flex-flow: column nowrap;
  65. /*水平垂直居中*/
  66. text-align: center;
  67. justify-content: center;
  68. }
  69. footer > .about {
  70. display: flex;
  71. justify-content: center;
  72. }
  73. footer > .about > a {
  74. /*设置左右边距*/
  75. margin: 0 20px;
  76. }
  77. footer > .about > a:hover {
  78. color: violet;
  79. }
  80. /*媒体查询*/
  81. /*当最大宽度900是,隐藏右边栏*/
  82. @media screen and (max-width: 900px) {
  83. .container > aside:last-of-type {
  84. display: none;
  85. }
  86. main {
  87. flex: 1 0 auto;
  88. }
  89. }
  90. /*当最大宽度700是,隐藏右边栏*/
  91. @media screen and (max-width: 700px) {
  92. .container > aside,
  93. header > a:not(:first-of-type):not(:last-of-type),
  94. footer {
  95. display: none;
  96. }
  97. /*登录按钮左边距350px*/
  98. header > a:last-of-type {
  99. margin-left: 350px;
  100. }
  101. }

代码运行结果图:

  • flex 实现(m.php.cn)首页
  1. <body>
  2. <!--顶部-->
  3. <header>
  4. <a href="" class="iconfont icon">&#xe65b;</a>
  5. <img src="static/images/logo.png" alt="" />
  6. <span class="iconfont icon">&#xe61f;</span>
  7. </header>
  8. <!--轮播图-->
  9. <div class="slider">
  10. <img src="static/images/slider.png" alt="" />
  11. </div>
  12. <!--主导航区-->
  13. <nav>
  14. <div>
  15. <a href=""><img src="static/images/nav_1.png" alt="" /></a>
  16. <a href="">HTML/CSS</a>
  17. </div>
  18. <div>
  19. <a href=""><img src="static/images/nav_2.png" alt="" /></a>
  20. <a href="">JavaScript</a>
  21. </div>
  22. <div>
  23. <a href=""><img src="static/images/nav_3.png" alt="" /></a>
  24. <a href="">服务端</a>
  25. </div>
  26. <div>
  27. <a href=""><img src="static/images/nav_3.png" alt="" /></a>
  28. <a href="">数据库</a>
  29. </div>
  30. <div>
  31. <a href=""><img src="static/images/nav_5.png" alt="" /></a>
  32. <a href="">移动端</a>
  33. </div>
  34. <div>
  35. <a href=""><img src="static/images/nav_6.png" alt="" /></a>
  36. <a href="">手册</a>
  37. </div>
  38. <div>
  39. <a href=""><img src="static/images/nav_7.png" alt="" /></a>
  40. <a href="">工具</a>
  41. </div>
  42. <div>
  43. <a href=""><img src="static/images/nav_8.png" alt="" /></a>
  44. <a href="">直播</a>
  45. </div>
  46. </nav>
  47. <!--推荐课程-->
  48. <div class="box recommend">
  49. <h3 class="title">推荐课程</h3>
  50. <div class="course_box">
  51. <div class="box_left">
  52. <img src="static/images/course_1.jpg" alt="" />
  53. </div>
  54. <div class="box_right">
  55. <img src="static/images/course_2.jpg" alt="" />
  56. </div>
  57. </div>
  58. <div class="course_inline">
  59. <a href=""><img src="static/images/course_3.jpg" alt="" /></a>
  60. <div class="course_intro">
  61. <h2>
  62. <a href="">CI框架30分钟极速入门</a>
  63. </h2>
  64. <p>
  65. <span class="tag">中级</span>
  66. <span>161651次播放</span>
  67. </p>
  68. </div>
  69. </div>
  70. <div class="course_inline">
  71. <a href=""><img src="static/images/course_4.jpg" alt="" /></a>
  72. <div class="course_intro">
  73. <h2>
  74. <a href="">2018前端入门_HTML5</a>
  75. </h2>
  76. <p>
  77. <span class="tag">初级</span>
  78. <span>161651次播放</span>
  79. </p>
  80. </div>
  81. </div>
  82. </div>
  83. <!--最近更新-->
  84. <div class="box update">
  85. <h3 class="title">最近更新</h3>
  86. <div class="update_list">
  87. <a href=""><img src="static/images/u_1.png" alt="" /></a>
  88. <div class="update_intro">
  89. <h2><a href="">《我的十年撸码生涯》公益直播课</a></h2>
  90. <p>主题:《十年撸码生涯:聊聊没羞没臊的工作...</p>
  91. <p><span class="tag">初级</span><span>1616次播放</span></p>
  92. </div>
  93. </div>
  94. <div class="update_list">
  95. <a href=""><img src="static/images/u_2.png" alt="" /></a>
  96. <div class="update_intro">
  97. <h2><a href="">2天速成VueJS免费公益直播课</a></h2>
  98. <p>php中文网免费公益直播课:两天四个小时Vue...</p>
  99. <p><span class="tag">初级</span><span>1616次播放</span></p>
  100. </div>
  101. </div>
  102. <div class="update_list">
  103. <a href=""><img src="static/images/u_3.png" alt="" /></a>
  104. <div class="update_intro">
  105. <h2><a href="">PHP代码整洁之道</a></h2>
  106. <p>本课程参考自 Robert C. Martin的Clean Code 书...</p>
  107. <p><span class="tag">中级</span><span>1616次播放</span></p>
  108. </div>
  109. </div>
  110. <div class="update_list">
  111. <a href=""><img src="static/images/u_4.png" alt="" /></a>
  112. <div class="update_intro">
  113. <h2><a href="">前端课程(五郞八卦棍系列)第...</a></h2>
  114. <p>本视频课程是Web开发的基础课程, 主要讲解H...</p>
  115. <p><span class="tag">初级</span><span>1616次播放</span></p>
  116. </div>
  117. </div>
  118. <div class="update_list">
  119. <a href=""><img src="static/images/u_5.png" alt="" /></a>
  120. <div class="update_intro">
  121. <h2><a href="">小皮面板使用视频教程</a></h2>
  122. <p>小皮面板(phpstudy-linux面板 )针对Linux服...</p>
  123. <p><span class="tag">初级</span><span>1616次播放</span></p>
  124. </div>
  125. </div>
  126. <div class="update_list">
  127. <a href=""><img src="static/images/u_6.png" alt="" /></a>
  128. <div class="update_intro">
  129. <h2><a href="">从零进入C语言</a></h2>
  130. <p>本课程带你从零进入C语言,课程内容包括Linux...</p>
  131. <p><span class="tag">初级</span><span>1616次播放</span></p>
  132. </div>
  133. </div>
  134. </div>
  135. <!--最新文章-->
  136. <article class="box">
  137. <h3 class="title">最新文章</h3>
  138. <div class="article_list">
  139. <div class="article_left">
  140. <h2><a href="">php mysql 并发解决方案</a></h2>
  141. <span>发布时间:2020-08-12</span>
  142. </div>
  143. <div class="article_right">
  144. <a href=""><img src="static/images/a_1.jpg" alt="" /></a>
  145. </div>
  146. </div>
  147. <div class="article_list">
  148. <div class="article_left">
  149. <h2><a href="">微信号被投诉了能查到是谁投诉的吗</a></h2>
  150. <span>发布时间:2020-08-12</span>
  151. </div>
  152. <div class="article_right">
  153. <a href=""><img src="static/images/a_2.jpg" alt="" /></a>
  154. </div>
  155. </div>
  156. <div class="article_list">
  157. <div class="article_left">
  158. <h2><a href="">php如何使用正则替换img src</a></h2>
  159. <span>发布时间:2020-08-12</span>
  160. </div>
  161. <div class="article_right">
  162. <a href=""><img src="static/images/a_1.jpg" alt="" /></a>
  163. </div>
  164. </div>
  165. <div class="article_list">
  166. <div class="article_left">
  167. <h2><a href="">php如何实现订单自动取消</a></h2>
  168. <span>发布时间:2020-08-12</span>
  169. </div>
  170. <div class="article_right">
  171. <a href=""><img src="static/images/a_3.jpg" alt="" /></a>
  172. </div>
  173. </div>
  174. <div class="article_list">
  175. <div class="article_left">
  176. <h2><a href="">java常用代码有哪些</a></h2>
  177. <span>发布时间:2020-08-12</span>
  178. </div>
  179. <div class="article_right">
  180. <a href=""><img src="static/images/a_4.jpg" alt="" /></a>
  181. </div>
  182. </div>
  183. <div class="more"><a href="">更多内容</a></div>
  184. </article>
  185. <!--最新博文-->
  186. <div class="box blog">
  187. <h3 class="title">最新博文</h3>
  188. <div class="blog_list">
  189. <a href="">
  190. <h2>querylist采集</h2>
  191. <span>2020-08-10</span>
  192. </a>
  193. </div>
  194. <div class="blog_list">
  195. <a href="">
  196. <h2>酱茄社区论坛商城小程序pro更新与WordPress小程...</h2>
  197. <span>2020-08-10</span>
  198. </a>
  199. </div>
  200. <div class="blog_list">
  201. <a href="">
  202. <h2>showdoc 自动生成API文档</h2>
  203. <span>2020-08-10</span>
  204. </a>
  205. </div>
  206. <div class="blog_list">
  207. <a href="">
  208. <h2>私有版 showdoc 文档管理工具</h2>
  209. <span>2020-08-10</span>
  210. </a>
  211. </div>
  212. <div class="blog_list">
  213. <a href="">
  214. <h2>Windows服务器搭建网站步骤 iis配置网站 举栗子...</h2>
  215. <span>2020-08-10</span>
  216. </a>
  217. </div>
  218. <div class="more"><a href="">更多内容</a></div>
  219. </div>
  220. <!--最新问答-->
  221. <div class="box question">
  222. <h3 class="title">最新问答</h3>
  223. <div class="question_list">
  224. <a href="">
  225. <h2>函数与结构体</h2>
  226. <span>2020-08-12</span>
  227. </a>
  228. </div>
  229. <div class="question_list">
  230. <a href="">
  231. <h2>我把laravel里面的东西拷贝到其他盘了,然后在...</h2>
  232. <span>2020-08-12</span>
  233. </a>
  234. </div>
  235. <div class="question_list">
  236. <a href="">
  237. <h2>在我的Terminal中好像不提示输入的信息,但是...</h2>
  238. <span>2020-08-12</span>
  239. </a>
  240. </div>
  241. <div class="question_list">
  242. <a href="">
  243. <h2>我也是使用镜像安装的,但不是按照咱们这样在...</h2>
  244. <span>2020-08-12</span>
  245. </a>
  246. </div>
  247. <div class="question_list">
  248. <a href="">
  249. <h2>想自己做网站有会做的师傅吗薪水丰厚</h2>
  250. <span>2020-08-12</span>
  251. </a>
  252. </div>
  253. <div class="more"><a href="">更多内容</a></div>
  254. </div>
  255. <!--底部-->
  256. <footer>
  257. <a href="" class="active"
  258. ><span class="iconfont">&#xe60c;</span><span>首页</span></a
  259. >
  260. <a href=""><span class="iconfont">&#xe60a;</span><span>视频</span></a>
  261. <a href=""><span class="iconfont">&#xe64b;</span><span>社区</span></a>
  262. <a href=""><span class="iconfont">&#xe65b;</span><span>我的</span></a>
  263. </footer>
  264. </body>

CSS 代码如下

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. /*box-sizing: border-box;*/
  5. }
  6. a {
  7. text-decoration: none;
  8. color: #666;
  9. }
  10. a:hover {
  11. color: black;
  12. }
  13. html {
  14. /*调整宽高为视口宽高*/
  15. width: 100vw;
  16. height: 100vh;
  17. font-size: 14px;
  18. color: #666;
  19. }
  20. body {
  21. display: flex;
  22. /*整个主轴垂直*/
  23. flex-flow: column nowrap;
  24. min-width: 360px;
  25. background-color: #edeff0;
  26. }
  27. body > header {
  28. display: flex;
  29. width: 95vw;
  30. background-color: #333;
  31. color: #fff;
  32. /*水平两端对齐*/
  33. justify-content: space-between;
  34. height: 42px;
  35. /*交叉轴方向居中*/
  36. align-items: center;
  37. /*左右15px边距*/
  38. padding: 0 15px;
  39. position: fixed;
  40. }
  41. /*给字体图标加个字号*/
  42. header > .icon {
  43. font-size: 26px;
  44. color: #fff;
  45. }
  46. body > .slider {
  47. height: 200px;
  48. margin-top: 42px;
  49. }
  50. /*轮播图宽度占据整个视宽*/
  51. .slider > img {
  52. width: 100%;
  53. }
  54. /*导航*/
  55. nav {
  56. height: 200px;
  57. background-color: #fff;
  58. display: flex;
  59. flex-flow: row wrap;
  60. /*分散对齐*/
  61. align-content: space-around;
  62. /*上下边距10px */
  63. margin-bottom: 10px;
  64. }
  65. nav > div {
  66. /*转成flex容器*/
  67. display: flex;
  68. /*给一个25vw的宽度*/
  69. flex: 0 1 25vw;
  70. flex-flow: column nowrap;
  71. align-items: center;
  72. }
  73. /*图片宽度改为原来大小的50%*/
  74. nav > div img {
  75. width: 50%;
  76. }
  77. /*图片居中对齐*/
  78. nav > div > a:first-of-type {
  79. text-align: center;
  80. }
  81. /*通用标题*/
  82. .title {
  83. margin-top: 20px;
  84. margin-bottom: 10px;
  85. color: #888;
  86. font-size: 18px;
  87. font-weight: bold;
  88. }
  89. /*两边内边距*/
  90. .box {
  91. padding: 0 10px;
  92. }
  93. /*推荐课程*/
  94. .recommend > .course_box {
  95. display: flex;
  96. /*分散对齐*/
  97. justify-content: space-around;
  98. }
  99. .course_box img {
  100. width: 47vw;
  101. }
  102. .course_inline {
  103. background-color: #fff;
  104. display: flex;
  105. padding: 10px;
  106. margin: 10px 0;
  107. }
  108. .course_inline img {
  109. width: 40vw;
  110. }
  111. .course_intro {
  112. display: flex;
  113. flex-flow: column nowrap;
  114. padding: 0 10px;
  115. }
  116. .course_intro > h2 {
  117. font: 16px Helvetica Neue, Helvetica, PingFang SC, 微软雅黑, Tahoma, Arial,
  118. sans-serif;
  119. font-weight: 400;
  120. color: #777;
  121. }
  122. .course_intro > p {
  123. margin-top: 10px;
  124. }
  125. /*标签*/
  126. .tag {
  127. background-color: #333;
  128. /*圆角*/
  129. border-radius: 8px;
  130. color: #fff;
  131. font-size: 12px;
  132. padding: 3px;
  133. }
  134. /*最近更新*/
  135. .update_list {
  136. background-color: #fff;
  137. display: flex;
  138. padding: 10px;
  139. margin: 20px 0;
  140. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  141. }
  142. .update_list img {
  143. width: 40vw;
  144. }
  145. .update_intro {
  146. display: flex;
  147. flex-flow: column nowrap;
  148. padding: 0 10px;
  149. justify-content: space-around;
  150. }
  151. .update_list > .update_intro > h2 {
  152. font-weight: 400;
  153. font-size: 17px;
  154. }
  155. .update_intro > p:first-of-type {
  156. font-size: 11px;
  157. }
  158. .update_intro > p:last-of-type {
  159. display: flex;
  160. justify-content: space-between;
  161. }
  162. /*最新文章*/
  163. article {
  164. display: flex;
  165. flex-flow: column nowrap;
  166. }
  167. article > .article_list {
  168. background-color: #fff;
  169. display: flex;
  170. padding: 10px;
  171. margin: 8px 0;
  172. justify-content: space-between;
  173. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  174. }
  175. article > .article_list > .article_left {
  176. display: flex;
  177. flex-flow: column nowrap;
  178. }
  179. .article_list > .article_left h2 {
  180. font-size: 14px;
  181. font-weight: bold;
  182. }
  183. .article_list > .article_left span {
  184. margin-top: 10px;
  185. }
  186. article > .article_list > .article_right {
  187. width: 30vw;
  188. }
  189. article > .article_list > .article_right img {
  190. width: 100%;
  191. }
  192. /*更多*/
  193. .more > a {
  194. display: block;
  195. background-color: #fff;
  196. margin: 5px 0;
  197. padding: 5px 0;
  198. text-align: center;
  199. }
  200. /*最新博文,最新问答*/
  201. .blog > .blog_list,
  202. .question > .question_list {
  203. background-color: #fff;
  204. margin: 12px 0;
  205. padding: 10px;
  206. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  207. }
  208. .blog > .blog_list > a,
  209. .question > .question_list > a {
  210. display: flex;
  211. justify-content: space-between;
  212. }
  213. .blog > .blog_list > a > h2,
  214. .question > .question_list > a > h2 {
  215. font-size: 14px;
  216. font-weight: bold;
  217. color: #777;
  218. }
  219. .blog > .blog_list > a > span,
  220. .question > .question_list > a > span {
  221. font-weight: 100;
  222. }
  223. .question .more {
  224. margin-bottom: 65px;
  225. }
  226. /*页脚*/
  227. footer {
  228. height: 55px;
  229. background-color: #efefef;
  230. width: 100vw;
  231. display: flex;
  232. justify-content: space-around;
  233. align-items: center;
  234. border-top: 1px solid #ccc;
  235. position: fixed;
  236. bottom: 0;
  237. }
  238. footer > a {
  239. display: flex;
  240. flex-flow: column nowrap;
  241. align-items: center;
  242. }
  243. /*选中*/
  244. .active {
  245. color: red;
  246. }

代码运行图如下:

2.Grid 网格布局

Grid 网格布局是一种将网页网格化,并实现布局的一种方式

  • 使用自定义 12 栅格布局实现百度首页
    html 代码如下:
  1. <body>
  2. <div class="container">
  3. <!-- 页眉 -->
  4. <div class="row">
  5. <div class="item col-12 header">
  6. <nav>
  7. <a href="">新闻</a>
  8. <a href="">hao123</a>
  9. <a href="">地图</a>
  10. <a href="">视频</a>
  11. <a href="">贴吧</a>
  12. <a href="">学术</a>
  13. <a href="">更多</a>
  14. </nav>
  15. <nav>
  16. <a href="">高考加油</a>
  17. <a href="">天气</a>
  18. <a href="">登录</a>
  19. </nav>
  20. </div>
  21. </div>
  22. <!-- 主体 -->
  23. <div class="row">
  24. <div class="item col-2 left"></div>
  25. <div class="item col-8 main">
  26. <div class="baidu_img">
  27. <a href=""><img src="static/image/baidu.png" alt="" /></a>
  28. </div>
  29. <div class="input"><input type="text" /><button>百度一下</button></div>
  30. <div class="box">
  31. <div class="menu">
  32. <a class="active" href="">我的关注</a>
  33. <a href="">推荐</a>
  34. <a href="">导航</a>
  35. </div>
  36. <div class="content">
  37. <div class="head_line">
  38. <p>我的导航</p>
  39. <div>
  40. <div class="cbox">
  41. <input type="checkbox" name="" id="show" /><label for="show"
  42. >分类显示</label
  43. >
  44. </div>
  45. <a href="">编辑</a>
  46. </div>
  47. </div>
  48. <div class="list">
  49. <div class="nav">
  50. <a href=""><img src="static/image/c_1.png" alt="" /></a>
  51. <a href="">百度贴吧</a>
  52. </div>
  53. <div class="nav">
  54. <a href=""><img src="static/image/c_2.png" alt="" /></a>
  55. <a href="">淘宝网</a>
  56. </div>
  57. <div class="nav">
  58. <a href=""><img src="static/image/c_3.png" alt="" /></a>
  59. <a href="">新浪微博</a>
  60. </div>
  61. <div class="nav">
  62. <a href=""><img src="static/image/c_4.png" alt="" /></a>
  63. <a href="">新闻</a>
  64. </div>
  65. <div class="nav">
  66. <span>+</span>
  67. <a href="">添加导航</a>
  68. </div>
  69. </div>
  70. </div>
  71. <div class="diy">
  72. <a href=""><span>+</span></a>
  73. <a href="">自定义</a>
  74. </div>
  75. </div>
  76. </div>
  77. <div class="item col-2 right"></div>
  78. </div>
  79. <!-- 页脚 -->
  80. <div class="row">
  81. <div class="item col-12 footer">
  82. <nav>
  83. <a href="">设为首页</a>
  84. <a href="">关于百度</a>
  85. <a href="">About</a>
  86. <a href="">Baidu</a>
  87. <a href="">百度营销</a>
  88. <a href="">使用百度前必读</a>
  89. <a href="">意见反馈</a>
  90. <a href="">帮助中心</a>
  91. </nav>
  92. </div>
  93. </div>
  94. </div>
  95. </body>

CSS 代码如下

  1. * {
  2. padding: 0;
  3. margin: 0;
  4. box-sizing: border-box;
  5. }
  6. a {
  7. text-decoration: none;
  8. color: #555;
  9. }
  10. body {
  11. display: flex;
  12. justify-content: center;
  13. align-items: center;
  14. /* 视口大小 */
  15. min-height: 100vh;
  16. min-width: 100vw;
  17. }
  18. .container {
  19. width: 1000px;
  20. display: grid;
  21. gap: 5px;
  22. }
  23. .container > .row {
  24. display: grid;
  25. grid-template-columns: repeat(12, 1fr);
  26. min-height: 50px;
  27. gap: 5px;
  28. grid-template-areas: "";
  29. }
  30. .container > .row > .item {
  31. padding: 10px;
  32. }
  33. .col-1 {
  34. grid-column: span 1;
  35. }
  36. .col-2 {
  37. grid-column: span 2;
  38. }
  39. .col-3 {
  40. grid-column: span 3;
  41. }
  42. .col-4 {
  43. grid-column: span 4;
  44. }
  45. .col-5 {
  46. grid-column: span 5;
  47. }
  48. .col-6 {
  49. grid-column: span 6;
  50. }
  51. .col-7 {
  52. grid-column: span 7;
  53. }
  54. .col-8 {
  55. grid-column: span 8;
  56. }
  57. .col-9 {
  58. grid-column: span 9;
  59. }
  60. .col-10 {
  61. grid-column: span 10;
  62. }
  63. .col-11 {
  64. grid-column: span 11;
  65. }
  66. .col-12 {
  67. grid-column: span 12;
  68. }
  69. /* 页眉 */
  70. .header {
  71. display: grid;
  72. grid-template-columns: 3fr 1fr;
  73. grid-template-rows: 1fr;
  74. align-items: center;
  75. }
  76. /* 导航 */
  77. nav {
  78. background-color: #fff;
  79. }
  80. nav > a {
  81. padding: 10px;
  82. }
  83. nav > a:hover {
  84. color: blue;
  85. }
  86. /* 主体 */
  87. .main {
  88. height: 600px;
  89. display: grid;
  90. grid-template-rows: 1fr 1fr 3fr;
  91. place-items: center;
  92. }
  93. /* LOGO */
  94. .baidu_img {
  95. display: grid;
  96. }
  97. .baidu_img img {
  98. width: 50%;
  99. }
  100. .baidu_img a {
  101. text-align: center;
  102. }
  103. /* 输入框 */
  104. .input > input {
  105. width: 300px;
  106. height: 44px;
  107. border: 2px solid #c4c7ce;
  108. border-radius: 5px;
  109. background-color: #fff;
  110. padding: 0 10px;
  111. }
  112. .input > button {
  113. background-color: #4e6ef2;
  114. border: 0;
  115. border-radius: 0 8px 8px 0;
  116. color: #fff;
  117. height: 44px;
  118. width: 108px;
  119. margin-left: -3px;
  120. }
  121. /* 主要内容区 */
  122. .box {
  123. width: 600px;
  124. height: 200px;
  125. display: grid;
  126. grid-template-rows: 1fr 6fr;
  127. }
  128. /* 内容区菜单 */
  129. .box > .menu {
  130. display: grid;
  131. grid-auto-flow: column;
  132. place-content: center start;
  133. }
  134. .menu > a {
  135. font-size: 14px;
  136. padding: 5px 10px;
  137. color: #777;
  138. }
  139. .menu > a:hover {
  140. color: #000;
  141. }
  142. /* 选中状态 */
  143. .active {
  144. border-bottom: 2px solid #4e6ef2;
  145. }
  146. /* 内容区主体 */
  147. .content {
  148. background-color: #fff;
  149. border-radius: 8px;
  150. box-shadow: 2px 3px 2px #ddd;
  151. }
  152. /* 主体头部 */
  153. .head_line {
  154. display: grid;
  155. grid-auto-flow: column;
  156. grid-template-columns: 5fr 2fr;
  157. padding: 10px 15px;
  158. }
  159. .head_line > p,
  160. .cbox label,
  161. .head_line > div > a {
  162. font-size: 14px;
  163. font-weight: 100;
  164. color: #777;
  165. }
  166. .head_line > div {
  167. display: grid;
  168. grid-auto-flow: column;
  169. justify-content: space-around;
  170. }
  171. /* 内容区内容 */
  172. .list {
  173. padding: 15px;
  174. display: grid;
  175. grid-auto-flow: column;
  176. grid-template-columns: repeat(5, 1fr);
  177. place-items: center;
  178. }
  179. .list > .nav {
  180. display: flex;
  181. flex-flow: column;
  182. align-items: center;
  183. }
  184. .nav img {
  185. width: 25%;
  186. }
  187. .nav a {
  188. font-size: 14px;
  189. text-align: center;
  190. }
  191. .nav a:hover {
  192. color: #4e6ef2;
  193. }
  194. /* 自定义 */
  195. .diy {
  196. display: grid;
  197. place-content: start;
  198. margin-top: 10px;
  199. }
  200. .diy {
  201. display: flex;
  202. justify-content: start;
  203. align-items: center;
  204. }
  205. .diy > a:hover {
  206. color: #4e6ef2;
  207. }
  208. .diy > a > span {
  209. padding: 5px;
  210. font-weight: bold;
  211. font-size: 16px;
  212. }
  213. /* 页脚 */
  214. .footer {
  215. display: grid;
  216. align-items: center;
  217. }
  218. .footer > nav a {
  219. font-size: 12px;
  220. color: #777;
  221. }
  222. .footer > nav a:hover {
  223. color: #4e6ef2;
  224. }

代码运行如图

  • Grid 布局实现响应式网络相册

html 代码如下

  1. <body>
  2. <h1>动物头像</h1>
  3. <div class="container">
  4. <div class="item">
  5. <a href=""><img src="static/image/1.jpg" alt="" /></a>
  6. <a href="">头像1</a>
  7. </div>
  8. <div class="item">
  9. <a href=""><img src="static/image/2.jpg" alt="" /></a>
  10. <a href="">头像2</a>
  11. </div>
  12. <div class="item">
  13. <a href=""><img src="static/image/3.jpg" alt="" /></a>
  14. <a href="">头像3</a>
  15. </div>
  16. <div class="item">
  17. <a href=""><img src="static/image/4.jpg" alt="" /></a>
  18. <a href="">头像4</a>
  19. </div>
  20. <div class="item">
  21. <a href=""><img src="static/image/5.jpg" alt="" /></a>
  22. <a href="">头像5</a>
  23. </div>
  24. <div class="item">
  25. <a href=""><img src="static/image/6.jpg" alt="" /></a>
  26. <a href="">头像6</a>
  27. </div>
  28. <div class="item">
  29. <a href=""><img src="static/image/7.jpg" alt="" /></a>
  30. <a href="">头像7</a>
  31. </div>
  32. <div class="item">
  33. <a href=""><img src="static/image/8.jpg" alt="" /></a>
  34. <a href="">头像8</a>
  35. </div>
  36. <div class="item">
  37. <a href=""><img src="static/image/9.jpg" alt="" /></a>
  38. <a href="">头像9</a>
  39. </div>
  40. <div class="item">
  41. <a href=""><img src="static/image/10.jpg" alt="" /></a>
  42. <a href="">头像10</a>
  43. </div>
  44. <div class="item">
  45. <a href=""><img src="static/image/11.jpg" alt="" /></a>
  46. <a href="">头像11</a>
  47. </div>
  48. </div>
  49. </body>

CSS 代码如下

  1. /* 初始化 */
  2. * {
  3. padding: 0;
  4. margin: 0;
  5. box-sizing: border-box;
  6. }
  7. a {
  8. text-decoration: none;
  9. color: #555;
  10. }
  11. body {
  12. background-color: lightseagreen;
  13. }
  14. /* 页头标题 */
  15. h1 {
  16. color: #fff;
  17. text-align: center;
  18. font-size: 2.5rem;
  19. font-weight: normal;
  20. text-shadow: 2px 2px 2px #555;
  21. margin-top: 20px;
  22. }
  23. /* 主体 */
  24. .container {
  25. width: 100vw;
  26. height: 100vh;
  27. padding: 50px;
  28. display: grid;
  29. /* 自动填充简介实现媒体查询 */
  30. grid-template-columns: repeat(auto-fill, 180px);
  31. grid-template-rows: repeat(auto-fill, 200px);
  32. /* 垂直,水平平均对齐 */
  33. place-content: space-evenly;
  34. /* 间隔 */
  35. gap: 10px;
  36. }
  37. .container img {
  38. width: 100%;
  39. /* 图片居中 */
  40. text-align: center;
  41. }
  42. .container > .item {
  43. /* 圆角 */
  44. border-radius: 10px;
  45. padding: 10px;
  46. background-color: #eee;
  47. /* 细节调整 */
  48. display: flex;
  49. flex-flow: column nowrap;
  50. align-items: center;
  51. justify-content: center;
  52. }
  53. /* 鼠标覆盖样式 */
  54. .container > .item:hover {
  55. box-shadow: 0 0 10px #666;
  56. width: calc(100% * 1.02);
  57. background-color: lightpink;
  58. }

代码运行如图

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
1 comments
涅槃重生 2020-08-14 11:46:06
向这位同学们学习
1 floor
Author's latest blog post