博客列表 >grid对齐方式以及简单实战

grid对齐方式以及简单实战

咸鱼老爷
咸鱼老爷原创
2021年02月27日 17:15:411717浏览

网格单元的对齐方式

只有项目在网格单元中存在剩余空间时,对齐才有意义;
1、设置所有项目的对齐方式
书写在容器中:place-items: 垂直方向 水平方向;
方位词start end center
垂直居上,水平居中:place-items: start center;
水平垂直居中:place-items: center;
normal->auto同义词
inherit:继承默认值
initial:初始化
unset:在继承和初始化之间切换
默认是拉伸:stretch 没有固定尺寸才有效果
2、设置容器中某一个项目在网格单元中的对齐方式
书写在项目中:place-self:垂直方向 水平方向;
同1中的用法一样

网格容器的对齐方式

书写在容器中place-content:垂直方向 水平方向;
1、将所有项目作为一个整体在容器中对齐
place-content:start start;默认
place-content: center;
2、将所有项目打散成独立个体在容器中设置对齐方式
place-content: space-between;俩端对齐
place-content: space-around ;分散对齐
place-content: space-evenly;平均对齐

使用命名网格区域实现圣杯布局

grid-area可以命名网格区域
效果图

  1. <style>
  2. body {
  3. display: grid;
  4. grid-template-columns: 15em minmax(50vh, auto) 15em;
  5. grid-template-rows: 3em minmax(80vh, auto) 3em;
  6. gap: .5em;
  7. grid-template-areas: 'header header header' 'left main right' 'footer footer foot';
  8. grid-template-areas: 'header header header' '. main .' 'footer footer foot';
  9. }
  10. body * {
  11. border: 1px solid #ccc;
  12. }
  13. header {
  14. grid-area: header;
  15. }
  16. footer {
  17. grid-area: footer;
  18. }
  19. /* aside.left {
  20. grid-area: left;
  21. }
  22. aside.right {
  23. grid-area: right;
  24. } */
  25. main {
  26. grid-area: main;
  27. }
  28. </style>
  29. <body>
  30. <header>header</header>
  31. <main>main</main>
  32. <aside class="left">left</aside>
  33. <aside class="right">right</aside>
  34. <footer>footer</footer>
  35. </body>

使用grid模拟栅格

效果图

  1. <style>
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. body {
  8. width: 100vw;
  9. height: 100vh;
  10. display: grid;
  11. }
  12. .row {
  13. display: grid;
  14. grid-template-columns: repeat(12, 1fr);
  15. gap: .2em;
  16. }
  17. .item {
  18. border: 1px solid #ccc;
  19. background-color: chocolate;
  20. }
  21. .col-12 {
  22. grid-area: auto / span 12;
  23. }
  24. .col-11 {
  25. grid-area: auto / span 11;
  26. }
  27. .col-10 {
  28. grid-area: auto / span 10;
  29. }
  30. .col-9 {
  31. grid-area: auto / span 9;
  32. }
  33. .col-8 {
  34. grid-area: auto / span 8;
  35. }
  36. .col-7 {
  37. grid-area: auto / span 7;
  38. }
  39. .col-6 {
  40. grid-area: auto / span 6;
  41. }
  42. .col-5 {
  43. grid-area: auto / span 5;
  44. }
  45. .col-4 {
  46. grid-area: auto / span 4;
  47. }
  48. .col-3 {
  49. grid-area: auto / span 3;
  50. }
  51. .col-2 {
  52. grid-area: auto / span 2;
  53. }
  54. .col-1 {
  55. grid-area: auto / span 1;
  56. }
  57. </style>
  58. <body>
  59. <div class="container">
  60. <div class="row">
  61. <div class=" item col-12">12列</div>
  62. <div class=" item col-6">6列</div>
  63. <div class=" item col-6">6列</div>
  64. <div class=" item col-5">5列</div>
  65. <div class=" item col-5">5列</div>
  66. <div class=" item col-4">4列</div>
  67. <div class=" item col-4">4列</div>
  68. <div class=" item col-4">4列</div>
  69. <div class=" item col-3">3列</div>
  70. <div class=" item col-3">3列</div>
  71. <div class=" item col-3">3列</div>
  72. <div class=" item col-3">3列</div>
  73. <div class=" item col-1">1列</div>
  74. <div class=" item col-1">1列</div>
  75. <div class=" item col-1">1列</div>
  76. <div class=" item col-1">1列</div>
  77. <div class=" item col-1">1列</div>
  78. <div class=" item col-1">1列</div>
  79. <div class=" item col-1">1列</div>
  80. <div class=" item col-1">1列</div>
  81. <div class=" item col-1">1列</div>
  82. <div class=" item col-1">1列</div>
  83. <div class=" item col-1">1列</div>
  84. <div class=" item col-1">1列</div>
  85. </div>
  86. </div>
  87. </body>

