网格布局简介
CSS3网格布局是让开发人员设计一个网格并将内容放在这些网格内。而不是使用浮动制作一个网格,实际上是你将一个元素声明为一个网格容器,并把元素内容置于网格中。
有一些浏览器是不支持网格布局的,可以从caniuse这个网站查看各个浏览器是否支持这些css3中的新属性,绿色表示支持,红色表示不支持,另一种颜色表示部分支持,如图(查看各个浏览器是否支持grid布局):
通过下面这张图片可以了解网格布局的基本概念
先来个表情包happy一下吧哈哈哈
代码部分
页面布局:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>WEUI GRID</title>
<script src="http://g.tbcdn.cn/mtb/lib-flexible/0.3.4/??flexible_css.js,flexible.js"></script>
<link rel="stylesheet" href="./static/css/common.css">
<link rel="stylesheet" href="./static/css/grid.css">
<link rel="stylesheet" href="./static/css/iconfont.css">
</head>
<body>
<div class="container">
<div class="page__hd">
<p class="page__title">腾讯服务</p>
</div>
<!-- 九宫格 start-->
<div class="grids">
<a href="#" class="grid">
<i class="iconfont icon-xinyongkahuankuan"></i>
<p class="grid__label">信用卡还款</p>
</a>
<a href="#" class="grid">
<i class="iconfont icon-jieqian"></i>
<p class="grid__label">微粒贷借钱</p>
</a>
<a href="#" class="grid">
<i class="iconfont icon-dibugeicon_jieqian"></i>
<p class="grid__label">手机充值</p>
</a>
<a href="#" class="grid">
<i class="iconfont icon-licaitong"></i>
<p class="grid__label">理财通</p>
</a>
<a href="#" class="grid">
<i class="iconfont icon-shenghuojiaofei"></i>
<p class="grid__label">生活缴费</p>
</a>
<a href="#" class="grid">
<i class="iconfont icon-qq"></i>
<p class="grid__label">Q币充值</p>
</a>
<a href="#" class="grid">
<i class="iconfont icon-chengshifuwu"></i>
<p class="grid__label">城市服务</p>
</a>
<a href="#" class="grid">
<i class="iconfont icon-love"></i>
<p class="grid__label">腾讯公益</p>
</a>
<a href="#" class="grid"></a>
</div>
<!-- 九宫格 end -->
</div>
</div>
</body>
</html>
公共样式css:
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
width: 100%;
overflow-x: hidden;
}
body {
line-height: 1.6;
background-color: #EFEFF4;
}
.container{
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
font-size: 0.37037037rem;
}
.page__hd {
padding: 10px;
}
.page__title {
text-align: left;
color: #716F76;
}
.grids {
display: grid;
grid-template-columns: repeat(3,3.33333333rem);
grid-template-rows: repeat(3,3.33333333rem);
}
.grid {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
text-decoration: none;
background-color: #FFFFFF;
color: #707070;
}
在grids中设置网格布局,display:grid;grid-template-columns属性设置网格布局中每列的宽度,grid-template-rows属性设置网格布局中每行的高度,由于在head中导入了淘宝的flexible,所以设置宽高时用了相对单位rem,grid-template-columns: repeat(3,3.33333333rem)相当于grid-template-columns: 3.33333333rem 3.33333333rem 3.33333333rem,grid-template-rows: repeat(3,3.33333333rem)同理;
在微信的界面中是有“边框的”,这里用了伪元素来实现
代码如下:
/* author: 倪子红
date: 17-11-28 */
.grids {
position: relative;
overflow: hidden;
}
.grids:before {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
height: 1px;
border-top: 1px solid #E5E5E5;
/* 1px 问题 retina 手机 1px很粗*/
/* -webkit-transform: scaleY(0.5);
transform: scaleY(0.5);
-webkit-transform-origin: 0 0;
transform-origin: 0 0; */
}
.grids:after {
content: "";
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 1px;
border-bottom: 1px solid #E5E5E5;
}
.grid {
position: relative;
float: left;
padding: 20px 10px;
box-sizing: border-box;
}
.grid:before {
content: "";
position: absolute;
right: 0;
top: 0;
bottom: 0;
width: 1px;
border-right: 1px solid #E5E5E5;
}
.grid:after {
content: "";
position: absolute;
right: 0;
left: 0;
bottom: 0;
height: 1px;
border-bottom: 1px solid #E5E5E5;
}
有的手机存在“1px问题”,可以通过transform和transform-origin属性来解决这个问题
我的所有图标是从iconfont这个网站得到的,下面也放出图标的样式
@font-face {font-family: "iconfont";
src: url('../iconfont.eot?t=1511887164151'); /* IE9*/
src: url('../iconfont.eot?t=1511887164151#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('data:application/x-font-woff;charset=utf-8;base64,d09GRgABAAAAAAsgAAsAAAAADwAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABHU1VCAAABCAAAADMAAABCsP6z7U9TLzIAAAE8AAAARAAAAFZW8kgqY21hcAAAAYAAAACXAAACCGdFARRnbHlmAAACGAAABqIAAAfck5S57mhlYWQAAAi8AAAAMQAAADYPqqh4aGhlYQAACPAAAAAgAAAAJAfjA4NobXR4AAAJEAAAABoAAAAoJ+r/+WxvY2EAAAksAAAAFgAAABYLhAjubWF4cAAACUQAAAAdAAAAIAEcAI9uYW1lAAAJZAAAAUUAAAJtPlT+fXBvc3QAAAqsAAAAdAAAAJggGtG0eJxjYGRgYOBikGPQYWB0cfMJYeBgYGGAAJAMY05meiJQDMoDyrGAaQ4gZoOIAgCKIwNPAHicY2Bk/sc4gYGVgYOpk+kMAwNDP4RmfM1gxMjBwMDEwMrMgBUEpLmmMDgwVDwzZm7438AQw9zO0AIUZgTJAQApiAyweJzFkTEOwyAQBJdASHCQC8u1Jffp/Ju8hCJ1qrxzv+EsHFLkF3jRIO1J3KE9AFcAXjxFANwXDlUfVV2rewytHvCSH3HHBRGFmRMXrtz2XfXCdPB/Ob0ZMfdTfcRNXZJme/WqUyNOkztv9FG53e/ukigdfZHJUHbgYChF8GEoTzAb9S0no+6Xi1H3zNVQ7uBmIPwAwqQjpAB4nE1Ua2wcVxW+59557Wt2Z2Z3HuvdnZ2dnRl7bW+8790kXruu28SOiV25ThU3CS5JbNEXgVYkbdxigkpSgVBQqkIRD4FCE9oIgUoTJFpQ1f4JAgECidYgBZQIIQFCFeJRyV5zry0kRnOPzpx7z5l7vvt9F/EIbf2RvEFMpKF+VEGTaA4hEAahIOMsOEG9jAch5fApIymTwA0c0S2UySgYBSGpV5t13xBEIQ4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woAN//8DxH9BbABBDAUIAAAAAAAAAHYArgF2AeoCbgKuAzwDqAPuAAB4nGNgZGBg4GJoZuBgAAEmMI8LSP4H8xkAFtsBrAAAAHicZY9NTsMwEIVf+gekEqqoYIfkBWIBKP0Rq25YVGr3XXTfpk6bKokjx63UA3AejsAJOALcgDvwSCebNpbH37x5Y08A3OAHHo7fLfeRPVwyO3INF7gXrlN/EG6QX4SbaONVuEX9TdjHM6bCbXRheYPXuGL2hHdhDx18CNdwjU/hOvUv4Qb5W7iJO/wKt9Dx6sI+5l5XuI1HL/bHVi+cXqnlQcWhySKTOb+CmV7vkoWt0uqca1vEJlODoF9JU51pW91T7NdD5yIVWZOqCas6SYzKrdnq0AUb5/JRrxeJHoQm5Vhj/rbGAo5xBYUlDowxQhhkiMro6DtVZvSvsUPCXntWPc3ndFsU1P9zhQEC9M9cU7qy0nk6T4E9XxtSdXQrbsuelDSRXs1JErJCXta2VELqATZlV44RelzRiT8oZ0j/AAlabsgAAAB4nG2MwQrCMBBEd2K1NgpCf1DSmCZbyy6hRtu/N6JHH8xheMyQoS+W/mNhsEODPQ5ocUQHixNh7VeWTSVmdqk4udeYnPsbDyUG9irXiUOV0sz6DJclBYmp6MROx8Bn/+lL4rG8Sjezd/yoZ+1vQ/QGMvkkpg==') format('woff'),
url('../iconfont.ttf?t=1511887164151') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+*/
url('../iconfont.svg?t=1511887164151#iconfont') format('svg'); /* iOS 4.1- */
}
.iconfont {
font-family:"iconfont" !important;
font-size: 0.78703704rem;
font-style:normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.icon-xinyongkahuankuan {
color: #00B25E;
}
.icon-qq {
color: #47A6DF;
}
.icon-love {
color: #EC6066;
}
.icon-shenghuojiaofei {
color: #00B25E;
}
.icon-chengshifuwu {
color: #00B25E;
}
.icon-licaitong {
color: #439DD1;
}
.icon-dibugeicon_jieqian {
color: #588DB4;
}
.icon-jieqian {
color: #D5912B;
}
.icon-xinyongkahuankuan:before { content: "\e621"; }
.icon-qq:before { content: "\e61e"; }
.icon-dibugeicon_jieqian:before { content: "\e633"; }
.icon-love:before { content: "\e612"; }
.icon-shenghuojiaofei:before { content: "\e609"; }
.icon-chengshifuwu:before { content: "\e60a"; }
.icon-licaitong:before { content: "\e60b"; }
.icon-jieqian:before { content: "\e60c"; }
效果图:
如果有错误或者不理解的地方,希望可以指出和交流,谢谢!