「译」CSS 网格要素 Grid Essentials

Introduction to Grids

Flexbox常用于一维排版(one-dimensional layout),而CSS grid更多用于二维排版(two-dimensional layou),且其提供了很多工具在行与列之间对其或移动elements。

Creating a Grid

要搭建一个grid,你同时需要 a grid container(a parent element) and grid items(children)。

要想使HTML元素变为a grid container,you must set the element’s display property to grid (for a block-level grid) or inline-grid (for an inline grid). Then, you can assign other properties to lay out the grid.

Creating Columns

默认情况下,grids只有one column。可以使用grid-template-columns属性来定义columns

上例中该属性产生了2个变化:其一,它定义了columns的个数,在上例中——是3个;其二,它定义了每个column的width,分别是20px、40px、60px(没错,你可以将这两种单位混合使用,即使相加后会产生overflow)。

Creating Rows

同样地,grid-template-rows属性几乎与grid-template-columns属性一模一样(identical),它定义了rows的个数和height。

Grid Template

grid-template 属性可以完全替代上述两个属性。

slash(斜杠)前面表示rows的个数和height,后面表示column的个数和width。

Fraction

在CSS grids 中,专门定制了一种单位——fr(分数)。通过它,你可以轻松防止items超出网格边界。

css .grid { display: grid; width: 1000px; height: 400px; grid-template: 2fr 1fr 1fr / 1fr 3fr 1fr; }

上例表示网格有3个rows(200px,100px,100px),3个columns(200px,600px,200px)

fr也可以和其他units(单位)混用:

css .grid { display: grid; width: 100px; grid-template-columns: 1fr 60px 1fr; }

上例中,已有60px被第二个column占据,因此剩余两栏共用40px,故分别为:20px,60px,20px。

Repeat

上述三个定义rows和columns的属性的值也可以用repeat表示。

Repeat尤其常用于fr。如repeat(5,1fr),会将等分为5栏或5列。

minmax

目前为止,网格都是固定尺寸的(有明确的height and width)。但是有时你可能需要网格根据浏览器尺寸重新调整大小( to resize based on the size of your web browser).

在这种情况下,你可能需要防止a row or column from getting too big or too small. 举例来说,如果你有一张100px宽的图片,你可能不想让column get thinner than 100 pixels! 此时minmax()功能可以解决这个问题。(使用它时,需要删掉grid的width属性)。

Grid Gap

目前我们的网格,items之间没有任何空隙。两个CSS属性grid-row-gap and grid-column-gap 可以在栏或列间插入空白。

css .grid { display: grid; width: 320px; grid-template-columns: repeat(3, 1fr); grid-column-gap: 10px; }

需要知道的是,grid-gap并未在the beginning or end of the grid 处添加空白。上例中,我们的grid会有3 columns,其中间会有10px的gap。

让我们来计算一下每个column的宽度。记住,fr 只以所有available space为基准。网格320px宽,且其中20px被2个 grid gap占据。因此available space是300px,故每个column为100px宽。

最后,grid-gap也可以同时设定 row and column gap 。

grid-gap: 20px 10px;

The distance between rows to 20 pixels and the distance between columns to 10 pixels. ( 中间无/隔开)。如果只有一个值需要设定,那么还得用grid-row-gap or grid-column-gap 。

Grid Items

当我们定义一个网格时,我们必须规定 the quantity of rows and columns and their respective sizes.

上面所有的例子中,被放置网格中的items总是占据exactly one square。但情况并非总是如此,通过making grid items take up more than one row and one column. ,我们可以极大程度上改变gird的l外观。如图, Items A, B, C, and E span(涵盖) more than one row!

Multiple Row Items

使用CSS属性——grid-row-start and grid-row-end,我们可以使 single grid items take up multiple rows. 记住,此时我们不是给父级元素(the outer grid container)应用CSS样式,而是给grid

里面的elements 应用CSS。

上例中,the HTML element of class item will take up two rows in the grid, rows 1 and 2. The values that grid-row-start and grid-row-end accept are grid lines.

Row grid lines and column grid lines start at 1 and end at a value that is 1 greater than the number of rows or columns the grid has. For example, if a grid has 5 rows, the grid row lines range from 1 to 6. If a grid has 8 columns, the grid row lines range from 1 to 9.

The value for grid-row-start should be the row at which you want the grid item to begin. The value for grid-row-end should be one greater than the row at which you want the grid item to end. An element that covers rows 2, 3, and 4 should have these declarations: grid-row-start: 2 and grid-row-end: 5.

Grid Row

也可以使用grid-row属性来代替grid-row-start and grid-row-end。下图二者等效:

当使用这些属性使一个item占据 multiple rows or columns 时,它也会包括 grid-gap的空间(如果存在的话)。例如, if an item spans two rows of height 100 pixels and there is a ten-pixel grid-gap, then the item will have a total height of 210 pixels.

Span

The previous three properties also exist for columns. grid-column-start, grid-column-end and grid-column work identically to the row properties. These properties allow a grid item to span multiple columns.

当使用上述6个属性时,可以使用关键词——span,来start or end a column or row relative to its other end(相对于其另一端)。

上面的代码说明了,该item element 始于 column 4 且占据了2 columns 的空间,故其会占据column4和5。它与下图的代码等效。

span是个有用的关键词,因其避免了 the ending grid line 的计算错误。如果你知道你想从哪开始,以及他应该span多少的时,就用span吧!

Grid Area

我们已经能够用 grid-row and grid-column as shorthand(简化) for properties like grid-row-start and grid-row-end.

我们可以进一步用 grid-area 属性来简化代码。该属性同时可以设置rows和columns是起止位置。

grid-area 有四个被斜杠分开的值,分别代表:

1.grid-row-start

2.grid-column-start

3.grid-row-end

4.grid-column-end

上例中,the item 将会占据rows2到3,columns3到8。如果你已知晓items要在网格中的位置,grid-area是个好方法来摆放它们。

Review

grid-template-columns defines the number and sizes of the columns of the grid

grid-template-rows defines the number and sizes of the rows of the grid

grid-template is a shorthand for defining both grid-template-columns and grid-template-rows in one line


grid-gap puts blank space between rows and/or columns of the grid


grid-row-start and grid-row-end makes elements span certain rows of the grid

grid-column-start and grid-column-end makes elements span certain columns of the grid

grid-area is a shorthand for grid-row-start, grid-column-start, grid-row-end, and grid-column-end, all in one line

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • 网格线(Grid Line) 构成网格结构的分界线。它们既可以是垂直的(“列网格线(column grid lin...
    晚溪呀阅读 1,019评论 0 0
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,391评论 0 13
  • 简介 CSS Grid布局 (又名"网格"),是一个基于二维网格布局的系统,旨在改变我们基于网格设计的用户界面方式...
    咕咚咚bells阅读 2,424评论 0 4
  • 相信一提到渣男,大家都会有两种观点: 第一种:渣男对待感情不专一,花言巧语,不负责,爱劈腿,骗钱骗炮; 第二种:渣...
    阿予曦阅读 1,442评论 5 6
  • 时光回不到最初 回忆一天天错过
    鄧虢阅读 233评论 0 0