久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

前端編碼風格規范之 HTML 規范

一般情況下,建議使用能通過標準規范驗證的 HTML 代碼,除非在性能優化和控制文件大小上不得不做出讓步。HTML 規范文檔類型推薦使用 HTML5 的文檔類型申明: !DOCTYPE html.(建議使用
英文原文:Web Styleguide - Style guide to harmonize HTML, Javascript and CSS / SASS coding style

HTML 規范

文檔類型

推薦使用 HTML5 的文檔類型申明: <!DOCTYPE html>.

(建議使用 text/html 格式的 HTML。避免使用 XHTML。XHTML 以及它的屬性,比如 application/xhtml+xml 在瀏覽器中的應用支持與優化空間都十分有限)。

HTML 中最好不要將無內容元素[1] 的標簽閉合,例如:使用 <br> 而非 <br />.

HTML 驗證

一般情況下,建議使用能通過標準規范驗證的 HTML 代碼,除非在性能優化和控制文件大小上不得不做出讓步。

使用諸如 W3C HTML validator 這樣的工具來進行檢測。

規范化的 HTML 是顯現技術要求與局限的顯著質量基線,它促進了 HTML 被更好地運用。

不推薦

  1. <title>Test</title>
  2. <article>This is only a test.
復制代碼


推薦

  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <title>Test</title>
  4. <article>This is only a test.</article>
復制代碼

省略可選標簽

HTML5 規范中規定了 HTML 標簽是可以省略的。但從可讀性來說,在開發的源文件中最好不要這樣做,因為省略標簽可能會導致一些問題。

省略一些可選的標簽確實使得頁面大小減少,這很有用,尤其是對于一些大型網站來說。為了達到這一目的,我們可以在開發后期對頁面進行壓縮處理,在這個環節中這些可選的標簽完全就可以省略掉了。

腳本加載

出于性能考慮,腳本異步加載很關鍵。一段腳本放置在 <head> 內,比如 <script src="main.js"></script>,其加載會一直阻塞 DOM 解析,直至它完全地加載和執行完畢。這會造成頁面顯示的延遲。特別是一些重量級的腳本,對用戶體驗來說那真是一個巨大的影響。

異步加載腳本可緩解這種性能影響。如果只需兼容 IE10+,可將 HTML5 的 async 屬性加至腳本中,它可防止阻塞 DOM 的解析,甚至你可以將腳本引用寫在 <head> 里也沒有影響。

如需兼容老舊的瀏覽器,實踐表明可使用用來動態注入腳本的腳本加載器。你可以考慮 yepnopelabjs。注入腳本的一個問題是:一直要等到 CSS 對象文檔已就緒,它們才開始加載(短暫地在 CSS 加載完畢之后),這就對需要及時觸發的 JS 造成了一定的延遲,這多多少少也影響了用戶體驗吧。

終上所述,兼容老舊瀏覽器(IE9-)時,應該遵循以下最佳實踐。

腳本引用寫在 body 結束標簽之前,并帶上 async 屬性。這雖然在老舊瀏覽器中不會異步加載腳本,但它只阻塞了 body 結束標簽之前的 DOM 解析,這就大大降低了其阻塞影響。而在現代瀏覽器中,腳本將在 DOM 解析器發現 body 尾部的 script 標簽才進行加載,此時加載屬于異步加載,不會阻塞 CSSOM(但其執行仍發生在 CSSOM 之后)。

所有瀏覽器中,推薦

  1. <html>
  2.   <head>
  3.     <link rel="stylesheet" href="main.css">
  4.   </head>
  5.   <body>
  6.     <!-- body goes here -->

  7.     <script src="main.js" async></script>
  8.   </body>
  9. </html>
復制代碼

只在現代瀏覽器中,推薦

  1. <html>
  2.   <head>
  3.     <link rel="stylesheet" href="main.css">
  4.     <script src="main.js" async></script>
  5.   </head>
  6.   <body>
  7.     <!-- body goes here -->
  8.   </body>
  9. </html>
復制代碼

語義化

根據元素(有時被錯誤地稱作“標簽”)其被創造出來時的初始意義來使用它。打個比方,用 heading 元素來定義頭部標題,p 元素來定義文字段落,用 a 元素來定義鏈接錨點,等等。

有根據有目的地使用 HTML 元素,對于可訪問性、代碼重用、代碼效率來說意義重大。

以下示例列出了一些的語義化 HTML 主要情況:

不推薦

  1. <b>My page title</b>
  2. <div class="top-navigation">
  3.   <div class="nav-item"><a href="#home">Home</a></div>
  4.   <div class="nav-item"><a href="#news">News</a></div>
  5.   <div class="nav-item"><a href="#about">About</a></div>
  6. </div>

  7. <div class="news-page">
  8.   <div class="page-section news">
  9.     <div class="title">All news articles</div>
  10.     <div class="news-article">
  11.       <h2>Bad article</h2>
  12.       <div class="intro">Introduction sub-title</div>
  13.       <div class="content">This is a very bad example for HTML semantics</div>
  14.       <div class="article-side-notes">I think I'm more on the side and should not receive the main credits</div>
  15.       <div class="article-foot-notes">
  16.         This article was created by David <div class="time">2014-01-01 00:00</div>
  17.       </div>
  18.     </div>

  19.     <div class="section-footer">
  20.       Related sections: Events, Public holidays
  21.     </div>
  22.   </div>
  23. </div>

  24. <div class="page-footer">
  25.   Copyright 2014
  26. </div>
復制代碼

推薦

  1. <!-- The page header should go into a header element -->
  2. <header>
  3.   <!-- As this title belongs to the page structure it's a heading and h1 should be used -->
  4.   <h1>My page title</h1>
  5. </header>

  6. <!-- All navigation should go into a nav element -->
  7. <nav class="top-navigation">
  8.   <!-- A listing of elements should always go to UL (OL for ordered listings) -->
  9.   <ul>
  10.     <li class="nav-item"><a href="#home">Home</a></li>
  11.     <li class="nav-item"><a href="#news">News</a></li>
  12.     <li class="nav-item"><a href="#about">About</a></li>
  13.   </ul>
  14. </nav>

  15. <!-- The main part of the page should go into a main element (also use role="main" for accessibility) -->
  16. <main class="news-page" role="main">
  17.   <!-- A section of a page should go into a section element. Divide a page into sections with semantic elements. -->
  18.   <section class="page-section news">
  19.     <!-- A section header should go into a section element -->
  20.     <header>
  21.       <!-- As a page section belongs to the page structure heading elements should be used (in this case h2) -->
  22.       <h2 class="title">All news articles</h2>
  23.     </header>

  24.     <!-- If a section / module can be seen as an article (news article, blog entry, products teaser, any other
  25.      re-usable module / section that can occur multiple times on a page) a article element should be used -->
  26.     <article class="news-article">
  27.       <!-- An article can contain a header that contains the summary / introduction information of the article -->
  28.       <header>
  29.         <!-- As a article title does not belong to the overall page structure there should not be any heading tag! -->
  30.         <div class="article-title">Good article</div>
  31.         <!-- Small can optionally be used to reduce importance -->
  32.         <small class="intro">Introduction sub-title</small>
  33.       </header>

  34.       <!-- For the main content in a section or article there is no semantic element -->
  35.       <div class="content">
  36.         <p>This is a good example for HTML semantics</p>
  37.       </div>
  38.       <!-- For content that is represented as side note or less important information in a given context use aside -->
  39.       <aside class="article-side-notes">
  40.         <p>I think I'm more on the side and should not receive the main credits</p>
  41.       </aside>
  42.       <!-- Articles can also contain footers. If you have footnotes for an article place them into a footer element -->
  43.       <footer class="article-foot-notes">
  44.         <!-- The time element can be used to annotate a timestamp. Use the datetime attribute to specify ISO time
  45.          while the actual text in the time element can also be more human readable / relative -->
  46.         <p>This article was created by David <time datetime="2014-01-01 00:00" class="time">1 month ago</time></p>
  47.       </footer>
  48.     </article>

  49.     <!-- In a section, footnotes or similar information can also go into a footer element -->
  50.     <footer class="section-footer">
  51.       <p>Related sections: Events, Public holidays</p>
  52.     </footer>
  53.   </section>
  54. </main>

  55. <!-- Your page footer should go into a global footer element -->
  56. <footer class="page-footer">
  57.   Copyright 2014
  58. </footer>
復制代碼

多媒體回溯

對頁面上的媒體而言,像圖片、視頻、canvas 動畫等,要確保其有可替代的接入接口。圖片文件我們可采用有意義的備選文本(alt),視頻和音頻文件我們可以為其加上說明文字或字幕。

提供可替代內容對可用性來說十分重要。試想,一位盲人用戶如何能知曉一張圖片是什么,要是沒有 @alt 的話。

(圖片的 alt 屬性是可不填寫內容的,純裝飾性的圖片就可用這么做:alt="")。

不推薦

  1. <img src="luke-skywalker.jpg">
復制代碼

推薦

  1. <img src="luke-skywalker.jpg" alt="Luke skywalker riding an alien horse">
復制代碼

盡量用 alt 標簽去描述圖片,設想你需要對于那些只能通過語音或者看不見圖片的用戶表達圖片到底是什么。

不推薦

  1. <img src="huge-spaceship-approaching-earth.jpg" alt="Header image">
復制代碼

推薦

  1. <img src="huge-spaceship-approaching-earth.jpg" alt="A huge spaceship that is approaching the earth">
復制代碼

關注點分離

理解 web 中如何和為何區分不同的關注點,這很重要。這里的關注點主要指的是:信息(HTML 結構)、外觀(CSS)和行為(JavaScript)。為了使它們成為可維護的干凈整潔的代碼,我們要盡可能的將它們分離開來。

嚴格地保證結構、表現、行為三者分離,并盡量使三者之間沒有太多的交互和聯系。

就是說,盡量在文檔和模板中只包含結構性的 HTML;而將所有表現代碼,移入樣式表中;將所有動作行為,移入腳本之中。

在此之外,為使得它們之間的聯系盡可能的小,在文檔和模板中也盡量少地引入樣式和腳本文件。

清晰的分層意味著:

  • 不使用超過一到兩張樣式表(i.e. main.css, vendor.css)
  • 不使用超過一到兩個腳本(學會用合并腳本)
  • 不使用行內樣式(<style>.no-good {}</style>)
  • 不在元素上使用 style 屬性(<hr style="border-top: 5px solid black">)
  • 不使用行內腳本(<script>alert('no good')</script>)
  • 不使用表象元素(i.e. <b>, <u>, <center>, <font>, <b>)
  • 不使用表象 class 名(i.e. red, left, center)

不推薦

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <link rel="stylesheet" href="base.css">
  5.   <link rel="stylesheet" href="grid.css">
  6.   <link rel="stylesheet" href="type.css">
  7.   <link rel="stylesheet" href="modules/teaser.css">
  8. </head>
  9. <body>
  10.   <h1 style="font-size: 3rem"></h1>
  11.   <b>I'm a subtitle and I'm bold!</b>
  12.   <center>Dare you center me!</center>
  13.   <script>
  14.     alert('Just dont...');
  15.   </script>
  16.   <div class="red">I'm important!</div>
  17. </body>
  18. </html>
復制代碼

推薦

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <!-- Concatinate your style sheets into a single one -->
  5.   <link rel="stylesheet" href="main.css">
  6. </head>
  7. <body>
  8.   <!-- Don't use style attributes but assign sensible classes and apply styles in the stylesheet -->
  9.   <h1 class="title"></h1>
  10.   <!-- Don't use presentational elements and assign sensible classes -->
  11.   <div class="sub-title">I'm a subtitle and I'm bold!</div>
  12.   <!-- Maybe your comments get centered in your presentation but that decision is up to the stylesheet -->
  13.   <span class="comment">Dare you center me!</span>
  14.   <!-- You wanted to make it red because it's important so then also name the class important and decide in the stylesheet
  15.    what you want to do with it -->
  16.   <div class="important">I'm important!</div>

  17.   <!-- Put all your scripts into files and concatinate them into a single one -->
  18.   <script async src="main.js"></script>
  19. </body>
  20. </html>
復制代碼

HTML 內容至上

不要讓非內容信息污染了你的 HTML。現在貌似有一種傾向:通過 HTML 來解決設計問題,這是顯然是不對的。HTML 就應該只關注內容。

HTML 標簽的目的,就是為了不斷地展示內容信息。

  • 不要引入一些特定的 HTML 結構來解決一些視覺設計問題
  • 不要將 img 元素當做專門用來做視覺設計的元素

以下例子展示了誤將 HTML 用來解決設計問題的這兩種情況:

不推薦

  1. <!-- We should not introduce an additional element just to solve a design problem  -->
  2. <span class="text-box">
  3.   <span class="square"></span>
  4.   See the square next to me?
  5. </span>
復制代碼
  1. .text-box > .square {
  2.   display: inline-block;
  3.   width: 1rem;
  4.   height: 1rem;
  5.   background-color: red;
  6. }
復制代碼

推薦

  1. <!-- That's clean markup! -->
  2. <span class="text-box">
  3.   See the square next to me?
  4. </span>
復制代碼

  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. <!-- Content images should never be used for design elements!  -->
  2. <span class="text-box">
  3.   <img src="square.svg" alt="Square" />
  4.   See the square next to me?
  5. </span>
復制代碼

推薦

  1. <!-- That's clean markup! -->
  2. <span class="text-box">
  3.   See the square next to me?
  4. </span>
復制代碼
  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 屬性一般是可以忽略掉的。甚至在老舊版本的瀏覽器中這么做也是安全可靠的。

不推薦

  1. <link rel="stylesheet" href="main.css" type="text/css">
  2. <script src="main.js" type="text/javascript"></script>
復制代碼

推薦

  1. <link rel="stylesheet" href="main.css">
  2. <script src="main.js"></script>
復制代碼

可用性

如果 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. <div>
  2. <h1>Avatar</h1>
  3. <span>Director: James Cameron (born August 16, 1954)</span>
  4. <span>Science fiction</span>
  5. <a href="../**avatar-theatrical-trailer.html">Trailer</a>
  6. </div>
復制代碼

帶有微格式

  1. <div itemscope itemtype ="http://schema.org/Movie">
  2.   <h1 itemprop="name">Avatar</h1>
  3.   <div itemprop="director" itemscope itemtype="http://schema.org/Person">
  4.   Director: <span itemprop="name">James Cameron</span> (born <span itemprop="birthDate">August 16, 1954)</span>
  5.   </div>
  6.   <span itemprop="genre">Science fiction</span>
  7.   <a href="../**avatar-theatrical-trailer.html" itemprop="trailer">Trailer</a>
  8. </div>
復制代碼

ID 和錨點

通常一個比較好的做法是將頁面內所有的頭部標題元素都加上 ID. 這樣做,頁面 URL 的 hash 中帶上對應的 ID 名稱,即形成描點,方便跳轉至對應元素所處位置。

打個比方,當你在瀏覽器中輸入 URL http://your-site.com/about#best-practices,瀏覽器將定位至以下 H3 上。

  1. <h3 id="best-practices">Best practices</h3>
復制代碼

格式化規則

在每一個塊狀元素,列表元素和表格元素后,加上一新空白行,并對其子孫元素進行縮進。內聯元素寫在一行內,塊狀元素還有列表和表格要另起一行。

(如果由于換行的空格引發了不可預計的問題,那將所有元素并入一行也是可以接受的,格式警告總好過錯誤警告)。

推薦

  1. <blockquote>
  2.   <p><em>Space</em>, the final frontier.</p>
  3. </blockquote>

  4. <ul>
  5.   <li>Moe</li>
  6.   <li>Larry</li>
  7.   <li>Curly</li>
  8. </ul>

  9. <table>
  10.   <thead>
  11.     <tr>
  12.       <th scope="col">Income</th>
  13.       <th scope="col">Taxes</th>
  14.     </tr>
  15.   </thead>
  16.   <tbody>
  17.     <tr>
  18.       <td>$ 5.00</td>
  19.       <td>$ 4.50</td>
  20.     </tr>
  21.   </tbody>
  22. </table>
復制代碼

HTML 引號

使用雙引號(“”) 而不是單引號(“) 。

不推薦

  1. <div class='news-article'></div>
復制代碼

推薦

  1. <div class="news-article"></div>
復制代碼

[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/
【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。

相關文檔推薦

由于實際運行環境是在瀏覽器中,因此性能還取決于JavaScript解釋器的效率,指定的FPS幀速在低性能解釋器中可能不會達到,所以這部分不是開發者能夠決定的,開發者能作的是盡可能通
本文將使用HTML5提供的VideoAPI做一個自定義的視頻播放器,需要用到HTML5提供的video標簽、以及HTML5提供的對JavascriptAPI的擴展。,HTML5中國,中國最大的HTML5中文門戶。
隨著 Hybrid 應用的豐富,HTML5 工程師們已經不滿足于把桌面端體驗簡單移植到移動端,他們覬覦移動原生應用人性化的操作體驗,特別是原生應用與生俱來的豐富的手勢系統。HTML5 沒有提
你想要在自己網站上分享一個產品,或者是一個作品集,又或者僅僅只是一個靈感。在你發布到網上之前,你想讓它看起來有吸引力,專業,或者至少得看起來像那么回事。那么你接下
H5廣告,包括H5廣告的設計流程,究竟有什么講究,和階段。為了能幫助更多的人了解H5廣告,我專門做了一個講義。同時,也讓我意外的收到了非常好反饋和認!這是對我的極大鼓勵!我的
本文主要內容有:框架與組件、構建生態、開發技巧與調試、html、css與重構、native/hybrid/桌面開發、前端/H5優化、全棧/全端開發、研究實驗、數據分析與監控、其它軟技能、前端技術網
主站蜘蛛池模板: 在线精品一区二区三区 | 午夜精品视频在线观看 | 天天操网 | 国产成人99久久亚洲综合精品 | 国产农村妇女精品一区 | 亚洲精品中文字幕在线观看 | 伊人精品国产 | 夜夜爽99久久国产综合精品女不卡 | 欧美日韩中文字幕在线 | 亚洲性爰 | 午夜小视频在线观看 | 日韩免费福利视频 | 午夜成人在线视频 | 国产成人精品一区二区 | 亚洲欧美视频在线观看 | 日韩午夜在线播放 | 国产精品欧美一区二区三区不卡 | 国产精品精品久久久 | 亚洲小视频在线观看 | 91 久久 | 国产精品自拍视频网站 | 电影午夜精品一区二区三区 | 黄色成人av| 成人精品毛片国产亚洲av十九禁 | 成人网址在线观看 | 国产美女视频 | 91av免费版 | 一区二区三区日本 | 91精品国产色综合久久 | 日本福利视频免费观看 | 91视频网址 | 久久国产精品99久久久大便 | 久久久久久久久久久久久久久久久久久久 | 国产ts人妖一区二区三区 | 久草在线在线精品观看 | 成人国产在线视频 | 亚洲视频免费在线 | 亚洲视频在线播放 | 成人免费大片黄在线播放 | 成人免费视频网站在线观看 | 国产精品九九九 |