Blogger Information
Blog 19
fans 0
comment 6
visits 19038
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
CSS使用grid和flex布局 仿php中文网首页
葵花宝典
Original
631 people have browsed it

本次布局最大心得:

grid 做大框架布局, flex 作细节布局,有时候还会用到定位,不要用一个属性为难自己
比如在一行中多种不规则对齐,用 flex 很灵活.
在做一些布局规则统一,对齐单一的布局用 grid 来的更快

效果如图:
php中文网首页
php中文网首页
样式和 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. <title>PHP中文网首页</title>
  7. <link rel="stylesheet" href="font/iconfont.css" />
  8. <style>
  9. /* 初始化 */
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. box-sizing: border-box;
  14. }
  15. body {
  16. background-color: #f3f5f7;
  17. }
  18. a {
  19. text-decoration: none;
  20. }
  21. a:link {
  22. color: #333333;
  23. }
  24. a:visited {
  25. color: #333333;
  26. }
  27. a:hover {
  28. color: #636262;
  29. }
  30. li {
  31. list-style: none;
  32. }
  33. /* 导航UL布局 */
  34. .header ul {
  35. display: grid;
  36. grid-template-columns: 200px 1fr 8rem;
  37. background-color: black;
  38. height: 60px;
  39. }
  40. .header a {
  41. color: #a8b4ae;
  42. font-size: 0.9rem;
  43. }
  44. .header .nav a {
  45. height: 60px;
  46. padding-top: 20px;
  47. }
  48. .header .nav a:hover {
  49. color: white;
  50. border-bottom: 5px solid green;
  51. }
  52. .header img {
  53. height: 60px;
  54. }
  55. .header .nav {
  56. display: grid;
  57. grid-template-columns: repeat(9, 6rem);
  58. place-content: center start;
  59. }
  60. .header .login {
  61. place-self: center start;
  62. }
  63. .header .login a {
  64. padding-left: 10px;
  65. font-weight: 800;
  66. }
  67. .header .login a:hover {
  68. color: white;
  69. }
  70. /* 主体第一部分 */
  71. .main .tj {
  72. background-color: white;
  73. border-radius: 10px;
  74. height: 510px;
  75. display: grid;
  76. grid-template-columns: 200px 1fr;
  77. grid-template-rows: 60px 1fr 105px;
  78. height: 510px;
  79. width: 1200px;
  80. margin: 2rem auto;
  81. }
  82. .main .kf-type {
  83. background-color: #2b333b;
  84. border-top-left-radius: 10px;
  85. border-bottom-left-radius: 10px;
  86. grid-area: span 3;
  87. color: #aaadb0;
  88. }
  89. .main .kf-type ul {
  90. padding: 10px 10px 0 30px;
  91. }
  92. .main .kf-type li {
  93. font-size: 1rem;
  94. height: 60px;
  95. display: flex;
  96. flex-flow: row nowrap;
  97. justify-content: space-between;
  98. align-items: center;
  99. }
  100. /* 推荐课程菜单 */
  101. .main .tj-nav {
  102. border-top-right-radius: 10px;
  103. display: flex;
  104. flex-flow: row nowrap;
  105. align-items: center;
  106. }
  107. .main .tj-nav a {
  108. font-size: 0.9rem;
  109. flex: 0 0 10%;
  110. padding: 10px;
  111. }
  112. .main .tj-nav .so {
  113. height: 40px;
  114. border-radius: 10px;
  115. flex: 0 0 28%;
  116. background-color: #f1f0f0;
  117. display: flex;
  118. flex-flow: row nowrap;
  119. align-items: center;
  120. }
  121. .main .tj-nav .so input {
  122. margin-left: 10px;
  123. border: none;
  124. background-color: #f1f0f0;
  125. flex: 0 0 80%;
  126. }
  127. /* 轮播图片 */
  128. .main .lb-img {
  129. width: 100%;
  130. }
  131. /* 底部推荐课程 */
  132. .main .kcheng {
  133. display: flex;
  134. flex-flow: row nowrap;
  135. align-items: center;
  136. justify-content: space-evenly;
  137. border-bottom-right-radius: 10px;
  138. }
  139. .main .kcheng img {
  140. border-radius: 10px;
  141. }
  142. .main .toutiao {
  143. display: grid;
  144. grid-template-columns: 300px 620px 260px;
  145. grid-template-rows: 416px;
  146. gap: 10px;
  147. height: 510px;
  148. width: 1200px;
  149. margin: 2rem auto;
  150. }
  151. .main .toutiao .toutiao_left {
  152. background-color: white;
  153. border-radius: 10px;
  154. }
  155. .main .toutiao .toutiao_center {
  156. background-color: white;
  157. border-radius: 10px;
  158. }
  159. .main .toutiao .toutiao_right {
  160. background-color: white;
  161. border-radius: 10px;
  162. }
  163. .main .toutiao {
  164. font-size: 0.9rem;
  165. }
  166. .main p {
  167. line-height: 30px;
  168. padding-left: 10px;
  169. }
  170. .main .toutiao .title {
  171. border-bottom: 1px solid #f3f5f7;
  172. margin: 5px;
  173. padding: 10px;
  174. font-weight: 800;
  175. }
  176. .main .toutiao .toutiao_center .content {
  177. display: grid;
  178. grid-template-columns: repeat(3, 170px);
  179. grid-template-rows: 170px 170px;
  180. gap: 0 20px;
  181. place-content: center;
  182. }
  183. .main .toutiao .toutiao_center .content a {
  184. place-self: center;
  185. border-radius: 10px;
  186. box-shadow: 0 0 3px #999;
  187. }
  188. .main .toutiao .toutiao_center .content div {
  189. padding: 10px;
  190. margin-top: -30px;
  191. background-color: white;
  192. position: relative;
  193. border-radius: 10px;
  194. line-height: 20px;
  195. }
  196. .main .toutiao .toutiao_center .content label {
  197. background-color: #999;
  198. color: white;
  199. font-size: 12px;
  200. padding: 3px;
  201. margin-right: 10px;
  202. }
  203. .main .toutiao .toutiao_center .content img {
  204. width: 170px;
  205. border-radius: 10px;
  206. }
  207. .main .toutiao .toutiao_right .content img {
  208. width: 40px;
  209. margin: 5px 10px;
  210. }
  211. .main .toutiao .toutiao_right .content div {
  212. display: flex;
  213. flex-flow: row nowrap;
  214. }
  215. .main .toutiao .toutiao_right .content a {
  216. padding: 5px;
  217. }
  218. .main .toutiao .toutiao_right .title {
  219. display: flex;
  220. flex-flow: row nowrap;
  221. justify-content: space-between;
  222. }
  223. </style>
  224. </head>
  225. <body>
  226. <header class="header">
  227. <ul>
  228. <li class="log">
  229. <a href=""><img src="img/logo.png" alt="" /></a>
  230. </li>
  231. <li class="nav">
  232. <a href="">首页</a>
  233. <a href="">视频教程</a>
  234. <a href="">入门教程</a>
  235. <a href="">社区问答</a>
  236. <a href="">技术文章</a>
  237. <a href="">资源下载</a>
  238. <a href="">编程词典</a>
  239. <a href="">工具下载</a>
  240. <a href="">PHP培训</a>
  241. </li>
  242. <li class="login"><a href="">登录</a> <a href="">注册</a></li>
  243. </ul>
  244. </header>
  245. <main class="main">
  246. <!-- 推荐课程区域 -->
  247. <div class="tj">
  248. <!-- 左则导航 -->
  249. <div class="kf-type">
  250. <ul>
  251. <li><a>PHP开发</a><i class="iconfont icon-more"></i></li>
  252. <li><a>前端开发</a><i class="iconfont icon-more"></i></li>
  253. <li><a>服务端开发</a><i class="iconfont icon-more"></i></li>
  254. <li><a>称动开发</a><i class="iconfont icon-more"></i></li>
  255. <li><a>数据库</a><i class="iconfont icon-more"></i></li>
  256. <li><a>服务器运维&下载</a><i class="iconfont icon-more"></i></li>
  257. <li><a>在线工具箱</a><i class="iconfont icon-more"></i></li>
  258. <li><a>常用类库</a><i class="iconfont icon-more"></i></li>
  259. </ul>
  260. </div>
  261. <!-- 上部导航 -->
  262. <div class="tj-nav">
  263. <a href="">PHP头条</a>
  264. <a href="">独孤九剑</a>
  265. <a href="">学习路线</a>
  266. <a href="">在线工具</a>
  267. <a href="">吃饭课堂</a>
  268. <a href="">社区问答</a>
  269. <a href="">课程直播</a>
  270. <div class="so">
  271. <input type="text" placeholder="输入关键词搜索" /><span
  272. class="iconfont icon-search"
  273. ></span>
  274. </div>
  275. </div>
  276. <!-- 轮播图片 -->
  277. <div class="lb-img">
  278. <img src="img/1.jpg" alt="" />
  279. </div>
  280. <div class="kcheng">
  281. <a><img src="img/2.jpg" alt="" /></a>
  282. <a><img src="img/3.png" alt="" /></a>
  283. <a><img src="img/4.jpg" alt="" /></a>
  284. <a><img src="img/5.jpg" alt="" /></a>
  285. </div>
  286. </div>
  287. <!-- 头条区域 -->
  288. <div class="toutiao">
  289. <div class="toutiao_left">
  290. <div class="title">头条</div>
  291. <a><p>php中文网原创视频:《天龙八部》公益ph</p></a>
  292. <a><p>php中文网《玉女心经》公益PHP WEB培</p></a>
  293. <a><p>五款漂亮的2021倒计时动态特效源码(免</p></a>
  294. <a><p>元旦福利:2021高品质新课上线汇总!</p></a>
  295. <a><p>十款炫酷的程序员圣诞节代码特效【免费下</p></a>
  296. <a><p>值得研究的20个Vue开源项目</p></a>
  297. <a><p>【推荐】17个提升用户体验图像特效库</p></a>
  298. <a><p>各种知名网站的404页面,看看谁更有创意?</p></a>
  299. <a><p>Chrome性能有了最大提升!(Chrome87新</p></a>
  300. <a><p>PHP 8 正式发布了!</p></a>
  301. <a><p>推荐10款GitHub中高价值的PHP项目(值</p></a>
  302. <a><p>推荐2021前端开发者11个必备的网站</p></a>
  303. </div>
  304. <div class="toutiao_center">
  305. <div class="title">最新课程</div>
  306. <div class="content">
  307. <a href=""
  308. ><img src="img/7.png" alt="" />
  309. <div>
  310. <label for="">中级</label>JavaScript对象与常用设计模式
  311. </div></a
  312. >
  313. <a href=""
  314. ><img src="img/8.png" alt="" />
  315. <div><label for="">高级</label>tp5怎么遍历多维数组。</div>
  316. </a>
  317. <a href=""
  318. ><img src="img/9.jpeg" alt="" />
  319. <div><label for="">中级</label>右面执行无结果无结果</div>
  320. </a>
  321. <a href=""
  322. ><img src="img/10.png" alt="" />
  323. <div><label for="">中级</label>J您没有访问此服务器的权限</div>
  324. </a>
  325. <a href=""
  326. ><img src="img/11.png" alt="" />
  327. <div>
  328. <label for="">高级</label>在win10中如何创建python格式文本
  329. </div>
  330. </a>
  331. <a href=""
  332. ><img src="img/12.png" alt="" />
  333. <div><label for="">中级</label>Jphp项目怎么修改和运行发布</div>
  334. </a>
  335. </div>
  336. </div>
  337. <div class="toutiao_right">
  338. <div class="title"><span>常用手册</span><a>更多</a></div>
  339. <div class="content">
  340. <div>
  341. <img src="img/14.jpg" alt="" /><a>
  342. php手册Linux手册ThinkPHP6.0CI手册大全</a
  343. >
  344. </div>
  345. <div>
  346. <img src="img/15.jpg" alt="" /><a
  347. >JavaScript中文参考手册jQuery手册大全</a
  348. >
  349. </div>
  350. <div>
  351. <img src="img/16.jpg" alt="" /><a
  352. >MySQL参考手册大全 laravel5.8速查表</a
  353. >
  354. </div>
  355. <div>
  356. <img src="img/17.jpg" alt="" /><a
  357. >Python参考手册大全Node.js中文学习手册</a
  358. >
  359. </div>
  360. <div>
  361. <img src="img/18.jpg" alt="" /><a
  362. >tml手册CSS手册AngularJSAjax手册</a
  363. >
  364. </div>
  365. <div>
  366. <img src="img/19.jpg" alt="" /><a
  367. >ASP参考手册大全Bootstrap3参考手册</a
  368. >
  369. </div>
  370. </div>
  371. </div>
  372. </div>
  373. </main>
  374. <footer class="footer"></footer>
  375. </body>
  376. </html>
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