本文译自【http://j4n.co/blog/Creating-your-own-css-grid-system】,英语好的,可直接查看原网页,不需要翻墙。

翻译拿不准的地方会有英文原文,方便大家理解。

一般的网格布局如下

可以看出主要有以下几个部分

  • a container(容器)
  • rows(行)
  • columns(列)
  • gutters (the space between columns)(列边距)

容器

容器的目的就是设置整个网格的宽度,通常设置为100%,但可能要给大屏显示器设置一个最大宽度。

.grid-container {
width : 100%;
max-width : 1200px;
}

行是为了将列放在里面,并防止其溢出到其他行中。为了实现这个目的,我们采用清楚浮动的hack技术来确保所有的内容都在行中。

  /*-- our cleafix hack -- */
.row:before,
.row:after {
content:"";
display: table ;
clear:both;
}

毫无疑问,列是网格布局中最复杂的一部分。首先,在CSS当中,有好几种不同的列的定位方式,而且还要考虑不同的宽度,比如响应式设计。在这里我们将列设定位置,并给出宽度。下一部分再讲响应式设计。

列定位

浮动,行内块,块级表格(display-table),flex,有各种各样的方法。从我个人经验出发,最不容易出错和使用最广泛的就是float了。如果列是空的,将会漂浮在其他元素上面,为了防止这个,我们可以设定一个最小的宽度。

 [class*='col-'] {
float: left;
min-height: 1px;
}

列宽

为了确定一列的大小,我们可用容器的宽度来除以总的列数。在这里,容器的宽度是100%,假设是6列,那么一列就是100%/6=16.66%。

 [class*='col-'] {
float: left;
min-height: 1px;
width: 16.66%;
}

当然,这只是开始,如果我们想要其他的宽度,可以这样设置:

.col-1{
width: 16.66%;
}
.col-2{
width: 33.33%;
}
.col-3{
width: 50%;
}
.col-4{
width: 66.664%;
}
.col-5{
width: 83.33%;
}
.col-6{
width: 100%;
}

我们唯一要注意的就是,不管你最终多少列,各列加起来不能超过容器的宽度。

列边距

在'border-box' box-sizing模型出现之前,给一个百分比元素一个不可改变的宽度是一件很痛苦的事。幸运的是,用'border-box' box-sizing可以轻易的做到。

Before the 'border-box' box-sizing model, giving percentage-width elements a static padding was a real pain. Luckily, using the 'border-box' model, we can create gutters with ease.

/*-- setting border box on all elements inside the grid --*/
.grid-container *{
box-sizing: border-box;
} [class*='col-'] {
float: left;
min-height: 1px;
width: 16.66%;
/*-- our gutter --*/
padding: 12px;
}

(Personally, I use * {box-sizing: border-box;} in my CSS to apply border-box to everything on the page).

(这句不好翻译,主要是还不理解box-sizing)

基本网格布局如下:

<div class="grid-container outline">
<div class="row">
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
</div>
<div class="row">
<div class="col-2"><p>col-2</p></div>
<div class="col-2"><p>col-2</p></div>
<div class="col-2"><p>col-2</p></div>
</div>
<div class="row">
<div class="col-3"><p>col-3</p></div>
<div class="col-3"><p>col-3</p></div>
</div>
</div>

CSS

.grid-container{
width: 100%;
max-width: 1200px;
} /*-- our cleafix hack -- */
.row:before,
.row:after {
content:"";
display: table ;
clear:both;
} [class*='col-'] {
float: left;
min-height: 1px;
width: 16.66%;
/*-- our gutter -- */
padding: 12px;
background-color: #FFDCDC;
} .col-1{ width: 16.66%; }
.col-2{ width: 33.33%; }
.col-3{ width: 50%; }
.col-4{ width: 66.66%; }
.col-5{ width: 83.33%; }
.col-6{ width: 100%; } .outline, .outline *{
outline: 1px solid #F6A1A1;
} /*-- some extra column content styling --*/
[class*='col-'] > p {
background-color: #FFC2C2;
padding:;
margin:;
text-align: center;
color: white;
}

HTML

    <div class="grid-container outline">
<div class="row">
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
</div>
<div class="row">
<div class="col-2"><p>col-2</p></div>
<div class="col-2"><p>col-2</p></div>
<div class="col-2"><p>col-2</p></div>
</div>
<div class="row">
<div class="col-3"><p>col-3</p></div>
<div class="col-3"><p>col-3</p></div>
</div>
</div> <hr/>

制作网格布局

针对手机,调整我们的网格布局非常简单,只需要调整列的宽度就够了。为简单起见,在屏幕小于800px时,将宽度增大一倍。要注意的就是当.col-2 , .col-1,col-5在一行的时候,这时候我们将.col-2 , .col-1占满整行。

The only thing to watch out for is a few exceptions where the last column in the row hangs off the end. Such as in the case of the .col-2 columns and the.col-1 beside the .col-5 column. 
To counter this, we'll make the last .col-2 and .col-1 in the row 100% wide.

@media all and (max-width:800px){
.col-1{ width: 33.33%; }
.col-2{ width: 50%; }
.col-3{ width: 83.33%; }
.col-4{ width: 100%; }
.col-5{ width: 100%; }
.col-6{ width: 100%; } .row .col-2:last-of-type{
width: 100%;
} .row .col-5 ~ .col-1{
width: 100%;
}
}

如果屏幕更小,我们继续调整。

 @media all and (max-width:650px){
.col-1{ width: 50%; }
.col-2{ width: 100%; }
.col-3{ width: 100%; }
.col-4{ width: 100%; }
.col-5{ width: 100%; }
.col-6{ width: 100%; }
}

