比定位更方便的叠加布局

image.png
通过设置grid-area,使各子项在同一个网格,以达到叠加效果

  1. .box {
  2. display: grid;
  3. width: 500px;
  4. height: 300px;
  5. }
  6. img {
  7. grid-area: 1/1/1/1;
  8. }
  9. span {
  10. grid-area: 1/1/1/1;
  11. justify-self: end;
  12. align-self: start;
  13. background: greenyellow;
  14. margin-top: 20px;
  15. }
  16. p {
  17. margin: 0;
  18. padding: 0 20px 0 0;
  19. align-self: end;
  20. grid-area: 1/1/1/1;
  21. background: rgba(0,0,0,.5);
  22. color: white;
  23. }

多重组合布局

image.png
设置最大子项的grid-area, 其他子项自动重排

  1. .box {
  2. background: skyblue;
  3. width: 300px;
  4. height: 300px;
  5. display: grid;
  6. grid-template-columns: repeat(auto-fill, 100px);
  7. grid-template-rows: repeat(auto-fill, 100px);
  8. }
  9. div:nth-of-type(1) {
  10. grid-area: 2/1/span 2/span 2;
  11. }
  12. .box > div {
  13. background: pink;
  14. border: 1px solid;
  15. }

栅格布局

将一行平均分成12份
grid-area设置auto使子项自适应排列
image.png

容器自适应行列布局

image.png

百度热词风云榜

