Grid 布局实现PHP中文网首页课程列表
实战项目图如下:
使用Grid布局效果:
HTML&DOM结构:
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Grid布局实战</title>
<link rel="stylesheet" href="./static/css/style.css" />
</head>
<body>
<div class="container">
<div class="item">php入门精品课程</div>
<div class="item"><img src="./static/img/ad01.jpg" alt="" /></div>
<div class="item">
<img src="./static/img/1.png" alt="" />
<a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
<span>4W+次播放</span>
</div>
<div class="item">
<img src="./static/img/2.jpg" alt="" />
<a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
<span>4W+次播放</span>
</div>
<div class="item">
<img src="./static/img/3.jpg" alt="" />
<a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
<span>4W+次播放</span>
</div>
<div class="item">
<img src="./static/img/4.jpg" alt="" />
<a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
<span>4W+次播放</span>
</div>
<div class="item">
<img src="./static/img/5.jpg" alt="" />
<a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
<span>4W+次播放</span>
</div>
<div class="item">
<img src="./static/img/6.jpeg" alt="" />
<a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
<span>4W+次播放</span>
</div>
<div class="item">
<img src="./static/img/7.jpg" alt="" />
<a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
<span>4W+次播放</span>
</div>
<div class="item">
<img src="./static/img/8.jpg" alt="" />
<a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
<span>4W+次播放</span>
</div>
<div class="item">
<img src="./static/img/9.jpg" alt="" />
<a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
<span>4W+次播放</span>
</div>
<div class="item">
<img src="./static/img/10.jpg" alt="" />
<a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
<span>4W+次播放</span>
</div>
<div class="item">
<img src="./static/img/11.jpg" alt="" />
<a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
<span>4W+次播放</span>
</div>
<div class="item">
<img src="./static/img/12.jpg" alt="" />
<a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
<span>4W+次播放</span>
</div>
<div class="item">
<img src="./static/img/13.jpg" alt="" />
<a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
<span>4W+次播放</span>
</div>
</div>
</body>
</html>
CSS样式:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
overflow: hidden;
overflow-y: auto;
font: 14px Helvetica Neue, Helvetica, PingFang SC, 微软雅黑, Tahoma, Arial,
sans-serif;
background: #f3f5f7;
}
li,
dd {
list-style: none;
}
a {
text-decoration: none;
}
button,
img {
border: none;
outline: none;
}
.container {
width: 1200px;
height: 650px;
margin: 10px auto;
background: #fff;
display: grid;
grid-template-columns: repeat(5, 20%);
grid-template-rows: 80px 190px 190px 190px;
}
.container i{
font-style: normal;
font-size: 12px;
color: #fff;
border-radius: 1px;
padding: 2px;
background-color: #93999f;
margin-right: 2px;
}
.container .item:first-of-type {
grid-column: span 5;
line-height: 80px;
text-align: center;
font-size: 20px;
font-weight: 600;
color: #4d555d;
}
.container .item:nth-child(2) {
grid-row: span 2;
/* background: lightseagreen; */
margin: 0 auto;
}
.container .item:nth-child(2) img {
border-radius: 8px;
}
.container .item:nth-child(n + 3) {
margin: 0 auto;
position: relative;
}
.container .item:nth-child(n + 3) img {
width: 217px;
height: 124px;
border-radius: 8px;
border: none;
}
.container .item:nth-child(n + 3) a {
display: block;
border-radius: 8px;
padding: 15px 20px;
position: absolute;
top: 92px;
transition: top 0.3s;
height: 80px;
width: 217px;
background: #fff;
color: #555;
}
.container .item:nth-child(n + 3) a:hover {
color: #333;
top: 55px;
}
.container .item:nth-child(n + 3) span {
position: absolute;
top: 130px;
left: 0;
width: 217px;
height: 42px;
padding: 0 20px;
line-height: 42px;
background: #fff;
color: #93999f;
font-size: 12px;
border-bottom-left-radius: 8px;
border-bottom-right-radius: 8px;
box-shadow: 0 4px 8px 0 rgba(7, 17, 27, 0.1);
}
总结:
1、使用Grid布局轻松实现以上效果,对比基础布局及FlexBox盒子布局好太多了;
2、Grid布局主要属性如下:
/*1、父容器中定义Grid布局 */
display: grid;
/*-----------------------------------*/
/*2、父容器中设定行/列*/
/*设定行*/
grid-template-rows: 30px 1fr 30px;
/*设定列*/
grid-template-columns: 30px 1fr 30px;
/*以上为3行3列布局,fr为剩余空间分为几份(相当与百分比),同样支持%为单位。*/
/*如多个行或列均为相同尺寸,可以使用repeat参数,比如: grid-template-columns: repeat(5, 25px);*/
/*-----------------------------------*/
/*3、网格合并*/
/*方法1、指定开始和结束的网格线*/
grid-column-start: 1;
grid-column-end: 2;
/*方法2、结束属性指定跨越几行或几列*/
grid-column-start:1;
grid-column-end: span 2;
/*方法3、简化( grid-column: 起始列号 / 结束列号)*/
grid-column: 1 / 2;
/*结束参数支持指定跨越几行或几列*/
/*-----------------------------------*/
/*Grid自定义名称模板布局*/
/*父容器中定义*/
grid-template-areas:
"head head head"
"left main right"
"footer footer footer";
/*项目中需命名*/
grid-area: 网格名称;