Blogger Information
Blog 37
fans 0
comment 0
visits 34711
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
grid布局中的对齐方式
手机用户1607314868
Original
4819 people have browsed it

grid布局中的对齐方式

对齐方式分为两大类,一是项目在网格单元内的对齐方式,而是项目在容器内的对齐方式

项目在网格单元内的对齐方式

在声明了grid布局容器内使用place-items:垂直 水平;属性
注意:垂直方向:start 代表上 center 中 end 下
水平方向:start 代表左 center中 end 右
如果垂直方向和水平方向值一样可以只写一个

单独设置某个项目的对齐方式

使用place-self:垂直 水平;属性

  1. <style>
  2. .container{
  3. border: 1px solid #000;
  4. padding: .5em;
  5. display: grid;
  6. grid-template-columns: repeat(3,1fr);
  7. grid-template-rows: repeat(2,5em);
  8. grid-auto-rows: 5em;
  9. gap: 0.5em;
  10. }
  11. .container>.item{
  12. background-color: lightcyan;
  13. border: 1px solid #000;
  14. padding: 0.5em;
  15. }
  16. .container>.item{
  17. width: 5em;
  18. height: 2em;
  19. }
  20. /* 项目在网格单元的对齐方式 */
  21. .container{
  22. /* place-items:垂直方向,水平方向 */
  23. /* 垂直向上,水平居中 */
  24. place-items: start center;
  25. /* 垂直水平+都居中 当第一个值和第二个值一样可以省略*/
  26. place-items: center;
  27. /* 继承 */
  28. place-items: inherit;
  29. /* 初始化 */
  30. place-items: initial;
  31. /* 取消项目的固定尺寸,拉伸 */
  32. place-items: stretch;
  33. }
  34. /* 单独设置项目的对齐方式 */
  35. .container>.item:nth-of-type(5){
  36. background-color: yellow;
  37. /* 垂直居中 */
  38. place-self: center center;
  39. place-self: end start;
  40. }
  41. </style>
  42. <body>
  43. <div class="container">
  44. <span class="item">item1</span>
  45. <span class="item">item2</span>
  46. <span class="item">item3</span>
  47. <span class="item">item4</span>
  48. <span class="item">item5</span>
  49. <span class="item">item6</span>
  50. <span class="item">item7</span>
  51. <span class="item">item8</span>
  52. <span class="item">item9</span>
  53. </div>
  54. </body>

项目在容器内的对齐方式

使用属性:place-content

  • 将所有项目作为一个整体在容器中的对齐方式
  • 将所有项目打散成一个个独立个体在容器中设置对齐方式
    注意:容器必须留有剩余空间,否则无法分配
  1. <style>
  2. .container{
  3. height: 25em;
  4. border: 1px solid #000;
  5. padding: 0.5em;
  6. display: grid;
  7. /* 宽度得是固定值,得有剩余空间才能分配 */
  8. grid-template-columns: repeat(3,10em);
  9. grid-template-rows: repeat(2,5em);
  10. grid-auto-rows: 5em;
  11. gap: 0.5em;
  12. }
  13. /* 项目在网格容器的对齐方式 */
  14. .container{
  15. /* 1.将所有项目作为一个整体在容器中对齐 */
  16. place-content: start start;
  17. place-content: start center;
  18. place-content: center;
  19. /* 2.将所有项目打散成一个个独立个体在容器中设置对齐方式 */
  20. /* 水平垂直 两端对齐 */
  21. place-content: space-between space-between;
  22. /* 垂直分散对齐 水平平均对齐*/
  23. place-content: space-around space-evenly;
  24. }
  25. </style>

