php.cn
search
logo
  • Community
    • Articles
    • Topics
    • Q&A
  • Learn
    • Course
    • Programming Dictionary
  • Tools Library
    • Development tools
    • Website Source Code
    • PHP Libraries
    • JS special effects
    • Website Materials
    • Extension plug-ins
  • AI Tools
  • Leisure
    • Game Download
    • Game Tutorials
Log in
logo
简体中文zh-cnEnglishen繁体中文zh-tw日本語ja한국어koMelayumsFrançaisfrDeutschde
logo
  • Bad article
  • My page title
  • All news articles
  • Avatar
  • Best practices
close
HomeWeb Front-endH5 Tutorial前端编码风格规范之 HTML 规范

前端编码风格规范之 HTML 规范

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
May 17, 2016 am 09:07 AM
ht

英文原文:Web Styleguide - Style guide to harmonize HTML, Javascript and CSS / SASS coding style

HTML 规范

文档类型

推荐使用 HTML5 的文档类型申明: .

(建议使用 text/html 格式的 HTML。避免使用 XHTML。XHTML 以及它的属性,比如 application/xhtml+xml 在浏览器中的应用支持与优化空间都十分有限)。

HTML 中最好不要将无内容元素[1] 的标签闭合,例如:使用
而非
.


HTML 验证

一般情况下,建议使用能通过标准规范验证的 HTML 代码,除非在性能优化和控制文件大小上不得不做出让步。

使用诸如 W3C HTML validator 这样的工具来进行检测。

规范化的 HTML 是显现技术要求与局限的显著质量基线,它促进了 HTML 被更好地运用。

不推荐

  1. Test
  2. This is only a test.
复制代码


推荐



  1. Test
  2. This is only a test.
复制代码

省略可选标签

HTML5 规范中规定了 HTML 标签是可以省略的。但从可读性来说,在开发的源文件中最好不要这样做,因为省略标签可能会导致一些问题。

省略一些可选的标签确实使得页面大小减少,这很有用,尤其是对于一些大型网站来说。为了达到这一目的,我们可以在开发后期对页面进行压缩处理,在这个环节中这些可选的标签完全就可以省略掉了。

脚本加载

出于性能考虑,脚本异步加载很关键。一段脚本放置在 内,比如 ,其加载会一直阻塞 DOM 解析,直至它完全地加载和执行完毕。这会造成页面显示的延迟。特别是一些重量级的脚本,对用户体验来说那真是一个巨大的影响。

异步加载脚本可缓解这种性能影响。如果只需兼容 IE10+,可将 HTML5 的 async 属性加至脚本中,它可防止阻塞 DOM 的解析,甚至你可以将脚本引用写在 里也没有影响。

如需兼容老旧的浏览器,实践表明可使用用来动态注入脚本的脚本加载器。你可以考虑 yepnope 或 labjs。注入脚本的一个问题是:一直要等到 CSS 对象文档已就绪,它们才开始加载(短暂地在 CSS 加载完毕之后),这就对需要及时触发的 JS 造成了一定的延迟,这多多少少也影响了用户体验吧。

终上所述,兼容老旧浏览器(IE9-)时,应该遵循以下最佳实践。

脚本引用写在 body 结束标签之前,并带上 async 属性。这虽然在老旧浏览器中不会异步加载脚本,但它只阻塞了 body 结束标签之前的 DOM 解析,这就大大降低了其阻塞影响。而在现代浏览器中,脚本将在 DOM 解析器发现 body 尾部的 script 标签才进行加载,此时加载属于异步加载,不会阻塞 CSSOM(但其执行仍发生在 CSSOM 之后)。

所有浏览器中,推荐


  1.   
  2.    
  3.   
  4.   
  5.    

  6.    
  7.   
复制代码

只在现代浏览器中,推荐


  1.   
  2.    
  3.    
  4.   
  5.   
  6.    
  7.   
复制代码

语义化

根据元素(有时被错误地称作“标签”)其被创造出来时的初始意义来使用它。打个比方,用 heading 元素来定义头部标题,p 元素来定义文字段落,用 a 元素来定义链接锚点,等等。

有根据有目的地使用 HTML 元素,对于可访问性、代码重用、代码效率来说意义重大。

以下示例列出了一些的语义化 HTML 主要情况:

