Blogger Information
Blog 13
fans 1
comment 0
visits 9914
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
使用grid实现layui/bootstrap中的12列栅格布局组件
大宇
Original
1105 people have browsed it

grid.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <link rel="stylesheet" href="grid.css" />
  8. <title>layui/bootstrap中的12列栅格布局组件</title>
  9. </head>
  10. <body>
  11. <!-- 先写一行,在行中定义列 -->
  12. <!-- 一等份 -->
  13. <div class="container">
  14. <div class="row">
  15. <span class="item col-12">12列</span>
  16. </div>
  17. <!-- 二等分 -->
  18. <div class="row">
  19. <span class="item col-6">6列</span>
  20. <span class="item col-6">6列</span>
  21. </div>
  22. <!-- 三等份 -->
  23. <div class="row">
  24. <span class="item col-4">4列</span>
  25. <span class="item col-4">4列</span>
  26. <span class="item col-4">4列</span>
  27. </div>
  28. <!-- 二等分:2:10 -->
  29. <div class="row">
  30. <span class="item col-2">2列</span>
  31. <span class="item col-10">10列</span>
  32. </div>
  33. </div>
  34. </body>
  35. </html>

grid.css

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. body {
  7. width: 100vw;
  8. height: 100vh;
  9. display: grid;
  10. place-content: center;
  11. }
  12. .container {
  13. min-width: 80vw;
  14. display: grid;
  15. gap: 0.5em;
  16. }
  17. .container > .row {
  18. display: grid;
  19. /* 任何一行都是由12列组成 */
  20. grid-template-columns: repeat(12, 1fr);
  21. min-height: 3em;
  22. gap: 0.5em;
  23. }
  24. .container > .row > .item {
  25. padding: 1em;
  26. border: 1px solid;
  27. }
  28. .col-12 {
  29. grid-area: auto / span 12;
  30. }
  31. .col-11 {
  32. grid-area: auto / span 11;
  33. }
  34. .col-10 {
  35. grid-area: auto / span 10;
  36. }
  37. .col-9 {
  38. grid-area: auto / span 9;
  39. }
  40. .col-8 {
  41. grid-area: auto / span 8;
  42. }
  43. .col-7 {
  44. grid-area: auto / span 7;
  45. }
  46. .col-6 {
  47. grid-area: auto / span 6;
  48. }
  49. .col-5 {
  50. grid-area: auto / span 5;
  51. }
  52. .col-4 {
  53. grid-area: auto / span 4;
  54. }
  55. .col-3 {
  56. grid-area: auto / span 3;
  57. }
  58. .col-2 {
  59. grid-area: auto / span 2;
  60. }
  61. .col-1 {
  62. grid-area: auto / span 1;
  63. }

效果:

