Layui 栅格系统是一套具备响应式能力的布局方案,采用业界比较常用的容器横向
12
等分规则,预设了5*12
种 CSS 排列类,内置多种大小尺寸的多终端适配,能很好地实现响应式布局,这意味着一套系统,能同时适配于电脑的不同大小屏幕和手机、平板等移动屏幕,使得网页布局变得更加灵活,同时也极大地降低了HTML/CSS
代码的耦合。
贴士:以下示例中的背景色仅仅只是为了让布局效果显得更加直观,实际使用时并不需要背景色。
01.<!DOCTYPE html>02.<html>03.<head>04. <meta charset="utf-8">05. <meta name="viewport" content="width=device-width, initial-scale=1">06. <title>Demo</title>07. <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->08. <link href="//unpkg.com/layui@2.9.7/dist/css/layui.css" rel="stylesheet">09.</head>10.<body>11.<div class="layui-row">12. <div class="layui-col-xs6">13. <div class="grid-demo grid-demo-bg1">6/12</div>14. </div>15. <div class="layui-col-xs6">16. <div class="grid-demo">6/12</div>17. </div>18.</div>19.<div class="layui-row">20. <div class="layui-col-xs3">21. <div class="grid-demo grid-demo-bg1">3/12</div>22. </div>23. <div class="layui-col-xs3">24. <div class="grid-demo">3/12</div>25. </div>26. <div class="layui-col-xs3">27. <div class="grid-demo grid-demo-bg1">3/12</div>28. </div>29. <div class="layui-col-xs3">30. <div class="grid-demo">3/12</div>31. </div>32.</div>33. 34.</body>35.</html>
01.<!DOCTYPE html>02.<html>03.<head>04. <meta charset="utf-8">05. <meta name="viewport" content="width=device-width, initial-scale=1">06. <title>Demo</title>07. <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->08. <link href="//unpkg.com/layui@2.9.7/dist/css/layui.css" rel="stylesheet">09.</head>10.<body>11.<div class="layui-row">12. <div class="layui-col-xs12 layui-col-md8">13. <div class="grid-demo grid-demo-bg1">xs:12/12 | md:8/12</div>14. </div>15. <div class="layui-col-xs6 layui-col-md4">16. <div class="grid-demo">xs:6/12 | md:4/12</div>17. </div>18. <div class="layui-col-xs6 layui-col-md12">19. <div class="grid-demo grid-demo-bg2">xs:6/12 | md:12/12</div>20. </div>21.</div>22. 23.</body>24.</html>
01.<!DOCTYPE html>02.<html>03.<head>04. <meta charset="utf-8">05. <meta name="viewport" content="width=device-width, initial-scale=1">06. <title>Demo</title>07. <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->08. <link href="//unpkg.com/layui@2.9.7/dist/css/layui.css" rel="stylesheet">09.</head>10.<body>11.<div class="layui-row">12. <div class="layui-col-xs6 layui-col-sm6 layui-col-md4">13. <div class="grid-demo grid-demo-bg1">xs:6/12 | sm:6/12 | md:4/12</div>14. </div>15. <div class="layui-col-xs6 layui-col-sm6 layui-col-md4">16. <div class="grid-demo layui-bg-red">xs:6/12 | sm:6/12 | md:4/12</div>17. </div>18. <div class="layui-col-xs4 layui-col-sm12 layui-col-md4">19. <div class="grid-demo layui-bg-blue">xs:4/12 | sm:12/12 | md:4/12</div>20. </div>21. <div class="layui-col-xs4 layui-col-sm7 layui-col-md8">22. <div class="grid-demo layui-bg-green">xs:4/12 | sm:7/12 | md:8/12</div>23. </div>24. <div class="layui-col-xs4 layui-col-sm5 layui-col-md4">25. <div class="grid-demo layui-bg-black">xs:4/12 | sm:5/12 | md:4/12</div>26. </div>27.</div>28. 29.</body>30.</html>
01.<!DOCTYPE html>02.<html>03.<head>04. <meta charset="utf-8">05. <meta name="viewport" content="width=device-width, initial-scale=1">06. <title>Demo</title>07. <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->08. <link href="//unpkg.com/layui@2.9.7/dist/css/layui.css" rel="stylesheet">09.</head>10.<body>11.<div class="layui-row">12. <div class="layui-col-md1">13. <div class="grid-demo grid-demo-bg1">1/12</div>14. </div>15. <div class="layui-col-md1">16. <div class="grid-demo">1/12</div>17. </div>18. <div class="layui-col-md1">19. <div class="grid-demo grid-demo-bg1">1/12</div>20. </div>21. <div class="layui-col-md1">22. <div class="grid-demo">1/12</div>23. </div>24. <div class="layui-col-md1">25. <div class="grid-demo grid-demo-bg1">1/12</div>26. </div>27. <div class="layui-col-md1">28. <div class="grid-demo">1/12</div>29. </div>30. <div class="layui-col-md1">31. <div class="grid-demo grid-demo-bg1">1/12</div>32. </div>33. <div class="layui-col-md1">34. <div class="grid-demo">1/12</div>35. </div>36. <div class="layui-col-md1">37. <div class="grid-demo grid-demo-bg1">1/12</div>38. </div>39. <div class="layui-col-md1">40. <div class="grid-demo">1/12</div>41. </div>42. <div class="layui-col-md1">43. <div class="grid-demo grid-demo-bg1">1/12</div>44. </div>45. <div class="layui-col-md1">46. <div class="grid-demo">1/12</div>47. </div>48.</div>49. 50.<div class="layui-row">51. <div class="layui-col-md9">52. <div class="grid-demo grid-demo-bg1">75%</div>53. </div>54. <div class="layui-col-md3">55. <div class="grid-demo">25%</div>56. </div>57.</div>58. 59.<div class="layui-row">60. <div class="layui-col-md4">61. <div class="grid-demo grid-demo-bg1">33.33%</div>62. </div>63. <div class="layui-col-md4">64. <div class="grid-demo">33.33%</div>65. </div>66. <div class="layui-col-md4">67. <div class="grid-demo grid-demo-bg1">33.33%</div>68. </div>69.</div>70. 71.<div class="layui-row">72. <div class="layui-col-md6">73. <div class="grid-demo grid-demo-bg1">50%</div>74. </div>75. <div class="layui-col-md6">76. <div class="grid-demo">50%</div>77. </div>78.</div>79. 80.</body>81.</html>
01.<!DOCTYPE html>02.<html>03.<head>04. <meta charset="utf-8">05. <meta name="viewport" content="width=device-width, initial-scale=1">06. <title>Demo</title>07. <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->08. <link href="//unpkg.com/layui@2.9.7/dist/css/layui.css" rel="stylesheet">09.</head>10.<body>11.<div class="layui-row layui-col-space1">12. <div class="layui-col-md3">13. <div class="grid-demo grid-demo-bg1">1/4</div>14. </div>15. <div class="layui-col-md3">16. <div class="grid-demo">1/4</div>17. </div>18. <div class="layui-col-md3">19. <div class="grid-demo grid-demo-bg1">1/4</div>20. </div>21. <div class="layui-col-md3">22. <div class="grid-demo">1/4</div>23. </div>24.</div>25. 26.<div class="layui-row layui-col-space5">27. <div class="layui-col-md4">28. <div class="grid-demo grid-demo-bg1">1/3</div>29. </div>30. <div class="layui-col-md4">31. <div class="grid-demo">1/3</div>32. </div>33. <div class="layui-col-md4">34. <div class="grid-demo grid-demo-bg1">1/3</div>35. </div>36.</div>37. 38.<div class="layui-row layui-col-space10">39. <div class="layui-col-md9">40. <div class="grid-demo grid-demo-bg1">9/12</div>41. </div>42. <div class="layui-col-md3">43. <div class="grid-demo">3/12</div>44. </div>45.</div>46. 47.<div class="layui-row layui-col-space15">48. <div class="layui-col-md7">49. <div class="grid-demo grid-demo-bg1">7/12</div>50. </div>51. <div class="layui-col-md5">52. <div class="grid-demo">5/12</div>53. </div>54.</div>55. 56.<div class="layui-row layui-col-space30">57. <div class="layui-col-md7">58. <div class="grid-demo grid-demo-bg1">7/12</div>59. </div>60. <div class="layui-col-md5">61. <div class="grid-demo">5/12</div>62. </div>63.</div>64. 65.</body>66.</html>
01.<!DOCTYPE html>02.<html>03.<head>04. <meta charset="utf-8">05. <meta name="viewport" content="width=device-width, initial-scale=1">06. <title>Demo</title>07. <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->08. <link href="//unpkg.com/layui@2.9.7/dist/css/layui.css" rel="stylesheet">09.</head>10.<body>11.<div class="layui-row">12. <div class="layui-col-md4">13. <div class="grid-demo grid-demo-bg1">4/12</div>14. </div>15. <div class="layui-col-md4 layui-col-md-offset4">16. <div class="grid-demo">偏移4列</div>17. </div>18.</div>19. 20.<div class="layui-row">21. <div class="layui-col-md3 layui-col-md-offset3">22. <div class="grid-demo grid-demo-bg1">偏移3列</div>23. </div>24. <div class="layui-col-md3">25. <div class="grid-demo">不偏移</div>26. </div>27.</div>28. 29.</body>30.</html>
理论上,你可以对栅格进行无穷层次的嵌套,这更加增强了栅格的表现能力
01.<!DOCTYPE html>02.<html>03.<head>04. <meta charset="utf-8">05. <meta name="viewport" content="width=device-width, initial-scale=1">06. <title>Demo</title>07. <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->08. <link href="//unpkg.com/layui@2.9.7/dist/css/layui.css" rel="stylesheet">09.</head>10.<body>11.<div class="layui-row">12. <div class="layui-col-md5">13. <div class="layui-row grid-demo">14. <div class="layui-col-md3">15. <div class="grid-demo grid-demo-bg1">内部列</div>16. </div>17. <div class="layui-col-md9">18. <div class="grid-demo grid-demo-bg2">内部列</div>19. </div>20. <div class="layui-col-md12">21. <div class="grid-demo grid-demo-bg3">内部列</div>22. </div>23. </div>24. </div>25. <div class="layui-col-md7">26. <div class="layui-row grid-demo grid-demo-bg1">27. <div class="layui-col-md12">28. <div class="grid-demo">内部列</div>29. </div>30. <div class="layui-col-md9">31. <div class="grid-demo grid-demo-bg2">内部列</div>32. </div>33. <div class="layui-col-md3">34. <div class="grid-demo grid-demo-bg3">内部列</div>35. </div>36. </div>37. </div>38.</div>39. 40.</body>41.</html>
01.<!DOCTYPE html>02.<html>03.<head>04. <meta charset="utf-8">05. <meta name="viewport" content="width=device-width, initial-scale=1">06. <title>Demo</title>07. <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->08. <link href="//unpkg.com/layui@2.9.7/dist/css/layui.css" rel="stylesheet">09.</head>10.<body>11.<div class="layui-row">12. <div class="layui-col-sm3">13. <div class="grid-demo grid-demo-bg1">25%</div>14. </div>15. <div class="layui-col-sm3">16. <div class="grid-demo">25%</div>17. </div>18. <div class="layui-col-sm3">19. <div class="grid-demo grid-demo-bg1">25%</div>20. </div>21. <div class="layui-col-sm3">22. <div class="grid-demo">25%</div>23. </div>24.</div>25. 26.</body>27.</html>
1. | 采用 layui-row 来定义行,如:<div class="layui-row"></div> |
2. |
采用类似 layui-col-md* 这样的预设类来定义一组列(column),且放在行(row)内。其中:
|
3. | 列可以同时出现最多 5 种不同的组合,分别是:xs / sm / md / lg / xl 2.8+,以在不同尺寸屏幕下进行自动适配。 |
4. | 可对列追加类似 layui-col-space5、 layui-col-md-offset3 这样的预设类来定义列的间距和偏移。 |
5. | 最后,在列(column)元素中放入你自己的任意元素填充内容,完成布局! |
栅格的响应式能力,得益于 CSS3
媒体查询(Media Queries
),针对不同尺寸的屏幕进行相应的适配处理。
超小屏幕 (手机<768px) |
小屏幕 (平板≥768px) |
中等屏幕 (桌面≥992px) |
大型屏幕 (桌面≥1200px) |
超大屏幕 (桌面≥1400px) |
|
---|---|---|---|---|---|
layui-container | auto | 750px | 970px | 1170px | 1330px |
标记 | xs | sm | md | lg | xl 2.8+ |
列对应类 | layui-col-xs* | layui-col-sm* | layui-col-md* | layui-col-lg* | layui-col-xl* |
总列数 | 12 | ||||
响应行为 | 始终按比例水平排列 | 在当前屏幕下水平排列,如果屏幕大小低于临界值则堆叠排列 |
类名(class) | 说明 |
---|---|
layui-show-*-block | 定义不同设备下的 display: block; * 可选值有:xs、sm、md、lg、xl |
layui-show-*-inline | 定义不同设备下的 display: inline; * 可选值同上 |
layui-show-*-inline-block | 定义不同设备下的 display: inline-block; * 可选值同上 |
layui-hide-* | 定义不同设备下的隐藏类,即: display: none; * 可选值同上 |
将栅格放入一个带有 class="layui-container"
的特定容器中,以便在小屏幕以上的设备中固定宽度,让列可控。
01.<div class="layui-container">02. <div class="layui-row">03. ……04. </div>05.</div>
当然,你还可以不固定容器宽度。将栅格或其它元素放入一个带有 class="layui-fluid"
的容器中,那么宽度将不会固定,而是 100% 适应
01.<div class="layui-fluid">02. ……03.</div>
通过“列间距”的预设类,来设定列之间的间距。且一行中最左的列不会出现左边距,最右的列不会出现右边距。列间距在保证排版美观的同时,还可以进一步保证分列的宽度精细程度。我们结合网页常用的边距,预设了 12 种不同尺寸的边距,分别是: | |
|
下面是一个简单的例子,列间距为 16px
:
01.<div class="layui-row layui-col-space16">02. <div class="layui-col-md4">03. 1/304. </div>05. <div class="layui-col-md4">06. 1/307. </div>08. <div class="layui-col-md4">09. 1/310. </div>11.</div>
对列追加类似 layui-col-md-offset*
的预设类,从而让列向右偏移。如:layui-col-md-offset3
,即代表在“中型桌面屏幕”下,让该列向右偏移
3 个列宽度。下面是一个采用「列偏移」机制让两个列左右对齐的实例
01.div class="layui-row">02. <div class="layui-col-md4">03. 4/1204. </div>05. <div class="layui-col-md4 layui-col-md-offset4">06. 偏移4列,从而在最右07. </div>08.</div>
请注意,列偏移可针对不同屏幕的标准进行设定,比如上述的例子,只会在桌面屏幕下有效,当低于桌面屏幕的规定的临界值,就会堆叠排列。
事实上 IE8/IE9
并不支持 Media Queries
,但你可以使用下面的补丁进行兼容(补丁来自于开源社区):
01.<!-- 让 IE8/9 支持媒体查询,从而兼容栅格 -->02.<!--[if lt IE 9]>03. <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>04. <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>05.<![endif]-->
将上述代码放入你页面 <body>
标签内的任意位置即可。