php中文网的布局




  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. <title>php中文网首页</title>
  7. <style>
  8. *{
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. li{
  14. list-style: none;
  15. }
  16. a{
  17. color:#444;
  18. text-decoration: none;
  19. }
  20. header{
  21. height: 60px;
  22. background-color:gray;
  23. margin-bottom: 30px;
  24. }
  25. header>ul.hnav{
  26. display: grid;
  27. grid-template-columns: repeat(8,1fr);
  28. grid-template-rows: 60px;
  29. place-items: center;
  30. }
  31. footer{
  32. height: 160px;
  33. color:#EEE;
  34. background-color: #444;
  35. }
  36. .main-top{
  37. width: 1200px;
  38. height: 510px;
  39. display: grid;
  40. grid-template-columns: 216px 1fr;
  41. grid-template-rows: 60px 1fr 120px;
  42. margin: auto;
  43. }
  44. .main-top>nav.menus{
  45. background-color: lightgreen;
  46. grid-area: span 3;
  47. border-radius: 10px 0 0 10px;
  48. }
  49. .main-top>ul.nav{
  50. background-color: lightcyan;
  51. display: grid;
  52. grid-template-columns: repeat(8,83px) 1fr;
  53. place-items: center;
  54. }
  55. .main-top>ul.nav>li:last-of-type{
  56. place-self: center start;
  57. padding-left: 30px;
  58. }
  59. .main-top>ul.course{
  60. background-color: wheat;
  61. display: grid;
  62. padding:10px;
  63. grid-template-columns: repeat(4,1fr);
  64. gap: 10px;
  65. }
  66. .main-top>ul.course>*{
  67. background-color: violet;
  68. cursor: pointer;
  69. }
  70. /* 横图 */
  71. .row-image{
  72. height: 80px;
  73. margin-top: 20px;
  74. background-color: lightslategray;
  75. margin: auto;
  76. }
  77. .row-image h1{
  78. text-align: center;
  79. line-height:80px;
  80. color: white;
  81. }
  82. /* ------------------------------------------------------------------------------------------ */
  83. .recommend{
  84. width: 1200px;
  85. height: 446px;
  86. background-color: yellowgreen;
  87. margin-top: 20px;
  88. display: grid;
  89. grid-template-columns: 300px 1fr 260px;
  90. padding: 15px;
  91. gap: 10px;
  92. margin: auto;
  93. }
  94. .recommend .article{
  95. background-color: lightcyan;
  96. display: grid;
  97. grid-template-rows:36px 1fr;
  98. padding: 15px;
  99. }
  100. .recommend .article h4{
  101. margin-left: 10px;
  102. }
  103. .recommend .article>ul.artul{
  104. display: grid;
  105. grid-template-rows: repeat(12,1fr);
  106. }
  107. /* ----------------------------------- */
  108. .recommend .course{
  109. background-color: lightcyan;
  110. display: grid;
  111. grid-template-rows:36px 1fr;
  112. padding: 15px;
  113. }
  114. .recommend .course h4{
  115. margin-left: 10px;
  116. }
  117. .recommend .course>ul.cou-list{
  118. display: grid;
  119. grid-template-columns: repeat(3,1fr);
  120. grid-template-rows: repeat(2,1fr);
  121. gap: 20px;
  122. }
  123. .recommend .course>ul.cou-list>*{
  124. background-color: lightgreen;
  125. border-radius: 10px;
  126. }
  127. /* ------------------------------------ */
  128. .recommend .manual{
  129. background-color: lightcyan;
  130. display: grid;
  131. grid-template-rows:36px 1fr;
  132. padding: 15px;
  133. }
  134. .recommend .manual>div:first-of-type{
  135. margin-left: 10px;
  136. display: flex;
  137. justify-content: space-between;
  138. }
  139. .recommend .manual>ul.man-list{
  140. display: grid;
  141. grid-template-rows: repeat(6,1fr);
  142. }
  143. .recommend .manual>ul.man-list>li{
  144. display: grid;
  145. grid-template-columns: 50px 1fr;
  146. gap: 10px;
  147. margin-bottom: 10px;
  148. }
  149. .recommend .manual>ul.man-list>li>img{
  150. background-color: lightsalmon;
  151. }
  152. .recommend .manual>ul.man-list>li>a{
  153. place-self: center start;
  154. }
  155. /* ---------------------------------------------------------------------- */
  156. .main-courses{
  157. width: 1200px;
  158. height: 646px;
  159. padding: 15px;
  160. background-color: lightskyblue;
  161. margin: 30px auto;
  162. display: grid;
  163. grid-template-rows: 50px 1fr;
  164. gap:20px;
  165. }
  166. .main-courses h3{
  167. color:#444;
  168. text-align: center;
  169. margin-bottom: 30px;
  170. }
  171. .main-courses .course-list{
  172. display: grid;
  173. grid-template-columns: repeat(5,1fr);
  174. grid-template-rows: repeat(3,1fr);
  175. gap: 20px;
  176. }
  177. .main-courses .course-list>*{
  178. background-color: lightcyan;
  179. border-radius: 10px;
  180. }
  181. .main-courses .course-list>li:first-of-type{
  182. grid-area: span 2;
  183. }
  184. </style>
  185. </head>
  186. <body>
  187. <header>
  188. <ul class="hnav">
  189. <li><a href="">item1</a></li>
  190. <li><a href="">item2</a></li>
  191. <li><a href="">item3</a></li>
  192. <li><a href="">item4</a></li>
  193. <li><a href="">item5</a></li>
  194. <li><a href="">item6</a></li>
  195. <li><a href="">item7</a></li>
  196. <li><a href="">item8</a></li>
  197. </ul>
  198. </header>
  199. <div class="main-top">
  200. <nav class="menus">菜单组</nav>
  201. <!-- 顶部菜单 -->
  202. <ul class="nav">
  203. <li><a href="">item1</a></li>
  204. <li><a href="">item2</a></li>
  205. <li><a href="">item3</a></li>
  206. <li><a href="">item4</a></li>
  207. <li><a href="">item5</a></li>
  208. <li><a href="">item6</a></li>
  209. <li><a href="">item7</a></li>
  210. <li><a href="">item8</a></li>
  211. <li><input type="text" placeholder="输入关键字"></li>
  212. </ul>
  213. <div class="slider">轮播图</div>
  214. <!-- 底部课程推荐 -->
  215. <ul class="course">
  216. <li><a href=""><img src="" alt=""></a></li>
  217. <li><a href=""><img src="" alt=""></a></li>
  218. <li><a href=""><img src="" alt=""></a></li>
  219. <li><a href=""><img src="" alt=""></a></li>
  220. </ul>
  221. </div>
  222. <div class="row-image">
  223. <h1>php编程就业班</h1>
  224. </div>
  225. <div class="recommend">
  226. <div class="article">
  227. <h4>头条</h4>
  228. <ul class="artul">
  229. <li><a href="">php中文网原创视频公益php</a></li>
  230. <li><a href="">php中文网原创视频公益php</a></li>
  231. <li><a href="">php中文网原创视频公益php</a></li>
  232. <li><a href="">php中文网原创视频公益php</a></li>
  233. <li><a href="">php中文网原创视频公益php</a></li>
  234. <li><a href="">php中文网原创视频公益php</a></li>
  235. <li><a href="">php中文网原创视频公益php</a></li>
  236. <li><a href="">php中文网原创视频公益php</a></li>
  237. <li><a href="">php中文网原创视频公益php</a></li>
  238. <li><a href="">php中文网原创视频公益php</a></li>
  239. <li><a href="">php中文网原创视频公益php</a></li>
  240. <li><a href="">php中文网原创视频公益php</a></li>
  241. </ul>
  242. </div>
  243. <div class="course">
  244. <h4>最新课程</h4>
  245. <ul class="cou-list">
  246. <li><a href=""><img src="" alt=""></a></li>
  247. <li><a href=""><img src="" alt=""></a></li>
  248. <li><a href=""><img src="" alt=""></a></li>
  249. <li><a href=""><img src="" alt=""></a></li>
  250. <li><a href=""><img src="" alt=""></a></li>
  251. <li><a href=""><img src="" alt=""></a></li>
  252. </ul>
  253. </div>
  254. <div class="manual">
  255. <div><h4>常用手册</h4><span>更多</span></div>
  256. <ul class="man-list">
  257. <li><img src="" alt=""><a href="">item1</a></li>
  258. <li><img src="" alt=""><a href="">item2</a></li>
  259. <li><img src="" alt=""><a href="">item3</a></li>
  260. <li><img src="" alt=""><a href="">item4</a></li>
  261. <li><img src="" alt=""><a href="">item5</a></li>
  262. <li><img src="" alt=""><a href="">item6</a></li>
  263. </ul>
  264. </div>
  265. </div>
  266. <!-- 课程列表区 -->
  267. <div class="main-courses">
  268. <h3>php入门精品课程</h3>
  269. <ul class="course-list">
  270. <li><a href=""><img src="" alt=""></a></li>
  271. <li><a href=""><img src="" alt=""></a></li>
  272. <li><a href=""><img src="" alt=""></a></li>
  273. <li><a href=""><img src="" alt=""></a></li>
  274. <li><a href=""><img src="" alt=""></a></li>
  275. <li><a href=""><img src="" alt=""></a></li>
  276. <li><a href=""><img src="" alt=""></a></li>
  277. <li><a href=""><img src="" alt=""></a></li>
  278. <li><a href=""><img src="" alt=""></a></li>
  279. <li><a href=""><img src="" alt=""></a></li>
  280. <li><a href=""><img src="" alt=""></a></li>
  281. <li><a href=""><img src="" alt=""></a></li>
  282. <li><a href=""><img src="" alt=""></a></li>
  283. <li><a href=""><img src="" alt=""></a></li>
  284. </ul>
  285. </div>
  286. <footer>页脚</footer>
  287. </body>
  288. </html>

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

grid的媒体查询兼容性有问题
media媒体查询兼容性强,但需要写多个媒体查询

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
0 comments
Author's latest blog post