使用给grid布局实现php中文网首页简单案例

效果图

html代码

  1. <body>
  2. <header>
  3. <div class="logo">
  4. <a href=""><img src="https://www.php.cn/static/images/logo.png" alt=""></a>
  5. </div>
  6. <ul class="navs">
  7. <li><a href="">首页</a></li>
  8. <li><a href="">视频教程</a></li>
  9. <li><a href="">入门教程</a></li>
  10. <li><a href="">社区问答</a></li>
  11. <li><a href="">技术文章</a></li>
  12. <li><a href="">资源下载</a></li>
  13. <li><a href="">编程词典</a></li>
  14. <li><a href="">工具下载</a></li>
  15. </ul>
  16. <div class="reg">
  17. <a href="">注册</a>
  18. <a href="">登录</a>
  19. </div>
  20. </header>
  21. <div class="main-top">
  22. <ul class="menus">
  23. <li>
  24. <a href="">php开发</a>
  25. </li>
  26. <li>
  27. <a href="">前端开发</a>
  28. </li>
  29. <li>
  30. <a href="">服务端开发</a>
  31. </li>
  32. <li>
  33. <a href="">移动开发</a>
  34. </li>
  35. <li>
  36. <a href="">数据库</a>
  37. </li>
  38. <li>
  39. <a href="">服务器运维&下载</a>
  40. </li>
  41. <li>
  42. <a href="">在线工具箱</a>
  43. </li>
  44. <li>
  45. <a href="">常用类库</a>
  46. </li>
  47. </ul>
  48. <ul class="tuijian">
  49. <li>
  50. <a href="">php头条</a>
  51. </li>
  52. <li>
  53. <a href="">独孤九剑</a>
  54. </li>
  55. <li>
  56. <a href="">学习路线</a>
  57. </li>
  58. <li>
  59. <a href="">在线工具</a>
  60. </li>
  61. <li>
  62. <a href="">趣味课堂</a>
  63. </li>
  64. <li>
  65. <a href="">社区问答</a>
  66. </li>
  67. <li>
  68. <a href="">课程直播</a>
  69. </li>
  70. <li>
  71. <input type="text" placeholder="输入关键词搜索">
  72. </li>
  73. </ul>
  74. <div class="slider"><img src="https://img.php.cn/upload/article/000/000/001/5fabba9a8557c153.jpg" alt=""></div>
  75. <ul class="course">
  76. <li>
  77. <a href=""><img src="https://www.php.cn/static/images/index_yunv.jpg" alt=""></a>
  78. </li>
  79. <li>
  80. <a href=""><img src="https://www.php.cn/static/images/index_php_item2_.png?1" alt=""></a>
  81. </li>
  82. <li>
  83. <a href=""><img src="https://www.php.cn/static/images/index_php_item3.jpg?1" alt=""></a>
  84. </li>
  85. <li>
  86. <a href=""><img src="https://www.php.cn/static/images/index_php_new4.jpg?1" alt=""></a>
  87. </li>
  88. </ul>
  89. </div>
  90. <!-- 课程列表 -->
  91. <div class="main-coures">
  92. <h3>&lt;\&gt;PHP入门精品课程&lt;\&gt;</h3>
  93. <ul class="course-list">
  94. <li>
  95. <a href=""><img src="https://www.php.cn/static/images/index_learn_first.jpg" alt=""></a>
  96. </li>
  97. <li>
  98. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  99. <div class="desc">
  100. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  101. </div>
  102. <div class="desc-bottom">
  103. <span>1w次播放</span>
  104. </div>
  105. </a>
  106. </li>
  107. <li>
  108. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  109. <div class="desc">
  110. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  111. </div>
  112. <div class="desc-bottom">
  113. <span>1w次播放</span>
  114. </div>
  115. </a>
  116. </li>
  117. <li>
  118. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  119. <div class="desc">
  120. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  121. </div>
  122. <div class="desc-bottom">
  123. <span>1w次播放</span>
  124. </div>
  125. </a>
  126. </li>
  127. <li>
  128. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  129. <div class="desc">
  130. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  131. </div>
  132. <div class="desc-bottom">
  133. <span>1w次播放</span>
  134. </div>
  135. </a>
  136. </li>
  137. <li>
  138. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  139. <div class="desc">
  140. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  141. </div>
  142. <div class="desc-bottom">
  143. <span>1w次播放</span>
  144. </div>
  145. </a>
  146. </li>
  147. <li>
  148. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  149. <div class="desc">
  150. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  151. </div>
  152. <div class="desc-bottom">
  153. <span>1w次播放</span>
  154. </div>
  155. </a>
  156. </li>
  157. <li>
  158. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  159. <div class="desc">
  160. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  161. </div>
  162. <div class="desc-bottom">
  163. <span>1w次播放</span>
  164. </div>
  165. </a>
  166. </li>
  167. <li>
  168. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  169. <div class="desc">
  170. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  171. </div>
  172. <div class="desc-bottom">
  173. <span>1w次播放</span>
  174. </div>
  175. </a>
  176. </li>
  177. <li>
  178. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  179. <div class="desc">
  180. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  181. </div>
  182. <div class="desc-bottom">
  183. <span>1w次播放</span>
  184. </div>
  185. </a>
  186. </li>
  187. <li>
  188. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  189. <div class="desc">
  190. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  191. </div>
  192. <div class="desc-bottom">
  193. <span>1w次播放</span>
  194. </div>
  195. </a>
  196. </li>
  197. <li>
  198. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  199. <div class="desc">
  200. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  201. </div>
  202. <div class="desc-bottom">
  203. <span>1w次播放</span>
  204. </div>
  205. </a>
  206. </li>
  207. <li>
  208. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  209. <div class="desc">
  210. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  211. </div>
  212. <div class="desc-bottom">
  213. <span>1w次播放</span>
  214. </div>
  215. </a>
  216. </li>
  217. <li>
  218. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  219. <div class="desc">
  220. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  221. </div>
  222. <div class="desc-bottom">
  223. <span>1w次播放</span>
  224. </div>
  225. </a>
  226. </li>
  227. </ul>
  228. </div>
  229. <div class="main-coures">
  230. <h3>&lt;\&gt;PHP进阶课程&lt;\&gt;</h3>
  231. <ul class="course-list">
  232. <li>
  233. <a href=""><img src="https://www.php.cn/static/images/index_learn_middel.jpg" alt=""></a>
  234. </li>
  235. <li>
  236. <a href=""><img src="https://img.php.cn/upload/course/000/000/003/5a530c9c6c775990.jpg" alt="">
  237. <div class="desc">
  238. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  239. </div>
  240. <div class="desc-bottom">
  241. <span>1w次播放</span>
  242. </div>
  243. </a>
  244. </li>
  245. <li>
  246. <a href=""><img src="https://img.php.cn/upload/course/000/000/003/5a530c9c6c775990.jpg" alt="">
  247. <div class="desc">
  248. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  249. </div>
  250. <div class="desc-bottom">
  251. <span>1w次播放</span>
  252. </div>
  253. </a>
  254. </li>
  255. <li>
  256. <a href=""><img src="https://img.php.cn/upload/course/000/000/003/5a530c9c6c775990.jpg" alt="">
  257. <div class="desc">
  258. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  259. </div>
  260. <div class="desc-bottom">
  261. <span>1w次播放</span>
  262. </div>
  263. </a>
  264. </li>
  265. <li>
  266. <a href=""><img src="https://img.php.cn/upload/course/000/000/003/5a530c9c6c775990.jpg" alt="">
  267. <div class="desc">
  268. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  269. </div>
  270. <div class="desc-bottom">
  271. <span>1w次播放</span>
  272. </div>
  273. </a>
  274. </li>
  275. <li>
  276. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  277. <div class="desc">
  278. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  279. </div>
  280. <div class="desc-bottom">
  281. <span>1w次播放</span>
  282. </div>
  283. </a>
  284. </li>
  285. <li>
  286. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  287. <div class="desc">
  288. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  289. </div>
  290. <div class="desc-bottom">
  291. <span>1w次播放</span>
  292. </div>
  293. </a>
  294. </li>
  295. <li>
  296. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  297. <div class="desc">
  298. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  299. </div>
  300. <div class="desc-bottom">
  301. <span>1w次播放</span>
  302. </div>
  303. </a>
  304. </li>
  305. <li>
  306. <a href=""><img src="https://img.php.cn/upload/course/000/000/003/5a530c9c6c775990.jpg" alt="">
  307. <div class="desc">
  308. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  309. </div>
  310. <div class="desc-bottom">
  311. <span>1w次播放</span>
  312. </div>
  313. </a>
  314. </li>
  315. <li>
  316. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  317. <div class="desc">
  318. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  319. </div>
  320. <div class="desc-bottom">
  321. <span>1w次播放</span>
  322. </div>
  323. </a>
  324. </li>
  325. <li>
  326. <a href=""><img src="https://img.php.cn/upload/course/000/000/003/5a530c9c6c775990.jpg" alt="">
  327. <div class="desc">
  328. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  329. </div>
  330. <div class="desc-bottom">
  331. <span>1w次播放</span>
  332. </div>
  333. </a>
  334. </li>
  335. <li>
  336. <a href=""><img src="https://img.php.cn/upload/course/000/000/003/5a530c9c6c775990.jpg" alt="">
  337. <div class="desc">
  338. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  339. </div>
  340. <div class="desc-bottom">
  341. <span>1w次播放</span>
  342. </div>
  343. </a>
  344. </li>
  345. <li>
  346. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  347. <div class="desc">
  348. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  349. </div>
  350. <div class="desc-bottom">
  351. <span>1w次播放</span>
  352. </div>
  353. </a>
  354. </li>
  355. <li>
  356. <a href=""><img src="https://img.php.cn/upload/course/000/000/001/5b63c72c61569244.png" alt="">
  357. <div class="desc">
  358. <h4><i>初级</i>编程学习方法分享直播公益课</h4>
  359. </div>
  360. <div class="desc-bottom">
  361. <span>1w次播放</span>
  362. </div>
  363. </a>
  364. </li>
  365. </ul>
  366. </div>
  367. <footer></footer>
  368. </body>

