CSS3网格基础:什么是网格布局

html-css018

CSS3网格基础:什么是网格布局,第1张

为什么我们需要网格布局?

在我们的Web内容中,可以将其分割成很多个内容块,而这些内容块都占据自己的区域(regions),可以将这些区域想像成是一个虚拟的网格。到目前为止,在一个模板中使用不同的结构标签,使用多个浮动和手动计算实现一个布局。这对于Web前端人员来说,这是一件痛苦之事。而网格布局将让你摆脱这样的困局,让你的布局方法变得非常简单与清晰。

网格布局给了我们一种方法——创建的结构与使用表格(table)布局并没有什么不同。然而,他是在CSS中实现而不是在HTML中实现,同时还可以依赖于媒体查询根据不同的上下文得新定义布局。这对于喜欢响应式设计的同学来说是一个绝对利好的消息。因为你不在需要再担心你的HTML结构而影响你的布局。

网格布局还可以让我们摆脱现在布局中存在的文档流限制,换句话说,你的结构不需要根据设计稿从上往上布置了。这也意味着您可以自由地更改页面元素位置。这最适合你在不同的断点位置实现你最需要的布局,而不再需要为响应你的设计而担心HTML结构的问题。

什么是CSS Grid Layout?

CSS Grid Layout是CSS为布局新增的一个模块。网格布局特性主要是针对于Web应用程序的开发者。可以用这个模块实现许多不同的布局。网络布局可以将应用程序分割成不同的空间,或者定义他们的大小、位置以及层级。

就像表格一样,网格布局可以让Web设计师根据元素按列或行对齐排列,但他和表格不同,网格布局没有内容结构,从而使各种布局不可能与表格一样。例如,一个网格布局中的子元素都可以定位自己的位置,这样他们可以重叠和类似元素定位。

此外,没有内容结构的网格布局有助于使用流体、调整顺序等技术管理或更改布局。通过结合CSS的媒体查询属性,可以控制网格布局容器和他们的子元素,使用页面的布局根据不同的设备和可用空间调整元素的显示风格与定位,而不需要去改变文档结构的本质内容。

浏览器兼容性

众观下来,仅在IE10+上支持,而且也仅支持部分属性。或许很多同学看到这样的兼容性,又会止步,或者呵呵两声闪人了。不过我们学习是无防,在各浏览器中通过设置可以做测试效果,在下一次中将会向大家介绍如何在浏览器中开启此功能。

Grid vs Flexbox

在www-list邮件中常常会看到一种Pk的现像:谈到网格就会扯到Flexbox。假设浏览器都支持这两个模块,你将选择grid还是flexbox来给页面布局。其实当你了解两者之后你会很轻松的做出选择。flexbox是一维布局,他只能在一条直线上放置你的内容区块;而grid是一个二维布局。前面也简单说到,你可以根据你的设计需求,将内容区块放置到任何你想要放的地方。那么不用多说,你应该知道哪一种更适合你的布局。

网格术语

想要玩转CSS Grid Layout,就需要先了解网格给我们的一些新术语以及一些CSS属性和值。在这一节中,将简单的向大家解释网格布局中的一些概念与术语。

网格线(Grid Lines)

网格线组成了网格,他是网格的水平和垂直的分界线。一个网格线存在行或列的两侧。我们可以引用它的数目或者定义的网格线名称。

上图突出显示的红线就是第二列的网格线(line2)。

网格轨道(Grid Track)

网格轨道是就是相邻两条网格线之间的空间,就好比表格中行或列。所在在网格中其分为grid column和grid row。每个网格轨道可以设置一个大小,用来控制宽度或高度。

图中突出显示的就是行线line2和line3之间组成的网格轨道。

网格单元格(Grid Cell)

网格单元格是指四条网格线之间的空间。所以它是最小的单位,就像表格中的单元格。

图中突出显示的单元格是由行线line2、line3和列表line2、line3组成的。

网格区域(Grid Area)

网格区域是由任意四条网格线组成的空间,所以他可能包含一个或多个单元格。相当于表格中的合并单元格之后的区域。

图中突出显示的网格区域是行线line1、line3和列线line2、line4之间的区域,其主要包括了四个网格单元格。

网格容器(Grid Containers)

