html
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseHTML Development Guidelines
HTML开发指南
Core Principles
核心原则
- Write semantic HTML to improve accessibility and SEO
- Use appropriate elements for their intended purpose
- Ensure proper document structure and hierarchy
- Prioritize accessibility by using ARIA roles and attributes when needed
- 编写语义化HTML以提升无障碍性和SEO
- 使用符合预期用途的恰当元素
- 确保正确的文档结构和层级
- 在需要时使用ARIA角色和属性,优先保障无障碍性
Semantic Elements
语义化元素
Document Structure
文档结构
- Use for introductory content and navigation
<header> - Use for the primary content (one per page)
<main> - Use for footer content
<footer> - Use for navigation sections
<nav> - Use for tangentially related content
<aside>
- 使用承载介绍性内容和导航
<header> - 使用承载主要内容(每页一个)
<main> - 使用承载页脚内容
<footer> - 使用承载导航区域
<nav> - 使用承载次要相关内容
<aside>
Content Sections
内容区块
- Use for self-contained, distributable content
<article> - Use for thematic grouping of content
<section> - Use appropriate heading hierarchy (through
<h1>)<h6> - Only one per page, representing the main topic
<h1>
- 使用承载独立可分发的内容
<article> - 使用承载主题性内容分组
<section> - 使用恰当的标题层级(至
<h1>)<h6> - 每页仅保留一个,代表页面核心主题
<h1>
Text Content
文本内容
- Use for paragraphs
<p> - Use ,
<ul>,<ol>for lists appropriately<dl> - Use for quotations
<blockquote> - Use and
<figure>for illustrations<figcaption>
- 使用承载段落
<p> - 根据需求恰当使用、
<ul>、<ol>列表<dl> - 使用承载引用内容
<blockquote> - 使用和
<figure>承载插图及说明<figcaption>
Interactive Elements
交互元素
Buttons and Links
按钮与链接
- Use for clickable actions that don't navigate
<button> - Use for links that navigate to URLs
<a> - Always include attribute on links
href - Never use or
<div>for clickable elements<span>
- 使用实现不涉及页面跳转的可点击操作
<button> - 使用实现跳转到URL的链接
<a> - 链接必须包含属性
href - 切勿使用或
<div>实现可点击元素<span>
Forms
表单
- Use with proper
<form>andactionattributesmethod - Associate labels with inputs using attribute
for - Use appropriate input types (,
email,tel, etc.)number - Group related inputs with and
<fieldset><legend> - Include validation attributes (,
required,pattern,min)max
- 使用带有正确和
action属性的method<form> - 通过属性将标签与输入框关联
for - 使用恰当的输入类型(、
email、tel等)number - 使用和
<fieldset>对相关输入框进行分组<legend> - 包含验证属性(、
required、pattern、min)max
Media Elements
媒体元素
Images
图片
- Always include attribute for images
alt - Use descriptive alt text for informational images
- Use empty for decorative images
alt="" - Use and
srcsetfor responsive imagessizes - Include and
widthto prevent layout shiftheight
- 图片必须包含属性
alt - 为信息类图片添加描述性替代文本
- 为装饰性图片设置空值
alt="" - 使用和
srcset实现响应式图片sizes - 包含和
width属性以避免布局偏移height
Video and Audio
视频与音频
- Provide captions and transcripts
- Include multiple source formats for compatibility
- Use attribute for video thumbnails
poster - Consider autoplay impact on user experience
- 提供字幕和文字转录
- 包含多种源格式以保证兼容性
- 为视频设置属性指定缩略图
poster - 考虑自动播放对用户体验的影响
Accessibility (ARIA)
无障碍访问(ARIA)
When to Use ARIA
ARIA的使用场景
- Use ARIA when native HTML semantics are insufficient
- Prefer native HTML elements over ARIA when possible
- Ensure ARIA attributes match actual element behavior
- 当原生HTML语义不足以满足需求时使用ARIA
- 优先使用原生HTML元素而非ARIA
- 确保ARIA属性与元素实际行为匹配
Common ARIA Attributes
常用ARIA属性
- for accessible names
aria-label - for additional descriptions
aria-describedby - for decorative elements
aria-hidden - for expandable sections
aria-expanded - for dynamic content updates
aria-live
- :设置无障碍名称
aria-label - :添加额外描述
aria-describedby - :标记装饰性元素
aria-hidden - :标记可展开区域
aria-expanded - :标记动态内容更新区域
aria-live
Landmarks
地标角色
- Use landmark roles to define page regions
- Ensure all content is within a landmark
- Use ,
role="main", etc., only when semantic elements aren't availablerole="navigation"
- 使用地标角色定义页面区域
- 确保所有内容都包含在地标区域内
- 仅当语义化元素不可用时,才使用、
role="main"等地标角色role="navigation"
Best Practices
最佳实践
Document Meta
文档元数据
- Include proper declaration
<!DOCTYPE html> - Set language attribute on element
<html> - Include viewport meta tag for responsive design
- Use appropriate meta tags for SEO
- 包含正确的声明
<!DOCTYPE html> - 在元素上设置语言属性
<html> - 添加视口元标签以实现响应式设计
- 使用恰当的元标签优化SEO
Code Quality
代码质量
- Validate HTML using W3C validator
- Maintain consistent indentation
- Close all tags properly (including self-closing)
- Use lowercase for element names and attributes
- 使用W3C验证工具校验HTML
- 保持一致的缩进格式
- 正确闭合所有标签(包括自闭合标签)
- 元素名称和属性使用小写
Deprecated Elements
已废弃元素
- Avoid ,
<font>,<center>,<b>for styling<i> - Use CSS for visual presentation
- Use and
<strong>for semantic emphasis<em>
- 避免使用、
<font>、<center>、<b>进行样式设置<i> - 使用CSS实现视觉呈现
- 使用和
<strong>实现语义化强调<em>
HTMX Integration
HTMX集成
When using HTMX for interactivity:
- Use ,
hx-getattributes for server requestshx-post - Leverage to specify update targets
hx-target - Use to control how content is inserted
hx-swap - Implement for critical actions
hx-confirm - Use for URL updates without full refresh
hx-push-url
使用HTMX实现交互时:
- 使用、
hx-get属性发起服务器请求hx-post - 利用指定内容更新目标
hx-target - 使用控制内容插入方式
hx-swap - 为关键操作实现确认机制
hx-confirm - 使用实现无全页刷新的URL更新
hx-push-url
Bootstrap Integration
Bootstrap集成
When using Bootstrap:
- Leverage Bootstrap's grid system for layouts
- Use Bootstrap components with proper markup structure
- Include necessary ARIA attributes with components
- Use Bootstrap's utility classes for spacing and typography
- Minimize custom CSS by using Bootstrap classes
使用Bootstrap时:
- 利用Bootstrap栅格系统构建布局
- 按照正确的标记结构使用Bootstrap组件
- 为组件添加必要的ARIA属性
- 使用Bootstrap工具类实现间距和排版
- 优先使用Bootstrap类以减少自定义CSS的编写
Performance
性能优化
- Minimize DOM depth and complexity
- Load scripts with or
deferwhen appropriateasync - Use for below-fold images
loading="lazy" - Preload critical resources with
<link rel="preload">
- 最小化DOM深度和复杂度
- 适时使用或
defer加载脚本async - 为首屏外图片设置实现懒加载
loading="lazy" - 使用预加载关键资源
<link rel="preload">