对于grid的应用
demo1.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. <!-- font-awesome -->
  7. <link
  8. rel="stylesheet"
  9. href="https://cdn.bootcss.com/font-awesome/5.11.2/css/all.css"
  10. />
  11. <link rel="stylesheet" href="style.css" />
  12. <title>Document</title>
  13. </head>
  14. <body>
  15. <!-- 个人卡片容器,居中卡片 -->
  16. <div class="wrapper">
  17. <!-- 个人卡片 -->
  18. <div class="profile-card">
  19. <!-- 卡片内容,导航,个人简介和工作经历 -->
  20. <div class="content">
  21. <!-- 导航 -->
  22. <nav id="nav-menu">
  23. <a class="active" href="#" data-rel="about-me-section">个人简介</a>
  24. <a href="#" data-rel="work-exp-section">工作经历</a>
  25. <!-- 当前选中菜单的指示条 -->
  26. <div class="indicator"></div>
  27. </nav>
  28. <!-- 关于我部分 -->
  29. <section class="about-me-section active-section">
  30. <!-- 名字和身份信息 -->
  31. <div class="personal-info">
  32. <div class="title">
  33. <h1>大宇</h1>
  34. <p>学习php的弟弟一个</p>
  35. </div>
  36. <!-- 关注按钮 -->
  37. <button class="follow-btn">
  38. <i class="fas fa-plus"></i>关注
  39. </button>
  40. </div>
  41. <!-- 个人简介的内容 -->
  42. <div class="about-me">
  43. <p>
  44. 我是xx,我是一名学习php的菜鸟一枚,毕业于xxxxxxxxxx学院,php小老弟一枚。目前在php中文网学习。擅长
  45. html css js php mysql等单词的拼写。
  46. </p>
  47. </div>
  48. <!-- 社交图标 -->
  49. <footer>
  50. <ul>
  51. <li>
  52. <a href="#"><i class="fab fa-weixin"></i></a>
  53. </li>
  54. <li>
  55. <a href="#"><i class="fab fa-weibo"></i></a>
  56. </li>
  57. <li>
  58. <a href="#"><i class="fab fa-github"></i></a>
  59. </li>
  60. <li>
  61. <a href="#"><i class="fab fa-linkedin"></i></a>
  62. </li>
  63. </ul>
  64. </footer>
  65. </section>
  66. <!-- 工作经历部分 -->
  67. <section class="work-exp-section">
  68. <!-- 工作经历容器 -->
  69. <div class="work-exps">
  70. <!-- 每条工作经历 -->
  71. <div class="work-exp-item">
  72. <!-- 职位 -->
  73. <div class="position">前端工程师</div>
  74. <!-- 分隔线 -->
  75. <div class="seperator"></div>
  76. <!-- 公司信息 -->
  77. <div class="work-info">
  78. <!-- 工作时间 -->
  79. <div class="time">
  80. 2018.07 ~ 2019.08 <i class="far fa-calendar"></i>
  81. </div>
  82. <!-- 公司名称 -->
  83. <div class="company">北京某某互联网金融公司</div>
  84. </div>
  85. </div>
  86. <div class="work-exp-item">
  87. <div class="position">软件工程师</div>
  88. <div class="seperator"></div>
  89. <div class="work-info">
  90. <div class="time">
  91. 2013.07 ~ 2016.07 <i class="far fa-calendar"></i>
  92. </div>
  93. <div class="company">北京某美国信息技术公司</div>
  94. </div>
  95. </div>
  96. <div class="work-exp-item">
  97. <div class="position">软件工程师</div>
  98. <div class="seperator"></div>
  99. <div class="work-info">
  100. <div class="time">
  101. 2010.07 ~ 2010.12 <i class="far fa-calendar"></i>
  102. </div>
  103. <div class="company">北京某大软件有限公司</div>
  104. </div>
  105. </div>
  106. </div>
  107. </section>
  108. </div>
  109. <!-- 头像 -->
  110. <aside class="profile-image"><img src="me.jpeg" alt="我" /></aside>
  111. </div>
  112. </div>
  113. </body>
  114. </html>