到这里,我们就创建害了我们的响应式布局:

<div class="grid-container outline">
<div class="row">
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
</div>
<div class="row">
<div class="col-2"><p>col-2</p></div>
<div class="col-2"><p>col-2</p></div>
<div class="col-2"><p>col-2</p></div>
</div>
<div class="row">
<div class="col-3"><p>col-3</p></div>
<div class="col-3"><p>col-3</p></div>
</div>
<div class="row">
<div class="col-4"><p>col-4</p></div>
<div class="col-2"><p>col-2</p></div>
</div>
<div class="row">
<div class="col-5"><p>col-5</p></div>
<div class="col-1"><p>col-1</p></div>
</div>
<div class="row">
<div class="col-6"><p>col-6</p></div>
</div>
</div>

要提醒的是这只是创建网格布局的开始,这还算不上是一个框架或完美的解决方法,但至少让我们觉得采用CSS创建网格布局的过程并不神秘。

不用bootstrap,只用CSS创建网格布局的更多相关文章

  1. CSS Grid 网格布局全解析

    介绍 CSS Grid(网格) 布局使我们能够比以往任何时候都可以更灵活构建和控制自定义网格. Grid(网格) 布局使我们能够将网页分成具有简单属性的行和列.它还能使我们在不改变任何HTML的情况下 ...

  2. CSS Grid网格布局全攻略

    CSS Grid网格布局全攻略 所有奇技淫巧都只在方寸之间. 几乎从我们踏入前端开发这个领域开始,就不停地接触不同的布局技术.从常见的浮动到表格布局,再到如今大行其道的flex布局,css布局技术一直 ...

  3. 转:Bootstrap研究 精巧的网格布局系统

    本网格布局系统属于Scaffolding(框架,布局)部分.在Scaffolding里面有(固定)网格布局(Grid System)和流式网格布局(Fluid Grid System).本文讨论第一种 ...

  4. CSS Grid 网格布局教程

    一.概述 网格布局(Grid)是最强大的 CSS 布局方案. 它将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局.以前,只能通过复杂的 CSS 框架达到的效果,现在浏览器内置了. 上 ...

  5. bootstrap学习总结-02 网格布局

    1  网格布局 Bootstrap 提供了一套响应式.移动设备优先的流式栅格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列. <!DOCTYPE html> ...

  6. CSS中网格布局实战(初级)

    大家好,网格布局是我们在网页布局中经常用到的,那这里我就给大家分享一篇简单的网格布局,让大家能简单明了的了解网格布局的基本内容.闲话不多说,直接进入主题! 第一步,基本的框架结构.这里直接一个div来 ...

  7. 轻松上手CSS Grid网格布局

    今天刚好要做一个好多div格子错落组成的布局,不是田字格,不是九宫格,12个格子这样子,看起来有点复杂.关键的是笔者有点懒,要写那么多div和css真是不想下手啊.多看了两眼,这布局不跟网格挺像吗?c ...

  8. Pure CSS 的网格布局(比bootstrap小很多且易扩展的css UI)

    (转自百度经验)http://jingyan.baidu.com/article/48a42057c44fdba9242504dd.html Pure是一个简单.实用的CSS框架,鉴于目前网上对pur ...

  9. css - Grid网格布局

    .wrapper{ display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: 100px 100px 1 ...

随机推荐

  1. js判断是否是数组

    //判断是否是数组function isArray(obj) {     return Object.prototype.toString.call(obj) === '[object Array]' ...

  2. Winform 搭建http服务器

    using System; using System.Collections; using System.IO; using System.Net; using System.Net.Sockets; ...

  3. LINQ to SQL语句大全

    LINQ to SQL语句大全     LINQ to SQL语句(1)之Where 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判 ...

  4. 批处理系列(14) - 路径/时间/字符切分等DEMO操作

    结合本系列文章第一篇,看本文. 本篇熟悉操作文件路径.时间,温习字符切分. 路径 @echo off @REM @Author: xianghongai@gmail.com :GTCONTINUE @ ...

  5. MongoDB复制集成员及状态转换

    此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 复制集(Replica Set)是MongoDB核心组件,相比早期版本采用的主从(Master-Slave) ...

  6. PageAdmin CMS网站建设教程:如何实现信息的定时发布

    PageAdmin Cms发布文章时候有一个上线时间设置和下线时间设置,网站编辑人员可以利用这个功能来实现定时发布,在信息发布界面,如下图: 设置后就会自动加入定时任务中,注意这个功能需要再系统设置& ...

  7. 1002. Find Common Characters

    Given an array A of strings made only from lowercase letters, return a list of all characters that s ...

  8. Linux基本命令学习与使用

    1.chgrp,chown,chmod(-R递归修改文件夹下的文件) chgrp:修改文件属于哪个组 chown:修改文件属于哪个用户 chmod:修改文件权限r=4,w=2,x=1 chmod 4+ ...

  9. Optimizing Your App for Today’s Internet

    这个 session 的主讲人感觉是一个很典型的美国人,年纪也不小. 网络现状 四十亿人在使用因特网,大概占有世界人口的一半.上网人数的增长在减缓. 但是网络仍然在增长.增长点主要在物联网.第三世界国 ...

  10. Java基础梳理(一)

    List和Set比较,各自的子类比较 对比一:Arraylist与LinkedList的比较 1.ArrayList是实现了基于动态数组的数据结构,因为地址连续,一旦数据存储好了,查询操作效率会比较高 ...