Contact 表单 Mockup
你将构建什么
`contact.HTML` file 使用表单包含 labeled name (文本), email (email 类型), and 消息 (textarea) 字段, plus 提交 button. 表单是 accessible 但做 不提交 anywhere.
学习目标
- Use 表单元素到 分组 related 输入
- Associate 每个输入使用 label using for/id
- 选择正确输入类型 (文本, email, textarea) for 每个字段
- 添加必需 attribute 到 mark mandatory 字段
前置要求
- HTML document 结构
- Headings and paragraphs
步骤
- 1
设置 page and 表单 shell
创建
contact.HTML使用 heading and 空表单. 表单元素是 容器那个分组所有输入; 操作 and 方法能 是左 off for mockup.代码<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Contact</title> </head> <body> <h1>Contact me</h1> <form></form> </body> </html>预期结果
page 使用 heading and 空表单容器.
- 2
添加 name 字段
内部表单, 添加 label and 输入. label's for attribute 必须 match 输入's id — 这个是 什么制作字段 accessible 到 screen readers and 制作点击 label focus 输入.
代码<label for="name">Name</label> <input type="text" id="name" name="name" required>预期结果
文本字段使用 clickable label; 提交空 shows 浏览器 validation 消息.
提示
必需 attribute 触发构建-in 浏览器 validation — 否 JS 需要.
- 3
添加 email 字段
添加其次 label/输入 pair, 这个时间使用类型="email". email 类型 gives 用户 mobile-friendly keyboard and 构建-in 格式 validation for free.
代码<label for="email">Email</label> <input type="email" id="email" name="email" required>预期结果
email 字段那个拒绝非-email 输入 on 提交.
- 4
添加消息 textarea and 提交 button
Use textarea (不输入) for multi-行消息文本. Finish 使用 button 类型="submit". button 文本描述操作 — avoid generic "Submit" 当某事 clearer fits.
代码<label for="message">Message</label> <textarea id="message" name="message" rows="4" required></textarea> <button type="submit">Send message</button>预期结果
multi-行消息字段 and 提交 button 完整表单.
- 5
添加 minimal 布局 CSS
Stack 字段 vertically 使用小 样式块. 表单字段是 easier 到 fill out 当每个 label sits above 它的输入使用 consistent spacing.
代码form { display: flex; flex-direction: column; gap: 0.75rem; max-width: 24rem; } label { font-weight: 600; }预期结果
表单字段 stack neatly 使用 consistent spacing.
自己动手试试
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact</title>
</head>
<body>
<h1>Contact me</h1>
<form>
<!-- TODO: name, email, message, submit -->
</form>
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact</title>
<style>
form { display: flex; flex-direction: column; gap: 0.75rem; max-width: 24rem; }
label { font-weight: 600; }
</style>
</head>
<body>
<h1>Contact me</h1>
<form>
<label for="name">Name</label>
<input type="text" id="name" name="name" required>
<label for="email">Email</label>
<input type="email" id="email" name="email" required>
<label for="message">Message</label>
<textarea id="message" name="message" rows="4" required></textarea>
<button type="submit">Send message</button>
</form>
</body>
</html>你学到了什么
你构建 accessible contact 表单使用 properly associated labels, 正确输入类型, 构建-in validation, and 提交 button — 相同模式每个真实表单 uses, 之前任何 backend 是添加.
相关资源
相关练习
- 表单元素 MCQ
测验 on 选择正确输入类型 and 元素.
- Accessibility MCQ
Label 关联 and 必需字段 — accessibility 基础 used 这里.
相关工具
- HTML/CSS/JS Minifier
格式你的表单 markup 到验证结构.
相关速查表
- HTML Cheatsheet
引用 for 表单, label, 输入, textarea, and button.
相关参考文档
- HTML 元素引用
引用 for 表单, label, 输入, textarea, and button.
相关术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
相关错误
用大白话解释常见错误——什么意思、为什么发生、如何修复。