Grid Layout

The CSS Grid Layout Module offers a grid-based layout system, with rows and columns, making it easier to design web pages without having to use floats and positioning.

Grid Elements

A grid layout consists of a parent element, with one or more child elements.

<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
background-color: #2196F3;
padding: 10px;
}
.grid-item {
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid rgba(0, 0, 0, 0.8);
padding: 20px;
font-size: 30px;
text-align: center;
}
</style>
</head>
<body> <h1>Grid Elements</h1> <p>A Grid Layout must have a parent element with the <em>display</em> property set to <em>grid</em> or <em>inline-grid</em>.</p> <p>Direct child element(s) of the grid container automatically becomes grid items.</p> <div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
<div class="grid-item">7</div>
<div class="grid-item">8</div>
<div class="grid-item">9</div>
</div> </body>
</html>

Display Property

An HTML element becomes a grid container by setting the display property to grid or inline-grid.

.grid-container {
display: grid;
}

All direct children of the grid container automatically become grid items.

Grid Columns

The vertical line of grid items are called columns.

Grid Rows

The horizontal line of grid items are called rows.

Grid Gaps

The space between each column/row are called gaps.

You can adjust the gap size by using one of the following properties:

grid-column-gap
grid-row-gap
grid-gap

The grid-gap property is a shorthand property for the grid-column-gap and the grid-row-gapproperties:

.grid-container {
display: grid;
grid-gap: 50px 100px;
}

Grid Lines

The line between columns are called column lines.

The line between rows are called row lines.

Place a grid item at column line 1, and let it end on column line 3

<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
} .grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
} .item1 {
grid-column-start: 1;
grid-column-end: 3;
}
</style>
</head>
<body> <h1>Grid Lines</h1> <div class="grid-container">
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
<div class="item6">6</div>
<div class="item7">7</div>
<div class="item8">8</div>
</div> <p>You can refer to line numbers when placing grid items.</p> </body>
</html>

Grid Container

To make an HTML element behave as a grid container, you have to set the display property to grid or inline-grid.

Grid containers consist of grid items, placed inside columns and rows

The grid-template-columns Property

The grid-template-columns property defines the number of columns in your grid layout, and it can define the width of each column.

The value is a space-separated-list, where each value defines the length of the respective column.

If you want your grid layout to contain 4 columns, specify the width of the 4 columns, or "auto" if all columns should have the same width.

.grid-container {
display: grid;
grid-template-columns: auto auto auto auto;
}

The grid-template-columns property can also be used to specify the size (width) of the columns.

.grid-container {
display: grid;
grid-template-columns: 80px 200px auto 40px;
}

The grid-template-rows Property

The grid-template-rows property defines the height of each row.

.grid-container {
display: grid;
grid-template-rows: 80px 200px;
}

The justify-content Property

The justify-content property is used to align the whole grid inside the container

.grid-container {
display: grid;
justify-content: space-evenly;
justify-content: space-around;
justify-content: space-between;
justify-content: center;
justify-content: end;
}

The align-content Property

The align-content property is used to vertically align the whole grid inside the container.

.grid-container {
display: grid;
align-content: space-evenly;
align-content: space-around;
align-content: space-between;
align-content: center;
align-content: start;
align-content: end;
}

Child Elements (Items)

A grid container contains grid items.

By default, a container has one grid item for each column, in each row, but you can style the grid items so that they will span multiple columns and/or rows.


The grid-column Property:

The grid-column property defines on which column(s) to place an item.

You define where the item will start, and where the item will end.

Note: The grid-column property is a shorthand property for the grid-column-start and the grid-column-end properties.

Make "item1" start on line 1 and end on line 5:

.item1 {
grid-column: 1 / 5;
}

Make "item1" start on column 1 and span 3 columns:

.item1 {
grid-column: 1 / span 3;
}

Make "item2" start on column 2 and span 3 columns:

.item2 {
grid-column: 2 / span 3;
}

The grid-row Property: the same grid-column

The grid-area Property

The grid-area property can be used as a shorthand property for the grid-row-start, grid-column-start, grid-row-end and the grid-column-end properties.

Make "item8" start on row-line 1 and column-line 2, and end on row-line 5 and column line 6:

<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: auto auto auto auto auto auto;
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
} .grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
} .item8 {
grid-area: 1 / 2 / 5 / 6;
}
</style>
</head>
<body> <h1>The grid-area Property</h1> <p>You can use the <em>grid-area</em> property to specify where to place an item.</p> <p>The syntax is:</p>
<p>grid-row-start / grid-column-start / grid-row-end / grid-column-end.</p> <p>Item8 will start on row-line 1 and column-line 2, and end on row-line 5 column-line 6:</p> <div class="grid-container">
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
<div class="item6">6</div>
<div class="item7">7</div>
<div class="item8">8</div>
<div class="item9">9</div>
<div class="item10">10</div>
<div class="item11">11</div>
<div class="item12">12</div>
<div class="item13">13</div>
<div class="item14">14</div>
<div class="item15">15</div>
</div> </body>
</html>

Make "item8" start on row-line 2 and column-line 1, and span 2 rows and 3 columns:

.item8 {
grid-area: 2 / 1 / span 2 / span 3;
}

Naming Grid Items

The grid-area property can also be used to assign names to grid items.

Named grid items can be referred to by the grid-template-areas property of the grid container.

Item1 gets the name "myArea" and spans all five columns in a five columns grid layout:
<!DOCTYPE html>
<html>
<head>
<style>
.item1 {
grid-area: myArea;
} .grid-container {
display: grid;
grid-template-areas: 'myArea myArea myArea myArea myArea';
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
} .grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body> <h1>The grid-area Property</h1> <p>You can use the <em>grid-area</em> property to name grid items.</p> <p>You can refer to the name when you set up the grid layout, by using the <em>grid-template-areas</em> property on the grid container.</p> <p>Item1, is called "myArea" and will take up the place of all five columns:</p> <div class="grid-container">
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
<div class="item6">6</div>
</div> </body>
</html>

