HTML Element - 基本元素
HTML Element - 基本元素
目标:
- 内容区分
- 文本内容
- 内联文本语义
- 图片和多媒体
- 内嵌内容
- 脚本
- 编辑标识
- 表格内容
- 表单
- 交互元素
- Web 组件
本章主要以 HTML 目前主要的元素使用为例,并附于元素实现效果图。
1. 内容区分
目标:
- address
- article
- aside
- footer
- header
- main
- nav
- section
内容分区元素允许你将文档内容从逻辑上进行组织划分。使用包括页眉 (header)、页脚 (footer)、导航 (nav) 和标题 (h1~h6) 等分区元素,来为页面内容创建明确的大纲,以便区分各个章节的内容。
1.1 address 地址
HTML <address>
元素 表示其中的 HTML 提供了某个人或某个组织(等等)的联系信息。
代码:
<p>Contact the author of this page:</p>
<address>
<a href="mailto:jim@rock.com">jim@rock.com</a><br>
<a href="tel:+13115552368">(311) 555-2368</a>
</address>
2
3
4
5
6
a[href^="mailto"]::before {
content: "📧 ";
}
a[href^="tel"]::before {
content: "📞 ";
}
2
3
4
5
6
7
效果图:
1.2 article 文章
HTML <article>
元素表示文档、页面、应用或网站中的独立结构,其意在成为可独立分配的或可复用的结构,如在发布中,它可能是论坛帖子、杂志或新闻文章、博客、用户提交的评论、交互式组件,或者其他独立的内容项目。
代码:
<article class="forecast">
<h1>Weather forecast for Seattle</h1>
<article class="day-forecast">
<h2>03 March 2018</h2>
<p>Rain.</p>
</article>
<article class="day-forecast">
<h2>04 March 2018</h2>
<p>Periods of rain.</p>
</article>
<article class="day-forecast">
<h2>05 March 2018</h2>
<p>Heavy rain.</p>
</article>
</article>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.forecast {
margin: 0;
padding: .3rem;
background-color: #eee;
font: 1rem 'Fira Sans', sans-serif;
}
.forecast>h1,
.day-forecast {
margin: .5rem;
padding: .3rem;
font-size: 1.2rem;
}
.day-forecast {
background: right/contain content-box border-box no-repeat url('/media/examples/rain.svg') white;
}
.day-forecast>h2,
.day-forecast>p {
margin: .2rem;
font-size: 1rem;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
效果图:
1.3 aside 侧边栏
HTML <aside>
元素表示一个和其余页面内容几乎无关的部分,被认为是独立于该内容的一部分并且可以被单独的拆分出来而不会使整体受影响。其通常表现为侧边栏或者标注框(call-out boxes)。
代码:
<p>Salamanders are a group of amphibians with a lizard-like appearance, including short legs and a tail in both larval and adult forms.</p>
<aside>
<p>The Rough-skinned Newt defends itself with a deadly neurotoxin.</p>
</aside>
<p>Several species of salamander inhabit the temperate rainforest of the Pacific Northwest, including the Ensatina, the Northwestern Salamander and the Rough-skinned Newt. Most salamanders are nocturnal, and hunt for insects, worms and other small creatures.</p>
2
3
4
5
6
7
8
效果图:
1.4 footer 脚注
HTML <footer>
元素表示最近一个章节内容或者根节点(sectioning root )元素的页脚。一个页脚通常包含该章节作者、版权数据或者与文档相关的链接等信息。
代码:
<article>
<h1>How to be a wizard</h1>
<ol>
<li>Grow a long, majestic beard.</li>
<li>Wear a tall, pointed hat.</li>
<li>Have I mentioned the beard?</li>
</ol>
<footer>
<p>© 2018 Gandalf</p>
</footer>
</article>
2
3
4
5
6
7
8
9
10
11
article {
min-height: 100%;
display: grid;
grid-template-rows: auto 1fr auto;
}
footer {
display: flex;
justify-content: center;
padding: 5px;
background-color: #45a1ff;
color: #fff;
}
2
3
4
5
6
7
8
9
10
11
12
13
效果图:
1.5 header 头部
HTML <header>
元素用于展示介绍性内容,通常包含一组介绍性的或是辅助导航的实用元素。它可能包含一些标题元素,但也可能包含其他元素,比如 Logo、搜索框、作者名称,等等。
代码:
<header class="page-header">
<h1>Cute Puppies Express!</h1>
</header>
<main>
<p>I love beagles <em>so</em> much! Like, really, a lot. They’re adorable and their ears are so, so snuggly soft!</p>
</main>
2
3
4
5
6
7
header.page-header {
background: no-repeat left/cover url(/media/examples/puppy-header-logo.jpg);
display: flex;
height: 120px;
min-width: 120px;
align-items: center;
color: #fff;
text-shadow: #000 0 0 .2em;
}
header.page-header > h1 {
font: bold calc(1em + 2 * (100vw - 120px) / 100) 'Dancing Script', cursive,
fantasy;
margin: 2%;
}
main {
font: 1rem 'Fira Sans', sans-serif;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
效果图:
1.6 main 主体
HTML <main>
元素呈现了文档的 body 或应用的主体部分。主体部分由与文档直接相关,或者扩展于文档的中心主题、应用的主要功能部分的内容组成。
代码:
<header>Gecko facts</header>
<main>
<p>Geckos are a group of usually small, usually nocturnal lizards. They are found on every continent except Australia.</p>
<p>Many species of gecko have adhesive toe pads which enable them to climb walls and even windows.</p>
</main>
2
3
4
5
6
7
header {
font: bold calc(.025 * (100vw)) 'Arial', sans-serif;
}
2
3
效果图:
1.7 nav 导航
HTML <nav>
元素表示页面的一部分,其目的是在当前文档或其他文档中提供导航链接。导航部分的常见示例是菜单,目录和索引。
代码:
<nav class="crumbs">
<ol>
<li class="crumb"><a href="#">Bikes</a></li>
<li class="crumb"><a href="#">BMX</a></li>
<li class="crumb">Jump Bike 3000</li>
</ol>
</nav>
<h1>Jump Bike 3000</h1>
<p>This BMX bike is a solid step into the pro world. It looks as legit as it rides and is built to polish your skills.</p>
2
3
4
5
6
7
8
9
10
nav {
border-bottom: 1px solid black;
}
.crumbs ol {
list-style-type: none;
padding-left: 0;
}
.crumb {
display: inline-block;
}
.crumb a::after {
display: inline-block;
color: #000;
content: '>';
font-size: 80%;
font-weight: bold;
padding: 0 3px;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
效果图:
1.8 section 块
HTML <section>
元素表示一个包含在 HTML 文档中的独立部分,它没有更具体的语义元素来表示,一般来说会有包含一个标题。
代码:
<h1>Choosing an Apple</h1>
<section>
<h2>Introduction</h2>
<p>This document provides a guide to help with the important task of choosing the correct Apple.</p>
</section>
<section>
<h2>Criteria</h2>
<p>There are many different criteria to be considered when choosing an Apple — size, color, firmness, sweetness, tartness...</p>
</section>
2
3
4
5
6
7
8
9
10
hgroup > h1, h2 {
margin: 0;
}
2
3
效果图:
2. 文本内容
目标:
- blockquote
- dd、dl、dt
- div
- figure、figcaption
- hr
- li、ol、ul
- menu
- p
- pre
使用 HTML 文本内容元素来组织在开标签 <body>
和闭标签 </body>
里的块或章节的内容。这些元素能标识内容的宗旨或结构,而这对于 accessibility 和 SEO 很重要。
2.1 blockquote 引用
HTML <blockquote>
元素(或者 HTML 块级引用元素),代表其中的文字是引用内容。通常在渲染时,这部分的内容会有一定的缩进(注 中说明了如何更改)。若引文来源于网络,则可以将原内容的出处 URL 地址设置到 cite 特性上,若要以文本的形式告知读者引文的出处时,可以通过 <cite>
元素。
代码:
<figure>
<blockquote cite="https://www.huxley.net/bnw/four.html">
<p>Words can be like X-rays, if you use them properly—they’ll go through anything. You read and you’re pierced.</p>
</blockquote>
<figcaption>—Aldous Huxley, <cite>Brave New World</cite></figcaption>
</figure>
2
3
4
5
6
blockquote {
margin: 0;
}
blockquote p {
padding: 15px;
background: #eee;
border-radius: 5px;
}
blockquote p::before {
content: '\201C';
}
blockquote p::after {
content: '\201D';
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
效果图:
2.2 dd 列表项、dl 列表、dt 列表项
HTML <dd>
元素(HTML 描述元素)用来指明一个描述列表 (dl) 元素中一个术语的描述。这个元素只能作为描述列表元素的子元素出现,并且必须跟着一个 dt 元素。
HTML <dl>
元素 (或 HTML 描述列表元素)是一个包含术语定义以及描述的列表,通常用于展示词汇表或者元数据 (键 - 值对列表)。
HTML <dt>
元素 (或 HTML 术语定义元素)用于在一个定义列表中声明一个术语。该元素仅能作为 dl 的子元素出现。通常在该元素后面会跟着 dd 元素, 然而,多个连续出现的 <dt>
元素都将由出现在它们后面的第一个 dd 元素定义。
代码:
<dl>
<div>
<dt>Name</dt>
<dd>Godzilla</dd>
</div>
<div>
<dt>Born</dt>
<dd>1952</dd>
</div>
<div>
<dt>Birthplace</dt>
<dd>Japan</dd>
</div>
<div>
<dt>Color</dt>
<dd>Green</dd>
</div>
</dl>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
dt:after {
content: ": ";
}
2
3
效果图:
2.3 div 块
HTML <div>
元素 (或 HTML 文档分区元素) 是一个通用型的流内容容器,在不使用CSS的情况下,其对内容或布局没有任何影响。
代码:
<div class="warning">
<img src="https://pic4.zhimg.com/80/v2-1a72072cf6819399d195908e5fcac14f_720w.jpg"
alt="An intimidating leopard.">
<p>Beware of the leopard</p>
</div>
2
3
4
5
.warning {
border: 10px ridge #f00;
background-color: #ff0;
padding: .5rem;
display: flex;
flex-direction: column;
}
.warning img {
width: 100%;
}
.warning p {
font: small-caps bold 1.2rem sans-serif;
text-align: center;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
效果图:
2.4 figure 图片、figcaption 图片标题
HTML <figure>
元素代表一段独立的内容,经常与说明(caption)figcaption 配合使用,并且作为一个独立的引用单元。当它属于主内容流(main flow)时,它的位置独立于主体。这个标签经常是在主文中引用的图片,插图,表格,代码段等等,当这部分转移到附录中或者其他页面时不会影响到主体。
HTML <figcaption>
元素 是与其相关联的图片的说明/标题,用?于描述其父节点 figure 元素里的其他数据。这意味着 <figcaption>
在figure 块里是第一个或最后一个。同时 HTML Figcaption 元素是可选的;如果没有该元素,这个父节点的图片只是会没有说明/标题。
代码:
<figure>
<img src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/elephant-660-480.jpg"
alt="Elephant at sunset">
<figcaption>An elephant at sunset</figcaption>
</figure>
2
3
4
5
figure {
border: thin #c0c0c0 solid;
display: flex;
flex-flow: column;
padding: 5px;
max-width: 220px;
margin: auto;
}
img {
max-width: 220px;
max-height: 150px;
}
figcaption {
background-color: #222;
color: #fff;
font: italic smaller sans-serif;
padding: 3px;
text-align: center;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
效果图:
2.5 hr 水平线
HTML <hr>
元素表示段落级元素之间的主题转换(例如,一个故事中的场景的改变,或一个章节的主题的改变)。
在 HTML 的早期版本中,它是一个水平线。现在它仍能在可视化浏览器中表现为水平线,但目前被定义为语义上的,而不是表现层面上。所以如果想画一条横线,请使用适当的 css 样式来修饰。
代码:
<p>§1: The first rule of Fight Club is: You do not talk about Fight Club.</p>
<hr>
<p>§2: The second rule of Fight Club is: Always bring cupcakes.</p>
2
3
4
5
hr {
border: none;
border-top: 3px double #333;
color: #333;
overflow: visible;
text-align: center;
height: 5px;
}
hr:after {
background: #fff;
content: '§';
padding: 0 4px;
position: relative;
top: -13px;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
效果图:
2.6 li 列表项、ol 有序列表、ul 列表
HTML <li>
元素 (或称 HTML 列表条目元素) 用于表示列表里的条目。它必须包含在一个父元素里:一个有序列表 (ol),一个无序列表 (ul),或者一个菜单 (menu)。在菜单或者无序列表里,列表条目通常用点排列显示;在有序列表里,列表条目通常在左边显示按升序排列的计数,例如数字或者字母。
HTML <ol>
元素表示有序列表,通常渲染为一个带编号的列表。
HTML <ul>
元素(或称 HTML 无序列表元素)表示一个内可含多个元素的无序列表或项目符号列表。
代码:
<ul>
<li>first item</li>
<li>second item <!-- Look, the closing </li> tag is not placed here! -->
<ol>
<li>second item first subitem</li>
<li>second item second subitem</li>
<li>second item third subitem</li>
</ol>
</li> <!-- Here is the closing </li> tag -->
<li>third item</li>
</ul>
2
3
4
5
6
7
8
9
10
11
效果图:
2.7 menu 菜单
HTML <menu>
元素呈现了一组用户可执行或激活的命令。这既包含了可能出现在屏幕顶端的列表菜单,也包含了那些隐藏在按钮之下、当点击按钮后显示出来的文本菜单。
代码:
<!-- A <div> element with a context menu -->
<div contextmenu="popup-menu">
Right-click to see the adjusted context menu
</div>
<menu type="context" id="popup-menu">
<menuitem>Action</menuitem>
<menuitem>Another action</menuitem>
<hr/>
<menuitem>Separated action</menuitem>
</menu>
2
3
4
5
6
7
8
9
10
11
div {
width: 300px;
height: 80px;
background-color: lightgreen;
}
2
3
4
5
效果图:
2.8 p 段落
HTML <p>
元素(或者说 HTML 段落元素)表示文本的一个段落。该元素通常表现为一整块与相邻文本分离的文本,或以垂直的空白隔离或以首行缩进。
代码:
<p>这是第一个段落。这是第一个段落。
这是第一个段落。这是第一个段落。</p>
<p>这是第二个段落。这是第二个段落。
这是第二个段落。这是第二个段落。</p>
2
3
4
效果图:
2.9 pre 格式化的文本
HTML <pre>
元素表示预定义格式文本。在该元素中的文本通常按照原文件中的编排,以等宽字体的形式展现出来,文本中的空白符(比如空格和换行符)都会显示出来。(紧跟在 <pre>
开始标签后的换行符也会被省略)
<pre>
L TE
A A
C V
R A
DOU
LOU
REUSE
QUE TU
PORTES
ET QUI T'
ORNE O CI
VILISÉ
OTE- TU VEUX
LA BIEN
SI RESPI
RER - Apollinaire
</pre>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
pre {
font-size: .7rem;
margin: 0;
}
2
3
4
效果图:
3. 内联文本语义
4. 图片和多媒体
5. 内嵌内容
6. 脚本
7. 编辑标识
8. 表格内容
9. 表单
HTML 提供了许多可一起使用的元素,这些元素能用来创建一个用户可以填写并提交到网站或应用程序的表单。
目标:
- button
- datalist
- fieldset
- form
- input
- label
- legend
- meter
- optgroup
- option
- output
- progress
- select
- textarea
9.1 button 按钮
HTML <button>
元素表示一个可点击的按钮,可以用在表单或文档其它需要使用简单标准按钮的地方。默认情况下,HTML 按钮的显示样式接近于 user agent 所在的宿主系统平台(用户操作系统)的按钮, 但你可以使用 CSS 来改变按钮的样貌。
代码:
<button name="button">Click me</button>
效果图:
9.2 datalist 列表
9.3 fieldset 字段集
10. 交互元素
HTML 提供了一系列有助于创建交互式用户界面对象的元素。
目标:
- details
- dialog
- summary
10.1 details 相信信息
HTML <details>
元素可创建一个挂件,仅在被切换成展开状态时,它才会显示内含的信息。<summary>
元素可为该部件提供概要或者标签。
代码:
<details>
<summary>Details</summary>
Something small enough to escape casual notice.
</details>
2
3
4
details {
border: 1px solid #aaa;
border-radius: 4px;
padding: .5em .5em 0;
}
summary {
font-weight: bold;
margin: -.5em -.5em 0;
padding: .5em;
}
details[open] {
padding: .5em;
}
details[open] summary {
border-bottom: 1px solid #aaa;
margin-bottom: .5em;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
效果图:
10.2 dialog 对话框
HTML <dialog>
元素表示一个对话框或其他交互式组件,例如一个检查器或者窗口。
代码:
<!-- Simple pop-up dialog box containing a form -->
<dialog id="favDialog">
<form method="dialog">
<p><label>Favorite animal:
<select>
<option></option>
<option>Brine shrimp</option>
<option>Red panda</option>
<option>Spider monkey</option>
</select>
</label></p>
<menu>
<button value="cancel">Cancel</button>
<button id="confirmBtn" value="default">Confirm</button>
</menu>
</form>
</dialog>
<menu>
<button id="updateDetails">Update details</button>
</menu>
<output aria-live="polite"></output>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(function() {
var updateButton = document.getElementById('updateDetails');
var favDialog = document.getElementById('favDialog');
var outputBox = document.getElementsByTagName('output')[0];
var selectEl = document.getElementsByTagName('select')[0];
var confirmBtn = document.getElementById('confirmBtn');
// “Update details” button opens the <dialog> modally
updateButton.addEventListener('click', function onOpen() {
if (typeof favDialog.showModal === "function") {
favDialog.showModal();
} else {
alert("The dialog API is not supported by this browser");
}
});
// "Favorite animal" input sets the value of the submit button
selectEl.addEventListener('change', function onSelect(e) {
confirmBtn.value = selectEl.value;
});
// "Confirm" button of form triggers "close" on dialog because of [method="dialog"]
favDialog.addEventListener('close', function onClose() {
outputBox.value = favDialog.returnValue + " button clicked - " + (new Date()).toString();
});
})();
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
效果图:
10.3 summary 描述
HTML <summary>
元素 用作 一个 <details>
元素的一个内容的摘要,标题或图例。
11. Web 组件
Web 组件是一种与 HTML 相关联(HTML-related)的技术,简单来说,它允许开发者创建自定义元素,并如同普通的 HTML 一样使用它们。另外,也可以创建经过自定义的标准 HTML 元素。
目标:
- content
- shadow
- slot
- template
11.1 content 内容
HTML <content>
元素— Web 组件 的技术套件的废弃部分 — 用于 Shadow DOM 内部作为 insertion point,并且不可用于任何正常的 HTML,现在已被 slot 元素代替,它在 DOM 中创建一个位置,Shadow DOM 会插入这里。
代码:
<html>
<head></head>
<body>
<!-- The original content accessed by <content> -->
<div>
<h4>My Content Heading</h4>
<p>My content text</p>
</div>
<script>
// Get the <div> above.
var myContent = document.querySelector('div');
// Create a shadow DOM on the <div>
var shadowroot = myContent.createShadowRoot();
// Insert into the shadow DOM a new heading and
// part of the original content: the <p> tag.
shadowroot.innerHTML =
'<h2>Inserted Heading</h2> <content select="p"></content>';
</script>
</body>
</html>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
效果图:
11.2 shadow 影子
HTML <shadow>
元素 — Web 组件技术套件的废弃部分 — 目的是用作 Shadow DOM insertion point。如果你在 shadow host 下面创建了多个 shadow root,你就可能已经使用了它。在正常的 HTML 没有任何用处。
11.3 slot 分派插槽
HTML <slot>
元素 ,作为 Web Components 技术套件的一部分,是Web 组件内的一个占位符。该占位符可以在后期使用自己的标记语言填充,这样您就可以创建单独的 DOM 树,并将它与其它的组件组合在一起。
11.4 template 模板
HTML 内容模板(<template>
)元素是一种用于保存客户端内容机制,该内容在加载页面时不会呈现,但随后可以 (原文为 may be) 在运行时使用 JavaScript 实例化。
将模板视为一个可存储在文档中以便后续使用的内容片段。虽然解析器在加载页面时确实会处理 <template>
元素的内容,但这样做只是为了确保这些内容有效;但元素内容不会被渲染。