处女贴,问个很浅薄也很困扰的问题吧
发现官方草案及答疑中说明即自关闭的斜线(/)对空标签无效,普通HTML标签不要自闭合才是标准的写法,那么我们是否还应该闭合html5中的空标签?
看了下SF的源码,也并不是所有的单标签都闭合了:)
Then, if the element is one of the void elements, or if the element is a foreign element, then there may be a single U+002F SOLIDUS character (/). This character has no effect on void elements, but on foreign elements it marks the start tag as self-closing.
Void elements in HTML (e.g. the br, img and input elements) do not require a trailing slash. e.g. Instead of writing, you only need to write. This is the same as in HTML4. However, due to the widespread attempts to use XHTML1, there are a significant number of pages using the trailing slash. Because of this, the trailing slash syntax has been permitted on void elements in HTML in order to ease migration from XHTML1 back to HTML.
怪我咯2017-04-17 11:09:00
XHTML的設計者認為,應當讓HTML頁麵嚴格符合XML,使得HTML真正成為XML的一個子集,從而提倡了和XML一致的自閉合標簽。
不得不說XHTML在促進程序員規範語義上,起到了巨大的曆史作用。但在結果上,實際寫出的XHTML頁麵沒有幾個真的送進XML解析器不報錯的。所以到了現在HTML5時代,對XML的執著終究被廢棄了,XHTML當初的設計目標被Web標準的設計者自己挖個坑給埋了。
現在的原則是:隻要標簽沒有二義性,愛咋整咋整。對於標準中所說的void elements,愛閉合不閉合,不必執著怎麼做更“標準”——這在當前的思想下,是個不必要的負擔。
巴扎黑2017-04-17 11:09:00
這個網站有的閉合,有的沒閉合,可能不是一個人寫的,不然不會產生兩種風格。
關於空標簽閉合,我偏向Google Style Guide中建議的不閉合,在這方麵我有點偷懶,覺得Google的標準就是業界今後的標準。
有點完美主義,以前力求閉合,現在力求不閉合。