# Grid 栅格

# Grid 概述

24:100% 12:50% 12:50% 8:33.33% 8:33.33% 8:33.33% 6:25% 6:25% 6:25% 6:25% 16:66.66% 8:33.33%

我们采用了 24 栅格系统,将区域进行 24 等分,这样可以轻松应对大部分布局问题。使用栅格系统进行网页布局,可以使页面排版美观、舒适。

我们定义了两个概念,行row和列col,具体使用方法如下:

  • 使用row在水平方向创建一行

  • 将一组col插入在row

  • 在每个col中,键入自己的内容

  • 通过设置colspan参数,指定跨越的范围,其范围是 1 到 24

  • 每个row中的col总和应该为 24

注意:非 template/render 模式下,需使用 h-col

# Grid 代码演示

# 基础用法

水平排列的布局。
col必须放在row里面。

col-12
col-12

col-8
col-8
col-8

col-6
col-6
col-6
col-6
<template>
  <div class="demo-row">
    <h-row>
      <h-col span="12">col-12</h-col>
      <h-col span="12">col-12</h-col>
    </h-row>
    <br />
    <h-row>
      <h-col span="8">col-8</h-col>
      <h-col span="8">col-8</h-col>
      <h-col span="8">col-8</h-col>
    </h-row>
    <br />
    <h-row>
      <h-col span="6">col-6</h-col>
      <h-col span="6">col-6</h-col>
      <h-col span="6">col-6</h-col>
      <h-col span="6">col-6</h-col>
    </h-row>
  </div>
</template>
显示代码 复制代码

# 区块间隔

通过给row添加gutter属性,可以给下属的col添加间距,推荐使用(16+8n)px作为栅格使用。

col-6
col-6
col-6
col-6
<template>
  <div class="demo-row gutter">
    <h-row :gutter="16">
      <h-col span="6">
        <div>col-6</div>
      </h-col>
      <h-col span="6">
        <div>col-6</div>
      </h-col>
      <h-col span="6">
        <div>col-6</div>
      </h-col>
      <h-col span="6">
        <div>col-6</div>
      </h-col>
    </h-row>
  </div>
</template>
显示代码 复制代码

# 栅格顺序( Flex )

通过 Flex 布局的order来改变栅格的顺序。

1 | order-4
2 | order-3
3 | order-2
4 | order-1
<template>
  <div>
    <h-row class="demo-row" type="flex">
      <h-col span="6" order="4">1 | order-4</h-col>
      <h-col span="6" order="3">2 | order-3</h-col>
      <h-col span="6" order="2">3 | order-2</h-col>
      <h-col span="6" order="1">4 | order-1</h-col>
    </h-row>
  </div>
</template>
显示代码 复制代码

# 栅格排序

通过设置pushpull来改变栅格的顺序。

col-18 | push-6
col-6 | pull-18
<template>
  <div>
    <h-row class="demo-row">
      <h-col span="18" push="6">col-18 | push-6</h-col>
      <h-col span="6" pull="18">col-6 | pull-18</h-col>
    </h-row>
  </div>
</template>
显示代码 复制代码

# 左右偏移

通过设置offset属性,将列进行左右偏移,偏移栅格数为offset的值。

col-8
col-8 | offset-8

col-6 | offset-8
col-6 | offset-4

col-12 | offset-8
<template>
  <div slot="demo">
    <h-row class="demo-row">
      <h-col span="8">col-8</h-col>
      <h-col span="8" offset="8">col-8 | offset-8</h-col>
    </h-row>
    <br />
    <h-row class="demo-row">
      <h-col span="6" offset="8">col-6 | offset-8</h-col>
      <h-col span="6" offset="4">col-6 | offset-4</h-col>
    </h-row>
    <br />
    <h-row class="demo-row">
      <h-col span="12" offset="8">col-12 | offset-8</h-col>
    </h-row>
  </div>
</template>

显示代码 复制代码

# Flex 布局

通过给row设置参数justify为不同的值,来定义子元素的排布方式。在flex模式下有效。

子元素向左排列

col-4
col-4
col-4
col-4

子元素向右排列

col-4
col-4
col-4
col-4

子元素居中排列

col-4
col-4
col-4
col-4

子元素等宽排列

col-4
col-4
col-4
col-4

子元素分散排列

col-4
col-4
col-4
col-4
<template>
  <div>
    <p>子元素向左排列</p>
    <h-row type="flex" justify="start" class="demo-row code-row-bg">
      <h-col span="4">col-4</h-col>
      <h-col span="4">col-4</h-col>
      <h-col span="4">col-4</h-col>
      <h-col span="4">col-4</h-col>
    </h-row>
    <p>子元素向右排列</p>
    <h-row type="flex" justify="end" class="demo-row code-row-bg">
      <h-col span="4">col-4</h-col>
      <h-col span="4">col-4</h-col>
      <h-col span="4">col-4</h-col>
      <h-col span="4">col-4</h-col>
    </h-row>
    <p>子元素居中排列</p>
    <h-row type="flex" justify="center" class="demo-row code-row-bg">
      <h-col span="4">col-4</h-col>
      <h-col span="4">col-4</h-col>
      <h-col span="4">col-4</h-col>
      <h-col span="4">col-4</h-col>
    </h-row>
    <p>子元素等宽排列</p>
    <h-row type="flex" justify="space-between" class="demo-row code-row-bg">
      <h-col span="4">col-4</h-col>
      <h-col span="4">col-4</h-col>
      <h-col span="4">col-4</h-col>
      <h-col span="4">col-4</h-col>
    </h-row>
    <p>子元素分散排列</p>
    <h-row type="flex" justify="space-around" class="demo-row code-row-bg">
      <h-col span="4">col-4</h-col>
      <h-col span="4">col-4</h-col>
      <h-col span="4">col-4</h-col>
      <h-col span="4">col-4</h-col>
    </h-row>
  </div>