style.css

  1. @import url("https://fonts.googleapis.com/css?family=Raleway&display=swap");
  2. :root {
  3. /* 主蓝色,按钮,社交图标 */
  4. --primary-color: #71b3dd;
  5. /* 主蓝色,深,导航 */
  6. --primary-dark-color: #4489b5;
  7. --text-color-gray: #8b979f;
  8. --text-color-light-gray: #c1c7cb;
  9. --text-color-dark-gray: #5a6f7c;
  10. }
  11. * {
  12. margin: 0;
  13. padding: 0;
  14. font-size: 14px;
  15. box-sizing: border-box;
  16. font-family: "Raleway", "PingFang SC", "Microsoft Yahei", sans-serif;
  17. }
  18. .wrapper {
  19. /* 栅格布局,居中卡片 */
  20. display: grid;
  21. align-items: center;
  22. justify-content: center;
  23. height: 100vh;
  24. }
  25. .profile-card {
  26. display: grid;
  27. /* 12格栅格 */
  28. grid-template-columns: repeat(12, 1fr);
  29. /* 每个列之间的空隙是12px */
  30. column-gap: 12px;
  31. /* 卡片总宽度为627px */
  32. width: 627px;
  33. /* 卡片总高度为374px */
  34. height: 374px;
  35. box-shadow: 0px 0px 22px 3px rgba(0, 0, 0, 0.18);
  36. }
  37. .profile-image {
  38. /* 图片占5列 */
  39. grid-column: 8 / 13;
  40. max-width: 238px;
  41. height: 100%;
  42. /* 隐藏图片超出区域 */
  43. overflow: hidden;
  44. /* 垂直为靠上对齐 */
  45. align-self: start;
  46. /* 水平为靠右对齐 */
  47. justify-self: end;
  48. }
  49. .profile-image img {
  50. /* 图片放大,然后只显示一部分 */
  51. width: 210%;
  52. transform: translate(-205px, -280px);
  53. object-fit: cover;
  54. }
  55. .content {
  56. /* 左边内容占7列 */
  57. grid-column: 1 / 8;
  58. padding: 38px 28px 20px 33px;
  59. position: relative;
  60. }
  61. nav {
  62. margin-bottom: 24px;
  63. display: flex;
  64. position: relative;
  65. }
  66. nav a {
  67. color: var(--text-color-gray);
  68. text-decoration: none;
  69. }
  70. nav a.active {
  71. color: var(--primary-dark-color);
  72. }
  73. nav a:not(:last-child) {
  74. margin-right: 40px;
  75. }
  76. nav .indicator {
  77. height: 2px;
  78. background: var(--primary-dark-color);
  79. bottom: -7px;
  80. left: 0;
  81. position: absolute;
  82. transition: 0.4s;
  83. }
  84. .content section {
  85. /* 堆叠个人简介和工作经历部分 */
  86. position: absolute;
  87. /* 默认都不显示 */
  88. opacity: 0;
  89. /* 淡出淡入效果 */
  90. transition: 0.3s ease-out;
  91. }
  92. .content section.active-section {
  93. /* 显示active-section */
  94. opacity: 1;
  95. }
  96. /* 名字、身份与关注按钮左右布局,各占一列,子栅格不受父栅格影响,是个全新的栅格布局 */
  97. .personal-info {
  98. display: grid;
  99. grid-template-columns: 1fr 1fr;
  100. }
  101. /* 名字 */
  102. .title h1 {
  103. font-size: 2em;
  104. font-weight: 500;
  105. }
  106. /* 身份 */
  107. .title p {
  108. color: var(--text-color-gray);
  109. font-size: 1em;
  110. margin: 6px 0 18px 0;
  111. }
  112. /* 关注按钮 */
  113. .follow-btn {
  114. justify-self: end;
  115. height: 30px;
  116. background: var(--primary-color);
  117. border: none;
  118. color: white;
  119. padding: 0 27px;
  120. border-radius: 4px;
  121. display: flex;
  122. align-items: center;
  123. justify-content: center;
  124. line-height: 30px;
  125. }
  126. /* 关注按钮的加号 */
  127. .follow-btn .fas {
  128. font-size: 10px;
  129. margin-right: 6px;
  130. }
  131. /* 关于我部分 */
  132. .about-me {
  133. color: var(--text-color-dark-gray);
  134. font-weight: 300;
  135. text-align: justify;
  136. }
  137. /* 社交按钮部分 */
  138. footer {
  139. margin-top: 70px;
  140. }
  141. footer ul {
  142. display: flex;
  143. }
  144. footer ul li {
  145. list-style: none;
  146. }
  147. footer ul li:not(:last-child) {
  148. margin-right: 30px;
  149. }
  150. footer .fab {
  151. color: var(--primary-color);
  152. font-size: 24px;
  153. }
  154. /* ********************************* */
  155. /* 工作经历,一共3条工作经历,所以创建了3行栅格布局 */
  156. .work-exps {
  157. color: var(--text-color-gray);
  158. display: grid;
  159. grid-template-rows: repeat(3, minmax(80px, auto));
  160. }
  161. /* 每个工作经历为三列栅格布局,分别为职位,分隔线、公司名和工作时间部分 */
  162. .work-exp-item {
  163. display: grid;
  164. /* 列之间的比例 */
  165. grid-template-columns: 5fr 1fr 7fr;
  166. align-items: center;
  167. justify-content: center;
  168. }
  169. /* 职位 */
  170. .position {
  171. font-size: 18px;
  172. font-weight: 500;
  173. }
  174. /* 分隔线 */
  175. .seperator {
  176. height: 43px;
  177. /* width: 1px; */
  178. border-left: 2px dotted #eaeff2;
  179. }
  180. /* 工作时间 */
  181. .time {
  182. color: var(--text-color-light-gray);
  183. }
  184. /* 公司 */
  185. .company {
  186. font-size: 14px;
  187. color: var(--text-color-dark-gray);
  188. margin-top: 9px;
  189. }

效果:

这个是看了b站一个视频跟着写的。
这是视频链接:https://www.bilibili.com/video/BV1m741167V5?from=search&seid=1080097720873864678

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