博客列表 >Grid项目在网格单元和容器中对齐的相关属性

Grid项目在网格单元和容器中对齐的相关属性

xosing的博客
xosing的博客原创
2020年12月28日 12:33:29815浏览

1. 将项目在网格单元和容器中对齐的相关属性全部实例演示;

项目在网格容器中的对齐方式

  1. 默认: 项目在网格单元中是拉伸的
  2. 只有项目在网络单元中存在剩余空间的时候,对齐才有必要且有意义

HTML代码

  1. <div class="container">
  2. <div class="item">item1</div>
  3. <div class="item">item2</div>
  4. <div class="item">item3</div>
  5. <div class="item">item4</div>
  6. <div class="item">item5</div>
  7. <div class="item">item6</div>
  8. <div class="item">item7</div>
  9. <div class="item">item8</div>
  10. <div class="item">item9</div>
  11. </div>

CSS代码

  1. .container {
  2. width: 30em;
  3. height: 15em;
  4. background-color: bisque;
  5. padding: 0.5em;
  6. display: grid;
  7. grid-template-columns: repeat(3, 5em);
  8. grid-template-rows: repeat(2, 3em);
  9. grid-auto-rows: 3em;
  10. gap: 0.5em;
  11. }
  12. .container>.item {
  13. background-color: lightgoldenrodyellow;
  14. color: lightblue;
  15. padding: 0.5em;
  16. }

1.1 place-content:容器中使用,排列参数: 垂直 水平; 默认值 place-content: start start;

1.1.1.将所有项目做为一个整体在容器排列; start; center; end;

项目在容器中排列: 垂直居中 水平靠左
  1. .container {
  2. place-content: center start;
  3. }

项目在容器中排列: 垂直居中 水平居中
  1. .container {
  2. place-content: center center;
  3. }

项目在容器中排列: 垂直居中 水平靠右
  1. .container {
  2. place-content: center end;
  3. }

1.1.2.将所有项目打散成独立个体在容器中排列

二端对齐space-between; 分散对齐space-around; 平均对齐space-evenly;

项目在容器中排列: 二端对齐
  1. .container {
  2. place-content: space-between;
  3. }

项目在容器中排列: 分散对齐
  1. .container {
  2. place-content: space-around;
  3. }

项目在容器中排列: 平均对齐
  1. .container {
  2. place-content: space-evenly;
  3. }


1.2 place-items:容器中使用,排列参数: 垂直 水平; 默认值 place-items: start start;

place-items: 所有项目在网格单元中的对齐方式

所有项目在单元格中排列: 垂直靠底 水平靠右
  1. .container {
  2. place-items: end end;
  3. }

所有项目在单元格中排列: 垂直居中 水平居中
  1. .container {
  2. place-items: center center;
  3. }


1.3. plce-self:项目中使用,排列参数: 垂直 水平; 默认值 place-self: start start;

place-self: 某一个项目在网络单元中的对齐方式

第五个项目在单元格中排列: 垂直居中 水平居中
  1. .container>.item:nth-last-of-type(5) {
  2. background-color: lightgreen;
  3. color: white;
  4. place-self: end start;
  5. }


2. 仿php.cn首页样式