</template>
显示代码 复制代码

# Flex 对齐

通过给row设置参数align为不同的值,来定义子元素在垂直方向上的排布方式。在flex模式下有效。

顶部对齐

col-4

col-4

col-4

col-4

底部对齐

col-4

col-4

col-4

col-4

居中对齐

col-4

col-4

col-4

col-4

<template>
  <div class="demo-row">
    <p>顶部对齐</p>
    <h-row type="flex" justify="center" align="top" class="code-row-bg">
      <h-col span="4"><p style="height: 80px;">col-4</p></h-col>
      <h-col span="4"><p style="height: 30px;">col-4</p></h-col>
      <h-col span="4"><p style="height: 100px;">col-4</p></h-col>
      <h-col span="4"><p style="height: 60px;">col-4</p></h-col>
    </h-row>
    <p>底部对齐</p>
    <h-row type="flex" justify="center" align="bottom" class="code-row-bg">
      <h-col span="4"><p style="height: 80px;">col-4</p></h-col>
      <h-col span="4"><p style="height: 30px;">col-4</p></h-col>
      <h-col span="4"><p style="height: 100px;">col-4</p></h-col>
      <h-col span="4"><p style="height: 60px;">col-4</p></h-col>
    </h-row>
    <p>居中对齐</p>
    <h-row type="flex" justify="center" align="middle" class="code-row-bg">
      <h-col span="4"><p style="height: 80px;">col-4</p></h-col>
      <h-col span="4"><p style="height: 30px;">col-4</p></h-col>
      <h-col span="4"><p style="height: 100px;">col-4</p></h-col>
      <h-col span="4"><p style="height: 60px;">col-4</p></h-col>
    </h-row>
  </div>
</template>
显示代码 复制代码

# 响应式布局

参照 Bootstrap 的响应式设计 (opens new window),预设四个响应尺寸:xs sm md lg,详见 API。
调整浏览器尺寸来查看效果。

Col
Col
Col
<template>
  <div class="demo-row">
    <h-row>
      <h-col :xs="2" :sm="4" :md="6" :lg="8">Col</h-col>
      <h-col :xs="20" :sm="16" :md="12" :lg="8">Col</h-col>
      <h-col :xs="2" :sm="4" :md="6" :lg="8">Col</h-col>
    </h-row>
  </div>
</template>
显示代码 复制代码

# 其它属性的响应式

spanpullpushoffsetorder 属性可以通过内嵌到 xssmmdlg 属性中来使用。其中 :xs="6" 相当于 :xs="{ span: 6 }"

Col
Col
Col
<template>
  <div class="demo-row">
    <h-row>
      <h-col :xs="{ span: 5, offset: 1 }" :lg="{ span: 6, offset: 2 }">Col</h-col>
      <h-col :xs="{ span: 11, offset: 1 }" :lg="{ span: 6, offset: 2 }">Col</h-col>
      <h-col :xs="{ span: 5, offset: 1 }" :lg="{ span: 6, offset: 2 }">Col</h-col>
    </h-row>
  </div>
</template>
显示代码 复制代码

# Grid API

# Row props

属性 说明 类型 默认值
gutter 栅格间距,单位 px,左右平分 Number 0
type 布局模式,可选值为flex或不选,在现代浏览器下有效 String -
align flex 布局下的垂直对齐方式,可选值为topmiddlebottom String -
justify flex 布局下的水平排列方式,可选值为startendcenterspace-aroundspace-between String -
class-name 自定义的class名称 String -

# Col props

属性 说明 类型 默认值
span 栅格的占位格数,可选值为0~24的整数,为 0 时,相当于display:none Number | String -
order 栅格的顺序,在flex布局模式下有效 Number | String -
offset 栅格左侧的间隔格数,间隔内不可以有栅格 Number | String -
push 栅格向右移动格数 Number | String -
pull 栅格向左移动格数 Number | String -
class-name 自定义的class名称 String -
xs <768px 响应式栅格,可为栅格数或一个包含其他属性的对象 Number | Object -
sm ≥768px 响应式栅格,可为栅格数或一个包含其他属性的对象 Number | Object -
md ≥992px 响应式栅格,可为栅格数或一个包含其他属性的对象 Number | Object -
lg ≥1200px 响应式栅格,可为栅格数或一个包含其他属性的对象 Number | Object -