不推荐

  1. My page title

  2.   
    Home

  3.   
    News

  4.   
    About




  •   

  •    
    All news articles

  •    

  •       

    Bad article


  •       
    Introduction sub-title

  •       
    This is a very bad example for HTML semantics

  •       
    I think I'm more on the side and should not receive the main credits

  •       

  •         This article was created by David
    2014-01-01 00:00

  •       

  •    


  •    

  •       Related sections: Events, Public holidays
  •    

  •   




  •   Copyright 2014
  • 复制代码
    推荐



    1.   
    2.   

      My page title






    3.   
    4.   

    5.    
    6. Home

    7.    
    8. News

    9.    
    10. About

    11.   




    12.   
    13.   

    14.    
    15.    

    16.       
    17.       

      All news articles


    18.    

    19.    
    20.    

    21.       
    22.       

    23.         
    24.         
      Good article

    25.         
    26.         Introduction sub-title
    27.       

    28.       
    29.       

    30.         

      This is a good example for HTML semantics


    31.       

  •       
  •       

  •         

    I think I'm more on the side and should not receive the main credits


  •       
  •       
  •       

  •         
  •         

    This article was created by David 1 month ago


  •       
  •    

  •    
  •    

  •       

    Related sections: Events, Public holidays


  •    
  •   




  •   Copyright 2014
  • 复制代码
    多媒体回溯

    对页面上的媒体而言,像图片、视频、canvas 动画等,要确保其有可替代的接入接口。图片文件我们可采用有意义的备选文本(alt),视频和音频文件我们可以为其加上说明文字或字幕。

    提供可替代内容对可用性来说十分重要。试想,一位盲人用户如何能知晓一张图片是什么,要是没有 @alt 的话。

    (图片的 alt 属性是可不填写内容的,纯装饰性的图片就可用这么做:alt="")。

    不推荐

    1. 前端编码风格规范之 HTML 规范
    复制代码

    推荐

    1. Luke skywalker riding an alien horse
    复制代码

    尽量用 alt 标签去描述图片,设想你需要对于那些只能通过语音或者看不见图片的用户表达图片到底是什么。

    不推荐

    1. Header image
    复制代码

    推荐

    1. A huge spaceship that is approaching the earth
    复制代码

    关注点分离

    理解 web 中如何和为何区分不同的关注点,这很重要。这里的关注点主要指的是:信息(HTML 结构)、外观(CSS)和行为(JavaScript)。为了使它们成为可维护的干净整洁的代码,我们要尽可能的将它们分离开来。

    严格地保证结构、表现、行为三者分离,并尽量使三者之间没有太多的交互和联系。

    就是说,尽量在文档和模板中只包含结构性的 HTML;而将所有表现代码,移入样式表中;将所有动作行为,移入脚本之中。

    在此之外,为使得它们之间的联系尽可能的小,在文档和模板中也尽量少地引入样式和脚本文件。

    清晰的分层意味着:

    • 不使用超过一到两张样式表(i.e. main.css, vendor.css)
    • 不使用超过一到两个脚本(学会用合并脚本)
    • 不使用行内样式()
    • 不在元素上使用 style 属性(
      )
    • 不使用行内脚本(<script>alert('no good')</script>)
    • 不使用表象元素(i.e. , ,
      , , )
    • 不使用表象 class 名(i.e. red, left, center)

    不推荐




    1.   
    2.   
    3.   
    4.   


    5.   


    6.   I'm a subtitle and I'm bold!
    7.   
      Dare you center me!

    8.   <script><br> </script>
    9.     alert('Just dont...');
    10.   
    11.   
      I'm important!


    复制代码

    推荐




    1.   
    2.   


    3.   
    4.   


    5.   
    6.   
      I'm a subtitle and I'm bold!

    7.   
    8.   Dare you center me!
    9.   
    10.   
      I'm important!


    11.   
    12.   

    复制代码

    HTML 内容至上

    不要让非内容信息污染了你的 HTML。现在貌似有一种倾向:通过 HTML 来解决设计问题,这是显然是不对的。HTML 就应该只关注内容。

    HTML 标签的目的,就是为了不断地展示内容信息。

    • 不要引入一些特定的 HTML 结构来解决一些视觉设计问题
    • 不要将 img 元素当做专门用来做视觉设计的元素

    以下例子展示了误将 HTML 用来解决设计问题的这两种情况:

    不推荐



    1.   
    2.   See the square next to me?
    复制代码
    1. .text-box > .square {
    2.   display: inline-block;
    3.   width: 1rem;
    4.   height: 1rem;
    5.   background-color: red;
    6. }
    复制代码

    推荐



    1.   See the square next to me?
    复制代码

    1. /* We use a :before pseudo element to solve the design problem of placing a colored square in front of the text content */
    2. .text-box:before {
    3.   content: "";
    4.   display: inline-block;
    5.   width: 1rem;
    6.   height: 1rem;
    7.   background-color: red;
    8. }
    复制代码

    图片和 SVG 图形能被引入到 HTML 中的唯一理由是它们呈现出了与内容相关的一些信息。

    不推荐



    1.   Square
    2.   See the square next to me?
    复制代码

    推荐



    1.   See the square next to me?
    复制代码
    1. /* We use a :before pseudo element with a background image to solve the problem */
    2. .text-box:before {
    3.   content: "";
    4.   display: inline-block;
    5.   width: 1rem;
    6.   height: 1rem;
    7.   background: url(square.svg) no-repeat;
    8.   background-size: 100%;
    9. }
    复制代码

    Type 属性

    省略样式表与脚本上的 type 属性。鉴于 HTML5 中以上两者默认的 type 值就是 text/css 和 text/javascript,所以 type 属性一般是可以忽略掉的。甚至在老旧版本的浏览器中这么做也是安全可靠的。

    不推荐


    复制代码

    推荐


    复制代码

    可用性

    如果 HTML5 语义化标签使用得当,许多可用性问题已经引刃而解。ARIA 规则在一些语义化的元素上可为其添上默认的可用性角色属性,使用得当的话已使网站的可用性大部分成立。假如你使用 nav, aside, main, footer 等元素,ARIA 规则会在其上应用一些关联的默认值。 更多细节可参考 ARIA specification

    另外一些角色属性则能够用来呈现更多可用性情景(i.e. role="tab")。

    Tab Index 在可用性上的运用

    检查文档中的 tab 切换顺序并传值给元素上的 tabindex,这可以依据元素的重要性来重新排列其 tab 切换顺序。你可以设置 tabindex="-1" 在任何元素上来禁用其 tab 切换。

    当你在一个默认不可聚焦的元素上增加了功能,你应该总是为其加上 tabindex 属性使其变为可聚焦状态,而且这也会激活其 CSS 的伪类 :focus。选择合适的 tabindex 值,或是直接使用 tabindex="0" 将元素们组织成同一 tab 顺序水平,并强制干预其自然阅读顺序。

    微格式在 SEO 和可用性上的运用

    如果 SEO 和可用性环境条件允许的话,建议考虑采用微格式。微格式是通过在元素标签上申明一系列特定数据来达成特定语义的方法。

    谷歌、微软和雅虎对如何使用这些额外的数据一定程度上的达成一致,如果正确的使用,这将给搜索引擎优化带来巨大的好处。

    你可以访问 schema.org 获得更多内容细节。

    看一个电影网站的简单例子:

    不带微格式


    1. Avatar


    2. Director: James Cameron (born August 16, 1954)
    3. Science fiction
    4. Trailer
    复制代码

    带有微格式


    1.   

      Avatar


    2.   

    3.   Director: James Cameron (born August 16, 1954)
    4.   

  •   Science fiction
  •   Trailer
  • 复制代码
    ID 和锚点

    通常一个比较好的做法是将页面内所有的头部标题元素都加上 ID. 这样做,页面 URL 的 hash 中带上对应的 ID 名称,即形成描点,方便跳转至对应元素所处位置。

    打个比方,当你在浏览器中输入 URL http://your-site.com/about#best-practices,浏览器将定位至以下 H3 上。

    1. Best practices

    复制代码

    格式化规则

    在每一个块状元素,列表元素和表格元素后,加上一新空白行,并对其子孙元素进行缩进。内联元素写在一行内,块状元素还有列表和表格要另起一行。

    (如果由于换行的空格引发了不可预计的问题,那将所有元素并入一行也是可以接受的,格式警告总好过错误警告)。

    推荐


    1.   

      Space, the final frontier.





    2.   
    3. Moe

    4.   
    5. Larry

    6.   
    7. Curly




    8.   

    9.    

    10.       

    11.       

    12.    

    13.   

    14.   

    15.    

    16.       

    17.       

    18.    

    19.   

    20. Income Taxes
      $ 5.00 $ 4.50
    复制代码

    HTML 引号

    使用双引号(“”) 而不是单引号(“) 。

    不推荐

    复制代码

    推荐

    复制代码

    [1]: 此处的空白元素指的是以下元素:area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr

    via:http://roshanca.com/2014/web-develop-styleguide-html/
    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Related Article
    Mastering Microdata: A Step-by-Step Guide for HTML5Mastering Microdata: A Step-by-Step Guide for HTML5May 14, 2025 am 12:07 AM

    MicrodatainHTML5enhancesSEOanduserexperiencebyprovidingstructureddatatosearchengines.1)Useitemscope,itemtype,anditempropattributestomarkupcontentlikeproductsorevents.2)TestmicrodatawithtoolslikeGoogle'sStructuredDataTestingTool.3)ConsiderusingJSON-LD

    What's New in HTML5 Forms? Exploring the New Input TypesWhat's New in HTML5 Forms? Exploring the New Input TypesMay 13, 2025 pm 03:45 PM

    HTML5introducesnewinputtypesthatenhanceuserexperience,simplifydevelopment,andimproveaccessibility.1)automaticallyvalidatesemailformat.2)optimizesformobilewithanumerickeypad.3)andsimplifydateandtimeinputs,reducingtheneedforcustomsolutions.

    Understanding H5: The Meaning and SignificanceUnderstanding H5: The Meaning and SignificanceMay 11, 2025 am 12:19 AM

    H5 is HTML5, the fifth version of HTML. HTML5 improves the expressiveness and interactivity of web pages, introduces new features such as semantic tags, multimedia support, offline storage and Canvas drawing, and promotes the development of Web technology.

    H5: Accessibility and Web Standards ComplianceH5: Accessibility and Web Standards ComplianceMay 10, 2025 am 12:21 AM

    Accessibility and compliance with network standards are essential to the website. 1) Accessibility ensures that all users have equal access to the website, 2) Network standards follow to improve accessibility and consistency of the website, 3) Accessibility requires the use of semantic HTML, keyboard navigation, color contrast and alternative text, 4) Following these principles is not only a moral and legal requirement, but also amplifying user base.

    What is the H5 tag in HTML?What is the H5 tag in HTML?May 09, 2025 am 12:11 AM

    The H5 tag in HTML is a fifth-level title that is used to tag smaller titles or sub-titles. 1) The H5 tag helps refine content hierarchy and improve readability and SEO. 2) Combined with CSS, you can customize the style to enhance the visual effect. 3) Use H5 tags reasonably to avoid abuse and ensure the logical content structure.

    H5 Code: A Beginner's Guide to Web StructureH5 Code: A Beginner's Guide to Web StructureMay 08, 2025 am 12:15 AM

    The methods of building a website in HTML5 include: 1. Use semantic tags to define the web page structure, such as, , etc.; 2. Embed multimedia content, use and tags; 3. Apply advanced functions such as form verification and local storage. Through these steps, you can create a modern web page with clear structure and rich features.

    H5 Code Structure: Organizing Content for ReadabilityH5 Code Structure: Organizing Content for ReadabilityMay 07, 2025 am 12:06 AM

    A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

    H5 vs. Older HTML Versions: A ComparisonH5 vs. Older HTML Versions: A ComparisonMay 06, 2025 am 12:09 AM

    The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Show More

    Hot Article

    How to fix KB5055612 fails to install in Windows 10?
    1 months agoByDDD
    Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Roblox: Grow A Garden - Complete Mutation Guide
    3 weeks agoByDDD
    Nordhold: Fusion System, Explained
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Show More

    Hot Tools

    VSCode Windows 64-bit Download

    VSCode Windows 64-bit Download

    A free and powerful IDE editor launched by Microsoft

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    Show More

    Hot Topics

    Java Tutorial
    1676
    14
    CakePHP Tutorial
    1429
    52
    Laravel Tutorial
    1333
    25
    PHP Tutorial
    1278
    29
    C# Tutorial
    1257
    24
    Show More

    Public welfare online PHP training,Help PHP learners grow quickly!

    About usDisclaimerSitemap

    © php.cn All rights reserved