结构与表单

本页目录

列表与语义结构

标签用途
<ul> / <ol>无序列表 / 有序列表。列表项用 <li>
<dl>描述列表。<dt> 写术语,<dd> 写描述
<header> / <footer>页面或章节的页眉 / 页脚
<main> / <nav>页面主体 / 导航区域
<article> / <section>可独立阅读的内容 / 按主题划分的章节
<aside>与主体相关的补充内容

<div> 是通用容器,默认另起一行。<span> 是行内容器,常用来包住一小段文字。标签能表达内容含义时,优先用语义标签。

表格

<table>
  <thead>
    <tr><th scope="col">项目</th><th scope="col">数量</th></tr>
  </thead>
  <tbody>
    <tr><td>笔记</td><td>3</td></tr>
  </tbody>
</table>
html

<tr> 表示一行,<th> 是表头单元格,<td> 是普通单元格。rowspan="2" 跨两行,colspan="2" 跨两列。合并后,省略被占用位置的单元格。

表单

<form action="/register" method="post">
  <label for="name">用户名</label>
  <input id="name" name="username" required>
  <label for="bio">简介</label>
  <textarea id="bio" name="bio" rows="3"></textarea>
  <button type="submit">提交</button>
</form>
html

action 指定提交地址,method 指定提交方式。name 是提交时的字段名。让 label 的 for 与控件的 id 相同,点击标签即可聚焦或切换控件。

控件常用写法
输入框<input> 可用 type 指定文本、密码等类型
单选框type="radio",同组使用相同的 name
复选框type="checkbox",可多选,checked 表示默认选中
下拉列表<select> 包住 <option>,selected 表示默认选项
文件上传type="file",multiple 允许多选。提交文件的表单用 method="post" 和 enctype="multipart/form-data"
按钮submit 提交,reset 恢复初始值,button 供自定义交互