html代码

  1. <!DOCTYPE html>
  2. <html lang="Zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>简易仿制php.cn首页</title>
  7. <link rel="stylesheet" href="style/1227.css">
  8. </head>
  9. <body>
  10. <header>
  11. <div class="logo">
  12. <a href="">
  13. <h1><img src="images/logo.png" title="php中文网"></h1>
  14. </a>
  15. </div>
  16. <div class="menu">
  17. <li><a href="">首页</a></li>
  18. <li><a href="">视频教程</a></li>
  19. <li><a href="">入门教程</a></li>
  20. <li><a href="">社区问答</a></li>
  21. <li><a href="">技术文章</a>
  22. <span class="iconfont icon-arrow-down-filling"></span>
  23. </li>
  24. <li><a href="">编程词典</a>
  25. <span class="iconfont icon-arrow-down-filling"></span>
  26. </li>
  27. <li><a href="">资源下载</a>
  28. <span class="iconfont icon-arrow-down-filling"></span>
  29. </li>
  30. <li><a href="">工具下载</a>
  31. <span class="iconfont icon-direction-down-circle"></span>
  32. </li>
  33. <li><a href="">PHP培训</a>
  34. <span class="iconfont icon-play"></span>
  35. </li>
  36. </div>
  37. <div class="login">
  38. <a href="">登录</a>
  39. <a href="">注册</a>
  40. </div>
  41. </header>
  42. <main>
  43. <div class="top">
  44. <div class="top-left">
  45. <li><a href="">php开发</a><span class="iconfont icon-arrow-right"></span></li>
  46. <li><a href="">前端开发</a><span class="iconfont icon-arrow-right"></span></li>
  47. <li><a href="">服务端开发</a><span class="iconfont icon-arrow-right"></span></li>
  48. <li><a href="">移动开发</a><span class="iconfont icon-arrow-right"></span></li>
  49. <li><a href="">数据库</a><span class="iconfont icon-arrow-right"></span></li>
  50. <li><a href="">服务器运维&下载</a><span class="iconfont icon-arrow-right"></span></li>
  51. <li><a href="">在线工具</a><span class="iconfont icon-arrow-right"></span></li>
  52. <li><a href="">常用类库</a><span class="iconfont icon-arrow-right"></span></li>
  53. </div>
  54. <div class="top-right">
  55. <div class="top-nav">
  56. <li><a href="">php头条</a> <span class="orangered"></span></li>
  57. <li><a href="">独孤九贱</a></li>
  58. <li><a href="">学习路线</a> <span class="darkslategray"></span></li>
  59. <li><a href="">在线工具</a></li>
  60. <li><a href="">趣味课堂</a> <span class="orange"></span></li>
  61. <li><a href="">社区问答</a></li>
  62. <li><a href="">课程直播</a></li>
  63. <div class="top-search">
  64. <input type="text" name="search" placeholder="输入关键字">
  65. <span class="iconfont icon-search"></span>
  66. </div>
  67. </div>
  68. <div class="top-slider"><img src="images/slider.jpg" alt=""></div>
  69. <div class="top-img">
  70. <li>
  71. <a href=""><img src="images/slider1.jpg" alt=""></a>
  72. </li>
  73. <li>
  74. <a href=""><img src="images/slider2.jpg" alt=""></a>
  75. </li>
  76. <li>
  77. <a href=""><img src="images/slider3.png" alt=""></a>
  78. </li>
  79. <li>
  80. <a href=""><img src="images/slider4.jpg" alt=""></a>
  81. </li>
  82. </div>
  83. </div>
  84. </div>
  85. <div class="ad">
  86. <a href=""><img src="images/slider5.jpg" alt=""></a>
  87. </div>
  88. <div class="main-two">
  89. <div class="toutao">
  90. <div class="toutao-title">
  91. <h2>头条</h2>
  92. </div>
  93. <li><a href="">php中文网原创视频:《天龙八部》公益ph</a></li>
  94. <li><a href="">php中文网《玉女心经》公益PHP WEB培</a></li>
  95. <li><a href="">十款炫酷的程序员圣诞节代码特效【免费下</a></li>
  96. <li><a href="">值得研究的20个Vue开源项目</a></li>
  97. <li><a href="">【推荐】17个提升用户体验图像特效库</a></li>
  98. <li><a href="">各种知名网站的404页面,看看谁更有创意?</a></li>
  99. <li><a href="">Chrome性能有了最大提升!(Chrome87</a></li>
  100. <li><a href="">PHP 8 正式发布了!</a></li>
  101. <li><a href="">推荐10款GitHub中高价值的PHP项目(值</a></li>
  102. <li><a href="">推荐2020前端开发者11个必备的网站</a></li>
  103. <li><a href="">这15个有用的Github功能,你知道吗!</a></li>
  104. <li><a href="">八个写JavaScript代码的小技巧!</a></li>
  105. </div>
  106. <div class="course">
  107. <div class="course-title">
  108. <h2>最新课程</h2>
  109. </div>
  110. <li>
  111. <a href=""><img src="images/course1.png" alt=""></a>
  112. <a href="">
  113. <p><span>高级</span>通用后台管理系统实战开发</p>
  114. </a>
  115. </li>
  116. <li>
  117. <a href=""><img src="images/course2.png" alt=""></a>
  118. <a href="">
  119. <p><span>中级</span>通用后台管理系统实战开发</p>
  120. </a>
  121. </li>
  122. <li>
  123. <a href=""><img src="images/course3.png" alt=""></a>
  124. <a href="">
  125. <p><span>高级</span>通用后台管理系统实战开发</p>
  126. </a>
  127. </li>
  128. <li>
  129. <a href=""><img src="images/course4.png" alt=""></a>
  130. <a href="">
  131. <p><span>初级</span>通用后台管理系统实战开发</p>
  132. </a>
  133. </li>
  134. <li>
  135. <a href=""><img src="images/course5.png" alt=""></a>
  136. <a href="">
  137. <p><span>中级</span>通用后台管理系统实战开发</p>
  138. </a>
  139. </li>
  140. <li>
  141. <a href=""><img src="images/course6.jpg" alt=""></a>
  142. <a href="">
  143. <p><span>高级</span>通用后台管理系统实战开发</p>
  144. </a>
  145. </li>
  146. </div>
  147. <div class="notebook">
  148. <div class="notebook-title">
  149. <h2>常用手册</h2><a href="">更多</a>
  150. </div>
  151. <li><img src="images/notebook1.png"><a href="">ThinkPHP6.0CI手册大全</a><a href="">php手册Linux手册</a></li>
  152. <li><img src="images/notebook2.png"><a href="">MySQL参考手册大全</a><a href="">laravel5.8速查表</a></li>
  153. <li><img src="images/notebook3.png"><a href="">Python参考手册大全</a><a href="">Node.js中文学习手册</a></li>
  154. <li><img src="images/notebook4.png"><a href="">html手册CSS手册</a><a href="">phpjQuery手册大全</a></li>
  155. <li><img src="images/notebook5.png"><a href="">ThinkPHP6.0CI手册大全</a><a href="">AngularJSAjax手册</a></li>
  156. <li><img src="images/notebook6.jpg"><a href="">ASP参考手册大全</a><a href="">Bootstrap3参考手册</a></li>
  157. </div>
  158. </main>
  159. <footer>
  160. <div class="footer-main">
  161. <div class="footer-content">
  162. <ul>
  163. <li> <a href=""> 网站首页</a></li>
  164. <li> <a href=""> PHP视频</a></li>
  165. <li> <a href=""> PHP实战</a></li>
  166. <li> <a href=""> PHP代码</a></li>
  167. <li> <a href=""> PHP手册</a></li>
  168. <li> <a href=""> 词条</a></li>
  169. <li> <a href=""> 手记</a></li>
  170. <li> <a href=""> 编程词典</a></li>
  171. <li> <a href=""> php培训</a></li>
  172. </ul>
  173. <p>php中文网:公益在线php培训,帮助PHP学习者快速成长!</p>
  174. <div>
  175. <p>Copyright 2014-2020 www.php.cn All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1 </p>
  176. <img src="https://www.php.cn/static/images/foot_line.gif" alt="">
  177. <a href="">关于我们</a>
  178. <a href="">免责申明</a>
  179. <a href="">赞助与捐赠</a>
  180. <a href="">广告合作</a>
  181. </div>
  182. </div>
  183. <div class="code"><img src="images/phpcn_erwei.jpg" alt=""></div>
  184. <div class="code"><img src="images/qq.jpg" alt=""></div>
  185. </div>
  186. </footer>
  187. </body>
  188. </html>

css代码

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. li {
  7. list-style: none;
  8. }
  9. a {
  10. color: #7b7b7b;
  11. text-decoration: none;
  12. }
  13. html {
  14. font-size: 14px;
  15. height: 100vh;
  16. }
  17. @import '../icon-font/iconfont.css';
  18. body {
  19. display: grid;
  20. grid-template-rows: 60px 1fr 175px;
  21. }
  22. /* 头部菜单css */
  23. header {
  24. background-color: black;
  25. display: grid;
  26. grid-template-columns: 140px 1fr 150px;
  27. place-items: center start;
  28. }
  29. header .logo img {
  30. width: 140px;
  31. height: 60px;
  32. }
  33. header .menu {
  34. height: 52px;
  35. font-size: 1rem;
  36. display: grid;
  37. gap: 5px;
  38. place-content: center;
  39. grid-template-columns: repeat(9, 9rem);
  40. }
  41. header .menu li {
  42. place-self: center;
  43. width: 80px;
  44. height: 52px;
  45. line-height: 52px;
  46. text-align: center;
  47. }
  48. header .menu .icon-arrow-down-filling {
  49. color: #aaa;
  50. }
  51. header .menu .icon-direction-down-circle {
  52. color: #FF5722;
  53. }
  54. header .menu .icon-play {
  55. color: #FF5722;
  56. }
  57. header .menu a {
  58. border-bottom: 5px solid black;
  59. }
  60. header .menu li:hover {
  61. border-bottom: 5px solid #5FB878;
  62. transition: .1s;
  63. }
  64. header .menu a {
  65. color: #aaa;
  66. ;
  67. }
  68. header .login {
  69. /* background-color: aqua; */
  70. display: grid;
  71. grid-template-columns: repeat(2, 5rem);
  72. place-items: start end;
  73. grid-template-rows: 3rem;
  74. place-items: center;
  75. }
  76. header .login a {
  77. font-weight: bold;
  78. color: #aaa;
  79. }
  80. /* 主体css */
  81. main {
  82. /* min-height: 82.3vh; */
  83. /* background-color: lightslategray; */
  84. display: grid;
  85. place-content: stretch center;
  86. }
  87. main .top {
  88. width: 1200px;
  89. height: 510px;
  90. display: grid;
  91. margin: 20px 0;
  92. grid-template-columns: 216px 1fr;
  93. box-shadow: 1px 1px 10px #bbbbbb;
  94. border-radius: 10px;
  95. }
  96. /* 主体 > 头部左侧菜单栏css */
  97. main .top .top-left {
  98. height: 510px;
  99. background-color: #2B333B;
  100. border-radius: 10px 0 0 10px;
  101. padding-top: 15px;
  102. }
  103. main .top .top-left li {
  104. line-height: 50px;
  105. padding: 5px 10px 5px 20px;
  106. color: #999;
  107. display: grid;
  108. grid-template-columns: 1fr 20px;
  109. }
  110. main .top .top-left li:hover {
  111. background-color: #6B7176;
  112. }
  113. main .top .top-left li a {
  114. color: #999;
  115. font-size: 1.2rem;
  116. }
  117. main .top .top-left li a:hover {
  118. color: white;
  119. }
  120. main .top .top-right {
  121. /* border-radius: 0 10px 0 0; */
  122. }
  123. /* 主体 > 头部 > 顶部菜单栏css */
  124. main .top .top-right .top-nav {
  125. display: grid;
  126. grid-template-columns: repeat(7, 1fr) 3fr;
  127. grid-template-rows: 60px;
  128. gap: 5px;
  129. }
  130. main .top .top-right .top-nav li {
  131. line-height: 60px;
  132. text-align: center;
  133. }
  134. main .top .top-right .top-nav li a {
  135. color: #333;
  136. }
  137. main .top .top-right .top-nav li a:hover {
  138. color: #999;
  139. }
  140. main .top .top-right .top-nav span {
  141. font-size: 0.9rem;
  142. color: white;
  143. padding: 1px 5px;
  144. border-radius: 2px;
  145. }
  146. main .top .top-right .top-nav span.orange {
  147. background-color: orange;
  148. }
  149. main .top .top-right .top-nav span.darkslategray {
  150. background-color: darkslategray;
  151. }
  152. main .top .top-right .top-nav span.orangered {
  153. background-color: orangered;
  154. }
  155. main .top .top-right .top-search {
  156. display: grid;
  157. width: 260px;
  158. grid-template-columns: 4fr 1fr;
  159. place-self: center start;
  160. background-color: #F1F0F0;
  161. border-bottom: 1px solid #fff;
  162. }
  163. main .top .top-right .top-search:hover {
  164. border-bottom: 1px solid #d6d6d6;
  165. transition: .3s;
  166. }
  167. main .top .top-right .top-search input {
  168. border: none;
  169. outline: none;
  170. height: 40px;
  171. padding-left: 10px;
  172. background-color: #F1F0F0;
  173. }
  174. main .top .top-right .top-search span {
  175. padding-right: 10px;
  176. text-align: center;
  177. line-height: 40px;
  178. background-color: #F1F0F0;
  179. color: #b9b9b9;
  180. cursor: pointer;
  181. }
  182. main .top .top-right .top-slider img {
  183. width: 984px;
  184. }
  185. main .top .top-right .top-img {
  186. width: 984px;
  187. display: grid;
  188. grid-template-columns: repeat(4, 1fr);
  189. grid-template-rows: 108px;
  190. place-items: center;
  191. }
  192. main .top .top-right .top-img img {
  193. border-radius: 5px;
  194. }
  195. /* 主体 > 广告图 */
  196. main .ad img {
  197. border-radius: 5px;
  198. }
  199. /* 主体 > 第二个框架 */
  200. main .main-two {
  201. width: 1200px;
  202. height: 420px;
  203. display: grid;
  204. gap: 10px;
  205. margin: 20px 0;
  206. grid-template-columns: 300px 620px 260px;
  207. }
  208. main .main-two h2 {
  209. font-size: 1.1rem;
  210. padding: 3px;
  211. margin: 0 5px;
  212. line-height: 30px;
  213. color: #292727;
  214. }
  215. main .main-two .toutao {
  216. box-shadow: 2px 2px 10px #e1e1e1;
  217. border-radius: 10px;
  218. padding: 10px;
  219. display: grid;
  220. }
  221. main .main-two .toutao li {
  222. line-height: 30px;
  223. }
  224. main .main-two .toutao li a {
  225. color: #444;
  226. }
  227. main .main-two .toutao li a:hover {
  228. color: #999;
  229. }
  230. .toutao-title {
  231. border-bottom: 1px solid #f0f0f0;
  232. }
  233. main .main-two .course {
  234. display: grid;
  235. gap: 10px;
  236. grid-template-columns: repeat(3, 170px);
  237. grid-template-rows: auto repeat(2, 150px);
  238. place-content: space-evenly;
  239. box-shadow: 2px 2px 10px #e1e1e1;
  240. border-radius: 10px;
  241. }
  242. .course-title {
  243. grid-area: span 1 / 3 span;
  244. margin-top: -6px;
  245. border-bottom: 1px solid #f0f0f0;
  246. }
  247. main .main-two .course li {
  248. box-shadow: 2px 2px 10px #e1e1e1;
  249. position: relative;
  250. display: grid;
  251. margin-bottom: 10px;
  252. grid-template-rows: 2fr 1fr;
  253. border-radius: 10px;
  254. }
  255. main .main-two .course li a {
  256. width: 170px;
  257. background-color: white;
  258. }
  259. main .main-two .course li a:last-of-type {
  260. height: 50px;
  261. position: absolute;
  262. top: 70px;
  263. border-radius: 10px;
  264. padding: 10px;
  265. }
  266. main .main-two .course li span {
  267. background-color: #93999F;
  268. padding: 1px 3px;
  269. margin-right: 5px;
  270. color: white;
  271. font-size: 0.8rem;
  272. }
  273. main .main-two .course img {
  274. width: 170px;
  275. border-radius: 10px;
  276. }
  277. main .main-two .course img:hover {
  278. opacity: 0.7;
  279. transition: 0.2s;
  280. }
  281. main .main-two .notebook {
  282. display: grid;
  283. gap: 5px;
  284. grid-template-rows: auto repeat(6, 1fr);
  285. box-shadow: 2px 2px 10px #e1e1e1;
  286. border-radius: 10px;
  287. }
  288. main .main-two .notebook-title {
  289. display: grid;
  290. border-bottom: 1px solid #f0f0f0;
  291. grid-template-columns: 1fr 40px;
  292. margin: 10px 10px 0 10px;
  293. place-items: center start;
  294. }
  295. main .main-two .notebook img {
  296. width: 40px;
  297. height: 40px;
  298. grid-area: span 2;
  299. }
  300. main .main-two .notebook li {
  301. display: grid;
  302. padding: 5px;
  303. gap: 5px;
  304. grid-template-columns: 40px 1fr;
  305. grid-template-rows: 20px 20px;
  306. place-items: center start;
  307. }
  308. main .main-two .notebook li a {
  309. color: #333;
  310. }
  311. main .main-two .notebook li a:hover {
  312. color: #999;
  313. }
  314. /* 底部css */
  315. footer {
  316. background-color: #393D49;
  317. display: grid;
  318. place-content: stretch center;
  319. }
  320. footer .footer-main {
  321. width: 1200px;
  322. display: grid;
  323. gap: 5px;
  324. grid-template-columns: 1fr 10rem 10rem;
  325. }
  326. footer .footer-main .footer-content {
  327. display: grid;
  328. grid-template-rows: repeat(3, 1fr);
  329. }
  330. footer .footer-main .footer-content>ul {
  331. display: grid;
  332. place-content: end start;
  333. grid-template-columns: repeat(9, auto);
  334. gap: 10px;
  335. }
  336. footer .footer-main .footer-content>ul a {
  337. color: #eee;
  338. }
  339. footer .footer-main .footer-content>div {
  340. display: grid;
  341. color: #979797;
  342. place-content: start;
  343. grid-template-columns: 7fr 25px repeat(4, 1fr);
  344. }
  345. footer .footer-main .footer-content>p {
  346. font-size: 12px;
  347. place-self: center start;
  348. color: #979797;
  349. }
  350. footer .footer-main .footer-content>div>p {
  351. font-size: 10.5px;
  352. place-self: center start;
  353. }
  354. footer .footer-main .footer-content>div>img {
  355. width: 20px;
  356. }
  357. footer .footer-main .code {
  358. place-self: center;
  359. }
  360. footer .footer-main .code img {
  361. width: 100px;
  362. }

3. 理解grid的媒体查询与专业的媒体查询有什么区别?

Grid的媒体查询,根据Grid容器的宽度,除以最小最大值之间的项目宽度,获得auto-fit的值,垂直方向绘制auto-fit个列宽的网格,水平方向也类似绘制,空轨折叠,不占据容器空间。

专业的媒体查询,不是依据某元素的父级容器宽度计算,以屏幕宽度查询

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议