image.png

  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. <title>百度热榜</title>
  8. <link rel="stylesheet" href="../reset.css">
  9. <style>
  10. * {
  11. margin:0;
  12. padding: 0;
  13. box-sizing: border-box;
  14. }
  15. .top {
  16. width: 310px;
  17. border: 1px #dadadc solid;
  18. margin: 0 auto;
  19. font-size: 14px;
  20. }
  21. .title {
  22. height: 30px;
  23. line-height: 30px;
  24. font-weight: bold;
  25. margin-left: 16px;
  26. }
  27. .list {
  28. height: 350px;
  29. margin: 0 14px;
  30. display: grid;
  31. grid-template-columns: repeat(3, 1fr);
  32. grid-template-rows: repeat(4, 1fr);
  33. grid-template-areas:
  34. "a1 a2 a2"
  35. "a3 a2 a2"
  36. "a4 a4 a5"
  37. "a6 a7 a7";
  38. gap: 8px 8px;
  39. }
  40. .list div:nth-of-type(1) {
  41. grid-area: a1;
  42. }
  43. .list div:nth-of-type(2) {
  44. grid-area: a2;
  45. }
  46. .list div:nth-of-type(3) {
  47. grid-area: a3;
  48. }
  49. .list div:nth-of-type(4) {
  50. grid-area: a4;
  51. }
  52. .list div:nth-of-type(5) {
  53. grid-area: a5;
  54. }
  55. .list div:nth-of-type(6) {
  56. grid-area: a6;
  57. }
  58. .list div:nth-of-type(7) {
  59. grid-area: a7;
  60. }
  61. .theme1 {
  62. background-image: linear-gradient(#187fe6, #32aff2);
  63. border: 2px solid #299bda;
  64. }
  65. .theme2 {
  66. background-image: linear-gradient(#f2246c, #fe5bac);
  67. border: 2px solid #dc1f61;
  68. }
  69. .theme3 {
  70. background-image: linear-gradient(#d46300, #e5ad1c);
  71. border: 2px solid #d39818;
  72. }
  73. .page {
  74. height: 40px;
  75. padding-right: 10px;
  76. display: flex;
  77. justify-content: flex-end;
  78. align-items: center;
  79. }
  80. .page span {
  81. padding: 4px;
  82. }
  83. .page a {
  84. text-decoration: none;
  85. display: block;
  86. border: 1px solid #cccccc;
  87. padding: 5px 4px;
  88. margin-left: 2px;
  89. }
  90. .list a {
  91. text-decoration: none;
  92. display: block;
  93. color: white;
  94. width: 100%;
  95. height: 100%;
  96. line-height: 30px;
  97. }
  98. .list a > h3 {
  99. margin-right: 4px;
  100. text-align: right;
  101. }
  102. .list a > p {
  103. text-align: center;
  104. }
  105. </style>
  106. </head>
  107. <body>
  108. <div class="top">
  109. <div class="title">
  110. 今日热榜
  111. </div>
  112. <div class="list">
  113. <div class="theme1">
  114. <a href="#">
  115. <h3>实时热点</h3>
  116. <p>阿里第一颗芯</p>
  117. </a>
  118. </div>
  119. <div class="theme1">
  120. <a href="#">
  121. <h3>实时热点</h3>
  122. <p>阿里第一颗芯</p>
  123. </a>
  124. </div>
  125. <div class="theme2">
  126. <a href="#">
  127. <h3>实时热点</h3>
  128. <p>阿里第一颗芯</p>
  129. </a>
  130. </div>
  131. <div class="theme1">
  132. <a href="#">
  133. <h3>实时热点</h3>
  134. <p>阿里第一颗芯</p>
  135. </a>
  136. </div>
  137. <div class="theme1">
  138. <a href="#">
  139. <h3>实时热点</h3>
  140. <p>阿里第一颗芯</p>
  141. </a>
  142. </div>
  143. <div class="theme3">
  144. <a href="#">
  145. <h3>实时热点</h3>
  146. <p>阿里第一颗芯</p>
  147. </a>
  148. </div>
  149. <div class="theme3">
  150. <a href="#">
  151. <h3>实时热点</h3>
  152. <p>阿里第一颗芯</p>
  153. </a>
  154. </div>
  155. </div>
  156. <div class="page">
  157. <span>1</span>
  158. <a href="#">2</a>
  159. <a href="#">3</a>
  160. </div>
  161. </div>
  162. </body>
  163. </html>

小米商品导航菜单

右边的弹出面板使用绝对定位

  1. .panel {
  2. display: none;
  3. position: absolute;
  4. height: 460px;
  5. background: white;
  6. left: 100%;
  7. top: 0;
  8. border: 1px solid #e0e0e0;
  9. padding: 0 26px;
  10. color: black;
  11. grid-template-columns: 305px;
  12. grid-template-rows: repeat(6, 1fr);
  13. grid-auto-flow: column;
  14. grid-auto-columns: 305px;
  15. justify-items: start;
  16. }

image.png

  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. <title>小米导航</title>
  8. <link
  9. rel="stylesheet"
  10. href="//at.alicdn.com/t/font_2941503_ny0b8aku22.css"
  11. />
  12. <style>
  13. * {
  14. margin: 0;
  15. padding: 0;
  16. box-sizing: border-box;
  17. }
  18. a {
  19. text-decoration: none;
  20. color: inherit;
  21. }
  22. ul {
  23. list-style: none;
  24. }
  25. .mall {
  26. height: 100vh;
  27. background: #f5f5f5;
  28. }
  29. .sidebar {
  30. width: 233px;
  31. background: #7b7b7b;
  32. height: 100%;
  33. color: white;
  34. padding-top: 20px;
  35. }
  36. .sidebar > li {
  37. display: flex;
  38. justify-content: space-between;
  39. align-items: center;
  40. padding: 16px 28px;
  41. position: relative;
  42. font-size: 14px;
  43. }
  44. .panel {
  45. display: none;
  46. position: absolute;
  47. height: 460px;
  48. background: white;
  49. left: 100%;
  50. top: 0;
  51. border: 1px solid #e0e0e0;
  52. padding: 0 26px;
  53. color: black;
  54. grid-template-columns: 305px;
  55. grid-template-rows: repeat(6, 1fr);
  56. grid-auto-flow: column;
  57. grid-auto-columns: 305px;
  58. justify-items: start;
  59. }
  60. .panel > li {
  61. display: flex;
  62. justify-content: center;
  63. align-items: center;
  64. cursor: auto;
  65. }
  66. .panel span {
  67. margin-left: 20px;
  68. }
  69. .sidebar > li:hover .panel {
  70. display: grid;
  71. }
  72. .sidebar > li:hover {
  73. background: #ff6801;
  74. cursor: pointer;
  75. }
  76. </style>
  77. </head>
  78. <body>
  79. <div class="mall">
  80. <ul class="sidebar">
  81. <li>
  82. <div class="items">
  83. <a href="#">手机</a>
  84. <a href="#">电话卡</a>
  85. </div>
  86. <i class="iconfont icon-youjiantou1"></i>
  87. <ul class="panel">
  88. <li>
  89. <img src="./phone.png" alt="" />
  90. <span>小米MIX FOLD</span>
  91. </li>
  92. <li>
  93. <img src="./phone.png" alt="" />
  94. <span>小米MIX FOLD</span>
  95. </li>
  96. <li>
  97. <img src="./phone.png" alt="" />
  98. <span>小米MIX FOLD</span>
  99. </li>
  100. <li>
  101. <img src="./phone.png" alt="" />
  102. <span>小米MIX FOLD</span>
  103. </li>
  104. <li>
  105. <img src="./phone.png" alt="" />
  106. <span>小米MIX FOLD</span>
  107. </li>
  108. <li>
  109. <img src="./phone.png" alt="" />
  110. <span>小米MIX FOLD</span>
  111. </li>
  112. <li>
  113. <img src="./phone.png" alt="" />
  114. <span>小米MIX FOLD</span>
  115. </li>
  116. <li>
  117. <img src="./phone.png" alt="" />
  118. <span>小米MIX FOLD</span>
  119. </li>
  120. </ul>
  121. </li>
  122. <li>
  123. <div class="items">
  124. <a href="#">手机</a>
  125. <a href="#">电话卡</a>
  126. </div>
  127. <i class="iconfont icon-youjiantou1"></i>
  128. <ul class="panel">
  129. <li>
  130. <img src="./phone.png" alt="" />
  131. <span>小米MIX FOLD</span>
  132. </li>
  133. <li>
  134. <img src="./phone.png" alt="" />
  135. <span>小米MIX FOLD</span>
  136. </li>
  137. <li>
  138. <img src="./phone.png" alt="" />
  139. <span>小米MIX FOLD</span>
  140. </li>
  141. <li>
  142. <img src="./phone.png" alt="" />
  143. <span>小米MIX FOLD</span>
  144. </li>
  145. <li>
  146. <img src="./phone.png" alt="" />
  147. <span>小米MIX FOLD</span>
  148. </li>
  149. <li>
  150. <img src="./phone.png" alt="" />
  151. <span>小米MIX FOLD</span>
  152. </li>
  153. <li>
  154. <img src="./phone.png" alt="" />
  155. <span>小米MIX FOLD</span>
  156. </li>
  157. <li>
  158. <img src="./phone.png" alt="" />
  159. <span>小米MIX FOLD</span>
  160. </li>
  161. <li>
  162. <img src="./phone.png" alt="" />
  163. <span>小米MIX FOLD</span>
  164. </li>
  165. <li>
  166. <img src="./phone.png" alt="" />
  167. <span>小米MIX FOLD</span>
  168. </li>
  169. <li>
  170. <img src="./phone.png" alt="" />
  171. <span>小米MIX FOLD</span>
  172. </li>
  173. <li>
  174. <img src="./phone.png" alt="" />
  175. <span>小米MIX FOLD</span>
  176. </li>
  177. <li>
  178. <img src="./phone.png" alt="" />
  179. <span>小米MIX FOLD</span>
  180. </li>
  181. </ul>
  182. </li>
  183. <li>
  184. <div class="items">
  185. <a href="#">手机</a>
  186. <a href="#">电话卡</a>
  187. </div>
  188. <i class="iconfont icon-youjiantou1"></i>
  189. </li>
  190. <li>
  191. <div class="items">
  192. <a href="#">手机</a>
  193. <a href="#">电话卡</a>
  194. </div>
  195. <i class="iconfont icon-youjiantou1"></i>
  196. </li>
  197. <li>
  198. <div class="items">
  199. <a href="#">手机</a>
  200. <a href="#">电话卡</a>
  201. </div>
  202. <i class="iconfont icon-youjiantou1"></i>
  203. </li>
  204. <li>
  205. <div class="items">
  206. <a href="#">手机</a>
  207. <a href="#">电话卡</a>
  208. </div>
  209. <i class="iconfont icon-youjiantou1"></i>
  210. </li>
  211. <li>
  212. <div class="items">
  213. <a href="#">手机</a>
  214. <a href="#">电话卡</a>
  215. </div>
  216. <i class="iconfont icon-youjiantou1"></i>
  217. </li>
  218. </ul>
  219. </div>
  220. </body>
  221. </html>

练习题

image.png

  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. <title>练习题</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. }
  12. .main {
  13. display: grid;
  14. grid-template-columns: 100px 1fr 100px;
  15. grid-template-rows: 100px 300px 100px;
  16. gap: 10px 10px;
  17. background: skyblue;
  18. }
  19. .main > div {
  20. background: pink;
  21. display: flex;
  22. align-items: center;
  23. justify-content: center;
  24. }
  25. div:nth-of-type(1) {
  26. grid-area: 1/1/2/4;
  27. }
  28. div:nth-of-type(5) {
  29. grid-area: 3/1/4/4;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div class="main">
  35. <div>header</div>
  36. <div>left</div>
  37. <div>center</div>
  38. <div>right</div>
  39. <div>footer</div>
  40. </div>
  41. </body>
  42. </html>