Material Design Lite - 布局
在本章中,我们将讨论 Material Design Lite 中的不同布局。HTML5 具有以下容器元素 −
<div> − 为 HTML 内容提供通用容器。
<header> − 表示页眉部分。
<footer> − 表示页脚部分。
<article> − 表示文章。
<section> −为各种类型的部分提供通用容器。
MDL 提供各种 CSS 类,以将各种预定义的视觉和行为增强功能应用于容器。下表列出了可用的类及其效果。
Sr.No. | 类名和说明 |
---|---|
1 | mdl-layout 将容器标识为 MDL 组件。外部容器元素上是必需的。 |
2 | mdl-js-layout 向布局添加基本 MDL 行为。外部容器元素上是必需的。 |
3 | mdl-layout__header 将容器标识为 MDL 组件。标题元素上是必需的。 |
4 | mdl-layout-icon 用于添加应用程序图标。如果两者都可见,则被菜单图标覆盖。可选图标元素。 |
5 | mdl-layout__header-row 将容器标识为 MDL 标题行。标题内容容器上必需。 |
6 | mdl-layout__title 标识布局标题文本。布局标题容器上必需。 |
7 | mdl-layout-spacer 用于对齐标题或抽屉内的元素。它会增长以填充剩余空间。通常用于将元素向右对齐。布局标题后面的 div 上可选。 |
8 | mdl-navigation 将容器标识为 MDL 导航组。导航元素上是必需的。 |
9 | mdl-navigation__link 将锚点标识为 MDL 导航链接。标题和/或抽屉锚点元素上是必需的。 |
10 | mdl-layout__drawer 将容器标识为 MDL 布局抽屉。抽屉容器元素上必需。 |
11 | mdl-layout__content 将容器标识为 MDL 布局内容。主元素上必需。 |
12 | mdl-layout__header--scroll 使标题随内容滚动。标题元素上可选。 |
13 | mdl-layout--fixed-drawer 使抽屉始终可见并在较大的屏幕上打开。外部容器元素上可选,但抽屉容器元素上不可选。 |
14 | mdl-layout--fixed-header 使标题始终可见,即使在小屏幕上也是如此。外部容器元素上可选。 |
15 | mdl-layout--large-screen-only 在较小的屏幕上隐藏元素。在 mdl-layout 的任何后代上都是可选的。 |
16 | mdl-layout--small-screen-only 在较大的屏幕上隐藏元素。在 mdl-layout 的任何后代上都是可选的。 |
17 | mdl-layout__header--waterfall 允许使用多个标题行实现"瀑布"效果。标题元素上可选。 |
18 | mdl-layout__header--transparent 使标题透明并在布局背景上绘制。标题元素上可选。 |
19 | mdl-layout__header--seamed 使用没有阴影的标题。标题元素上可选。 |
20 | mdl-layout__tab-bar 将容器标识为 MDL 选项卡栏。标题内的容器元素上必需(选项卡式布局)。 |
21 | mdl-layout__tab 将锚点标识为 MDL 选项卡链接。标签栏锚元素上是必需的。 |
22 | is-active 将选项卡标识为默认活动选项卡。标签栏锚元素和相关选项卡部分元素上是可选的。 |
23 | mdl-layout__tab-panel 将容器标识为选项卡内容面板。选项卡部分元素上必需。 |
24 | mdl-layout--fixed-tabs 使用固定选项卡而不是默认的可滚动选项卡。外部容器元素上可选,而不是标题内的容器。 |
以下示例显示了如何使用 mdl-layout 类来设置各种容器的样式。
固定抽屉
要创建带有固定抽屉但没有标题的模板,请使用以下 MDL 类。
mdl-layout −将 div 标识为 MDL 组件。
mdl-js-layout − 向外部 div 添加基本 MDL 行为。
mdl-layout--fixed-drawer − 使抽屉始终可见并在更大的屏幕中打开。
mdl-layout__drawer − 将 div 标识为 MDL 布局抽屉。
mdl-layout-title − 标识布局标题文本。
mdl-navigation − 将 div 标识为 MDL 导航组。
mdl-navigation__link −将锚点标识为 MDL 导航链接。
mdl-layout__content − 将 div 标识为 MDL 布局内容。
mdl_fixeddrawer.htm
<html> <head> <link rel = "stylesheet" href = "https://storage.googleapis.com/code.getmdl.io/1.0.6/material.indigo-pink.min.css"> <script src = "https://storage.googleapis.com/code.getmdl.io/1.0.6/material.min.js"> </script> <link rel = "stylesheet" href = "https://fonts.googleapis.com/icon?family=Material+Icons"> </head> <body> <!-- No header, and the drawer stays open on larger screens (fixed drawer).--> <div class = "mdl-layout mdl-js-layout mdl-layout--fixed-drawer"> <div class = "mdl-layout__drawer"> <span class = "mdl-layout-title">HTML5 Tutorial</span> <nav class = "mdl-navigation"> <a class = "mdl-navigation__link" href = "">Home</a> <a class = "mdl-navigation__link" href = "">About</a> </nav> </div> <main class = "mdl-layout__content"> <div class = "page-content" style = "padding-left:100px;">Hello World!</div> </main> </div> </body> </html>
结果
验证结果。
固定标题
要创建带有固定标题的模板,请使用以下附加 MDL 类。
mdl-layout--fixed-header − 使标题始终可见,即使在小屏幕上也是如此。
mdl_fixedheader.htm
<html> <head> <link rel = "stylesheet" href = "https://storage.googleapis.com/code.getmdl.io/1.0.6/material.indigo-pink.min.css"> <script src = "https://storage.googleapis.com/code.getmdl.io/1.0.6/material.min.js"> </script> <link rel = "stylesheet" href = "https://fonts.googleapis.com/icon?family=Material+Icons"> </head> <body> <!-- Always shows a header, even in smaller screens. --> <div class = "mdl-layout mdl-js-layout mdl-layout--fixed-header"> <header class = "mdl-layout__header"> <div class = "mdl-layout__header-row"> <!-- Title --> <span class = "mdl-layout-title">HTML5 Tutorial</span> <!-- Add spacer, to align navigation to the right --> <div class = "mdl-layout-spacer"></div> <!-- Navigation --> <nav class = "mdl-navigation"> <a class = "mdl-navigation__link" href = "" style = "color:gray">Home</a> <a class = "mdl-navigation__link" href = "" style = "color:gray">About</a> </nav> </div> </header> <div class = "mdl-layout__drawer"> <span class = "mdl-layout-title">HTML5 Tutorial</span> <nav class = "mdl-navigation"> <a class = "mdl-navigation__link" href = "">Home</a> <a class = "mdl-navigation__link" href = "">About</a> </nav> </div> <main class = "mdl-layout__content"> <div class = "page-content">Hello World!</div> </main> </div> </body> </html>
结果
验证结果。
固定标题和抽屉
要创建具有固定标题和固定抽屉的模板,请使用以下附加 MDL 类。
mdl-layout--fixed-drawer − 使抽屉始终可见并在较大的屏幕上打开。
mdl-layout--fixed-header − 使标题始终可见,即使在小屏幕上也是如此。
mdl_fixedheader.htm
<html> <head> <link rel = "stylesheet" href = "https://storage.googleapis.com/code.getmdl.io/1.0.6/material.indigo-pink.min.css"> <script src = "https://storage.googleapis.com/code.getmdl.io/1.0.6/material.min.js"> </script> <link rel = "stylesheet" href = "https://fonts.googleapis.com/icon?family=Material+Icons"> </head> <body> <!-- The drawer is always open in large screens. The header is always shown, even in small screens. --> <div class = "mdl-layout mdl-js-layout mdl-layout--fixed-drawer mdl-layout--fixed-header"> <header class = "mdl-layout__header"> <div class = "mdl-layout__header-row"> <!-- Title --> <span class = "mdl-layout-title">HTML5 Tutorial</span> <!-- Add spacer, to align navigation to the right --> <div class = "mdl-layout-spacer"></div> <!-- Navigation --> <nav class = "mdl-navigation"> <a class = "mdl-navigation__link" href = "" style = "color:gray">Home</a> <a class = "mdl-navigation__link" href = "" style = "color:gray">About</a> </nav> </div> </header> <div class = "mdl-layout__drawer"> <span class = "mdl-layout-title">HTML5 Tutorial</span> <nav class = "mdl-navigation"> <a class = "mdl-navigation__link" href = "">Home</a> <a class = "mdl-navigation__link" href = "">About</a> </nav> </div> <main class = "mdl-layout__content"> <div class = "page-content">Hello World!</div> </main> </div> </body> </html>
结果
验证结果。
滚动标题
要创建带有滚动标题的模板,请使用以下 MDL 类。
mdl-layout--header--scroll − 使标题随内容滚动。
mdl_scrollingheader.htm
<html> <head> <link rel = "stylesheet" href = "https://storage.googleapis.com/code.getmdl.io/1.0.6/material.indigo-pink.min.css"> <script src = "https://storage.googleapis.com/code.getmdl.io/1.0.6/material.min.js"> </script> <link rel = "stylesheet" href = "https://fonts.googleapis.com/icon?family=Material+Icons"> </head> <body> <!-- Uses a header that scrolls with the text, rather than staying locked at the top --> <div class = "mdl-layout mdl-js-layout "> <header class = "mdl-layout__header mdl-layout__header--scroll"> <div class = "mdl-layout__header-row"> <!-- Title --> <span class = "mdl-layout-title">HTML5 Tutorial</span> <!-- Add spacer, to align navigation to the right --> <div class = "mdl-layout-spacer"></div> <!-- Navigation --> <nav class = "mdl-navigation"> <a class = "mdl-navigation__link" href = "">Home</a> <a class = "mdl-navigation__link" href = "">About</a> </nav> </div> </header> <div class = "mdl-layout__drawer"> <span class = "mdl-layout-title">HTML5 Tutorial</span> <nav class = "mdl-navigation"> <a class = "mdl-navigation__link" href = "">Home</a> <a class = "mdl-navigation__link" href = "">About</a> </nav> </div> <main class = "mdl-layout__content"> <div class = "page-content" style = "padding-left:100px;">Hello World! <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... </div> </main> </div> </body> </html>
结果
验证结果。
收缩标题
要创建一个标题在页面向下滚动时收缩的模板,请使用以下 MDL 类。
mdl-layout__header--waterfall − 允许使用多个标题行实现"瀑布"效果。
mdl_waterfallheader.htm
<html> <head> <link rel = "stylesheet" href = "https://storage.googleapis.com/code.getmdl.io/1.0.6/material.indigo-pink.min.css"> <script src = "https://storage.googleapis.com/code.getmdl.io/1.0.6/material.min.js"> </script> <link rel = "stylesheet" href = "https://fonts.googleapis.com/icon?family=Material+Icons"> </head> <body> <!-- The drawer is always open in large screens. The header is always shown, even in small screens. --> <div class = "mdl-layout mdl-js-layout "> <header class = "mdl-layout__header mdl-layout__header--waterfall"> <!-- Top row, always visible --> <div class = "mdl-layout__header-row"> <!-- Title --> <span class = "mdl-layout-title">HTML5 Tutorial</span> <!-- Add spacer, to align navigation to the right --> <div class = "mdl-layout-spacer"></div> </div> <!-- Bottom row, not visible on scroll --> <div class = "mdl-layout__header-row"> <div class = "mdl-layout-spacer"></div> <!-- Navigation --> <nav class = "mdl-navigation"> <a class = "mdl-navigation__link" href = "">Home</a> <a class = "mdl-navigation__link" href = "">About</a> </nav> </div> </header> <div class = "mdl-layout__drawer"> <span class = "mdl-layout-title">HTML5 Tutorial</span> <nav class = "mdl-navigation"> <a class = "mdl-navigation__link" href = "">Home</a> <a class = "mdl-navigation__link" href = "">About</a> </nav> </div> <main class = "mdl-layout__content"> <div class = "page-content" style = "padding-left:100px;">Hello World! <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... <br/><br/><br/><br/><br/><br/><br/><br/>... </div> </main> </div> </body> </html>
结果
验证结果。
带有可滚动选项卡的固定标题
要创建带有可滚动选项卡的标题的模板,请使用以下 MDL 类。
mdl-layout__tab-bar − 将容器标识为 MDL 选项卡栏。
mdl-layout__tab − 将锚点标识为 MDL 选项卡链接。
mdl-layout__tab-panel −将容器标识为选项卡内容面板。
mdl_scrollabletabheader.htm
<html> <head> <link rel = "stylesheet" href = "https://storage.googleapis.com/code.getmdl.io/1.0.6/material.indigo-pink.min.css"> <script src = "https://storage.googleapis.com/code.getmdl.io/1.0.6/material.min.js"> </script> <link rel = "stylesheet" href = "https://fonts.googleapis.com/icon?family=Material+Icons"> </head> <body> <!-- The drawer is always open in large screens. The header is always shown, even in small screens. --> <div class = "mdl-layout mdl-js-layout mdl-layout--fixed-header"> <header class = "mdl-layout__header"> <!-- Top row, always visible --> <div class = "mdl-layout__header-row"> <!-- Title --> <span class = "mdl-layout-title">HTML5 Tutorial</span> </div> <!-- Tabs --> <div class = "mdl-layout__tab-bar mdl-js-ripple-effect"> <a href = "#scroll-tab-1" class = "mdl-layout__tab is-active">Tab 1</a> <a href = "#scroll-tab-2" class = "mdl-layout__tab">Tab 2</a> <a href = "#scroll-tab-3" class = "mdl-layout__tab">Tab 3</a> </div> </header> <div class = "mdl-layout__drawer"> <span class = "mdl-layout-title">HTML5 Tutorial</span> <nav class = "mdl-navigation"> <a class = "mdl-navigation__link" href = "">Home</a> <a class = "mdl-navigation__link" href = "">About</a> </nav> </div> <main class = "mdl-layout__content"> <section class = "mdl-layout__tab-panel is-active" id = "scroll-tab-1"> <div class = "page-content">Tab 1 Contents</div> </section> <section class = "mdl-layout__tab-panel" id = "scroll-tab-2"> <div class = "page-content">Tab 2 Contents</div> </section> <section class = "mdl-layout__tab-panel" id = "scroll-tab-3"> <div class = "page-content">Tab 3 Contents</div> </section> </main> </div> </body> </html>
结果
验证结果。
带有固定选项卡的固定标题
要创建带有固定选项卡的标题的模板,请使用以下附加 MDL 类。
mdl-layout--fixed-tabs − 使用固定选项卡代替默认的可滚动选项卡。
mdl_fixedtabheader.htm
<html> <head> <link rel = "stylesheet" href = "https://storage.googleapis.com/code.getmdl.io/1.0.6/material.indigo-pink.min.css"> <script src = "https://storage.googleapis.com/code.getmdl.io/1.0.6/material.min.js"> </script> <link rel = "stylesheet" href = "https://fonts.googleapis.com/icon?family=Material+Icons"> </head> <body> <!-- The drawer is always open in large screens. The header is always shown, even in small screens. --> <div class = "mdl-layout mdl-js-layout mdl-layout--fixed-header mdl-layout--fixed-tabs"> <header class = "mdl-layout__header"> <!-- Top row, always visible --> <div class = "mdl-layout__header-row"> <!-- Title --> <span class = "mdl-layout-title">HTML5 Tutorial</span> </div> <!-- Tabs --> <div class = "mdl-layout__tab-bar mdl-js-ripple-effect"> <a href = "#scroll-tab-1" class = "mdl-layout__tab is-active">Tab 1</a> <a href = "#scroll-tab-2" class = "mdl-layout__tab">Tab 2</a> <a href = "#scroll-tab-3" class = "mdl-layout__tab">Tab 3</a> </div> </header> <div class = "mdl-layout__drawer"> <span class = "mdl-layout-title">HTML5 Tutorial</span> <nav class = "mdl-navigation"> <a class = "mdl-navigation__link" href = "">Home</a> <a class = "mdl-navigation__link" href = "">About</a> </nav> </div> <main class = "mdl-layout__content"> <section class = "mdl-layout__tab-panel is-active" id = "scroll-tab-1"> <div class = "page-content">Tab 1 Contents</div> </section> <section class = "mdl-layout__tab-panel" id = "scroll-tab-2"> <div class = "page-content">Tab 2 Contents</div> </section> <section class = "mdl-layout__tab-panel" id = "scroll-tab-3"> <div class = "page-content">Tab 3 Contents</div> </section> </main> </div> </body> </html>
结果
验证结果。