CSS3網格佈局是讓開發人員設計一個網格並將內容放在這些網格內。而不是使用浮動製做一個網格,其實是你將一個元素聲明爲一個網格容器,並把元素內容置於網格中。css
有一些瀏覽器是不支持網格佈局的,能夠從caniuse這個網站查看各個瀏覽器是否支持這些css3中的新屬性,綠色表示支持,紅色表示不支持,另外一種顏色表示部分支持,如圖(查看各個瀏覽器是否支持grid佈局):html
經過下面這張圖片能夠了解網格佈局的基本概念css3
先來個表情包happy一下吧哈哈哈web
頁面佈局:chrome
<!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)同理;微信
在微信的界面中是有「邊框的」,這裏用了僞元素來實現
代碼以下:app
/* 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屬性來解決這個問題svg
個人全部圖標是從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+VEGTaA4hEAahIOMsOEG9jAch5fApIymTwA0c0S2UySgYBSGpV5t13xBEIQ4y5KDmVJtBGQfQqHfxHqjqWQCrLz2vehmVXISwGeSe603jb0HKdjPx7nBvamgsWc1r0umoqlqq+nlJ4H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I8LSP4H8xkAFtsBrAAAAHicZY9NTsMwEIVf+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"; }
效果圖:
若是有錯誤或者不理解的地方,但願能夠指出和交流,謝謝!