通过使用display属性给元素显式设置了属性值grid或inline-grid,此时这个元素将自动变成网格容器。这个类似于flexbox一样,将元素设置设置为display:flex,元素将自动变成弹性盒模型。

由于网格容器不是块容器,所以有部分属性在网格布局中将会失效:

多列布局模块中的所有column-*属性运用在网格容器上将失效

float和clear使用在网格项目(网格单元格Grid Cell)上将失效

vertical-align使用在网格单元格上将失效

::first-line和::first-letter这样的伪元素不能应用在网格容器上

网格单元格顺序(order)

网格单元格顺序和Flexbox模块一样,通过order属性来对网格单父元格进行顺序重排。

下面是 CSS3 新增的 CSS3 属性列表。 

animation (and eight associated longhand properties)

background-clip

background-origin

background-size

border-radius (and four associated longhand properties)

border-image (and six associated longhand properties)

box-decoration-break

box-shadow

box-sizing

columns (and thirteen associated multi-column properties)

clear-after

flex (and eleven associated flexbox properties)

font-stretch

font-size-adjust

font-synthesis

font-kerning

font-variant-caps

hanging-punctuation

hyphens

icon

image-resolution

image-orientation

line-break

object-fit

object-position

opacity

outline-offset

overflow-wrap / word-wrap

backface-visibility

perspective

perspective-origin

pointer-events (for HTML)

resize

tab-size

text-align-last

text-decoration-line

text-decoration-skip

text-decoration-position

text-decoration-style

text-emphasis (and three associated properties)

text-justify

text-orientation

text-overflow

transform

transform-style

text-shadow

transition (and four associated longhand properties)

word-break

word-spacing

writing-mode

在 CSS3 中为 CSS2.1 新增的取值,下面是每个属性新值的列表。

Value “local” for the background-attachment property

Value "rgba()" for any property that accepts a color value

Value "hsl()" for any property that accepts a color value

Value "hsla()" for any property that accepts a color value

Value "currentColor" for any property that accepts a color value

Value "inset()" for the clip property

Value "linear-gradient()" for any property that accepts an image value

Value "radial-gradient()" for any property that accepts an image value

Value "repeating-linear-gradient()" for any property that accepts an image value

Value "repeating-radial-gradient()" for any property that accepts an image value

Value "image()" for any property that accepts an image value

Multiple comma-separated images for any property that accepts an image value

Multiple comma-separated background-related values to match multiple background image declarations

Value "center" for the position property

Value "page" for the position property

Value "space" for the background-repeat property

Value "round" for the background-repeat property

15 new values for the cursor property

Values "flex" and "inline-flex" for the display property

Values "all-small-caps", "petite-caps", "all-petite-caps", "titling-caps", and "unicase" for the font-variant property

Multiple, space-separated values for the letter-spacing property

New values for the text-align property, including "<string>", "match-parent", "start", "end", and "start end"

text-decoration is now a shorthand property

Keywords "hanging" and "each-line" declared along with length or percentage values for the tfext-indent property

Value "full-width" for the text-transform property

rem units for lengths

calc() units for lengths

toggle() units

Angle units (deg, grad, rad, turn)

Time units (s, ms)

下面是 CSS3 中新增的选择器。

Substring matching attribute selectors ([att^=val], [att$=val], [att*=val])

:target pseudo-class

New pseudo-classes: :enabled, :disabled, :checked, and :indeterminate

:root pseudo-class

New expression-based structural pseudo-classes: :nth-child(), :nth-last-child(), :nth-of-type(), :nth-last-of-type()

Other new structural pseudo-classes: :last-child, :first-of-type, :last-of-type, :only-child, :only-of-type, :empty

The negation pseudo-class :not()

Four pseudo-elements with double-colon syntax (::first-line, ::first-letter,::before, ::after)

The following-sibling combinator (p ~ img)

::selection pseudo-class (removed from the spec, but everyone uses it)

其它 CSS2.1 没有包含的特性。

@font-face

Media Queries

Keyframe animations using @keyframes

Conditional styles using @supports

Namespacing using @namespace

Regions

Filters

还有部分仍在变化中的。。。

www.cnblogs.com/lhb25/archive/2013/02/21/differences-between-css2-and-css3.html