CSS代码

  1. <style>
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. body {
  8. background-color: #f3f5f7;
  9. }
  10. li {
  11. list-style: none;
  12. }
  13. a {
  14. color: #444;
  15. text-decoration: none;
  16. }
  17. header {
  18. height: 60px;
  19. background-color: #000;
  20. margin-bottom: 30px;
  21. }
  22. footer {
  23. height: 8em;
  24. background-color: darkgray;
  25. }
  26. .main-top {
  27. height: 510px;
  28. width: 1200px;
  29. background-color: #fff;
  30. display: grid;
  31. grid-template-columns: 200px 1fr;
  32. grid-template-rows: 60px 1fr 120px;
  33. margin: auto;
  34. border-radius: .5em;
  35. }
  36. .main-top ul.menus {
  37. grid-area: span 3;
  38. display: grid;
  39. grid-template-rows: repeat(8, 1fr);
  40. background-color: #2b333b;
  41. border-top-left-radius: .5em;
  42. border-bottom-left-radius: .5em;
  43. place-content: center;
  44. padding: 2em;
  45. }
  46. .main-top ul.menus li a {
  47. color: rgba(255, 255, 255, .6);
  48. }
  49. .main-top .tuijian {
  50. display: grid;
  51. grid-template-columns: repeat(7, 100px) 1fr;
  52. place-items: center;
  53. }
  54. .main-top .tuijian li:last-of-type {
  55. place-self: center start;
  56. margin-left: 30px;
  57. }
  58. .main-top .tuijian li:last-of-type input {
  59. outline: none;
  60. background-color: #f1f0f0;
  61. height: 2.5em;
  62. border-radius: .3em;
  63. border: none;
  64. padding: 1em;
  65. }
  66. .main-top .slider {
  67. height: 330px;
  68. }
  69. .main-top .slider img {
  70. width: 100%;
  71. }
  72. .course li img {
  73. width: 100%;
  74. border-radius: .5em;
  75. }
  76. .course {
  77. display: grid;
  78. padding: 10px;
  79. grid-template-columns: repeat(4, 1fr);
  80. gap: 10px;
  81. place-content: center;
  82. margin-top: 10px;
  83. }
  84. header {
  85. overflow: hidden;
  86. }
  87. header .logo {
  88. height: 60px;
  89. float: left;
  90. }
  91. header .navs {
  92. float: left;
  93. }
  94. header .navs li {
  95. float: left;
  96. height: 60px;
  97. line-height: 60px;
  98. padding: 0 20px;
  99. margin: 0 10px;
  100. }
  101. header a {
  102. color: rgba(255, 255, 255, .7)
  103. }
  104. header .reg {
  105. float: right;
  106. height: 60px;
  107. line-height: 60px;
  108. }
  109. /* 课程列表 */
  110. .main-coures {
  111. width: 1200px;
  112. height: 646px;
  113. padding: 15px;
  114. background-color: #FFF;
  115. border-radius: 10px;
  116. margin: 30px auto;
  117. display: grid;
  118. grid-template-rows: 50px 1fr;
  119. }
  120. .main-coures h3 {
  121. text-align: center;
  122. color: #444;
  123. }
  124. .main-coures .course-list {
  125. display: grid;
  126. grid-template-columns: repeat(5, 1fr);
  127. grid-template-rows: repeat(3, 1fr);
  128. gap: 10px;
  129. }
  130. .main-coures .course-list img {
  131. width: 100%;
  132. }
  133. .main-coures .course-list li:first-of-type {
  134. grid-area: span 2;
  135. }
  136. .main-coures .course-list li {
  137. position: relative;
  138. border-radius: 10px;
  139. }
  140. .main-coures .course-list li img {
  141. border-radius: 10px;
  142. }
  143. .desc {
  144. background-color: #fff;
  145. height: 38px;
  146. width: 100%;
  147. position: absolute;
  148. bottom: 45px;
  149. left: 0;
  150. right: 0;
  151. border-top-right-radius: 10px;
  152. border-top-left-radius: 10px;
  153. transition: .5s;
  154. }
  155. .main-coures .course-list a:hover .desc {
  156. height: 100px;
  157. right: 0;
  158. left: 0;
  159. }
  160. .desc-bottom {
  161. position: absolute;
  162. left: 0;
  163. right: 0;
  164. background-color: #fff;
  165. padding: 10px 0;
  166. width: 100%;
  167. height: 42px;
  168. background-color: #fff;
  169. color: #93999f;
  170. font-size: 12px;
  171. line-height: 36px;
  172. border-bottom-left-radius: 10px;
  173. border-bottom-right-radius: 10px;
  174. }
  175. .desc i {
  176. padding: 2px;
  177. font-style: normal;
  178. font-size: 12px;
  179. color: #fff;
  180. line-height: 12px;
  181. border-radius: 5px;
  182. background-color: #93999f;
  183. margin-left: 5px;
  184. }
  185. </style>

grid实现媒体查询

根据自动填充,根据当前宽度自动决定放几行几列;
auto-fit auto-fill

  1. grid-template-columns: repeat(auto-fit,minmax(15em,1fr));
  2. grid-template-rows: repeat(auto-fit,minmax(13em,70%));
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议