To define two rows, define the column of the second row inside another set of apostrophes:

<!DOCTYPE html>
<html>
<head>
<style>
.item1 {
grid-area: myArea;
} .grid-container {
display: grid;
grid-template-areas: 'myArea myArea . . .' 'myArea myArea . . .';
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
} .grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body> <h1>The grid-area Property</h1> <p>You can use the <em>grid-area</em> property to name grid items.</p> <p>You can refer to the name when you set up the grid layout, by using the <em>grid-template-areas</em> property on the grid container.</p> <p>Item1, is called "myArea" and will take up the place of two columns (out of five), and will span two rows:</p> <div class="grid-container">
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
<div class="item6">6</div>
<div class="item7">7</div>
</div> </body>
</html>
.item1 { grid-area: header; }
.item2 { grid-area: menu; }
.item3 { grid-area: main; }
.item4 { grid-area: right; }
.item5 { grid-area: footer; } .grid-container {
grid-template-areas:
'header header header header header header'
'menu main main main right right'
'menu footer footer footer footer footer';
}

CSS: Grid Layout Module的更多相关文章

  1. [CSS] Get up and running with CSS Grid Layout

    We’ll discuss the display values pertinent to CSS Grid Layout – grid, inline-grid, and subgrid. Then ...

  2. CSS Grid Layout In Action

    CSS Grid Layout In Action CSS 异形网格布局实战 refs https://static-nginx-online.fbcontent.cn/tutor/tutor-ybc ...

  3. css grid layout in practice

    css grid layout in practice https://caniuse.com/#search=grid subgrid https://caniuse.com/#search=cal ...

  4. 各个浏览器开启CSS Grid Layout的方式

    2017年3月,Chrome.Firefox将开启默认支持. 当然对于很多人等不及浏览器默认支持,想提前体验一把,这里提供一些打开方式: 1.Chrome 在浏览器中输入:chrome://flags ...

  5. CSS Grid layout布局

    CSS Grid布局 (又名"网格"),是一个基于二维网格布局的系统,主要目的是改变我们基于网格设计的用户接口方式.你只需要定义一个容器元素并设置display:grid,使用gr ...

  6. CSS grid layout

      CSS网格布局用于将页面分割成数个主要区域,或者用来定义组件内部元素间大小.位置和图层之间的关系. 像表格一样,网格布局让我们能够按行或列来对齐元素. 但是,使用CSS网格可能还是比CSS表格更容 ...

  7. CSS grid layout demo 网格布局实例

    直接 上代码,里面我加注释,相当的简单, 也可以去我的github上直接下载代码,顺手给个星:https://github.com/yurizhang/micro-finance-admin-syst ...

  8. 关于CSS Grid Layout的代码解释

    .wrapper { display: grid; /*生成grid类型块级网格*/ grid-template-columns: repeat(3, 1fr); /*设置显示的列网格线,且重复3次1 ...

  9. CSS ? Layout Module : CSS 布局模型

    1 1 1 https://www.w3.org/TR/css-grid-1/ CSS Grid Layout Module Level 1 W3C Working Draft, 19 May 201 ...

随机推荐

  1. springCloud4---feign

    JAX-WS : soap是遵循这个标准. JAX-RS : 标准,jersey框架遵循这个标准. Feign是一个声明式的web service客户端.Feign使用的负载均衡也是ribbon,Fe ...

  2. 微服务—分布式服务追踪sleuth和zipkin

    随着业务的发展,系统规模也会越来越大,各微服务间的调用关系也越来越错综复杂. 通常一个客户端发起的请求在后端系统中会经过多个不同的微服务调用来协同产生最后的请求结果, 在复杂的微服务架构系统中,几乎每 ...

  3. java中hashSet原理

    转自: http://blog.csdn.net/guoweimelon/article/details/50804799 HashSet是JavaMap类型的集合类中最常使用的,本文基于Java1. ...

  4. PHP(Mysql/Redis)消息队列的介绍及应用场景案例

    在进行网站设计的时候,有时候会遇到给用户大量发送短信,或者订单系统有大量的日志需要记录,还有做秒杀设计的时候,服务器无法承受这种瞬间的压力,无法正常处理,咱们怎么才能保证系统正常有效的运行呢?这时候我 ...

  5. 分享个基于 Node.js + React 的博客系统

    是使用 ES2015+ 特性写的,使用了 ThinkJS 框架,后台使用了 React. 完全使用 Markdown 来写文章,还可以把文章推送到团队博客系统中(团队博客也需要使用该系统). 项目地址 ...

  6. 《JAVA程序设计》第五次实验报告

    20145333 实验五 Java网络编程及安全 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验日期:2016.05.06 18:30-21:3 ...

  7. MR案例:MR和Hive中使用Lzo压缩

    在MapReduce中使用lzo压缩 1).首先将数据文件在本地使用lzop命令压缩.具体配置过详见配置hadoop集群的lzo压缩 //压缩lzop,解压缩lzop -d [root@ncst wo ...

  8. HDFS的回收站 && 安全模式

    回收站机制 1). HDFS 的回收站机制由 core-site.xml 中 fs.trash.interval 属性(以分钟为单位)设置,默认是 0,表示未启用.注意:配置数值应该为1440,而配置 ...

  9. maven项目引入本地包,不使用中央仓库

    1. dependendy引入 <dependency> <groupId>com.taobao</groupId> <artifactId>taoba ...

  10. JSON and XML Serialization in ASP.NET Web API

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/json-and-xml-seri ...