关闭HTML标记

san*_*nta 5 html xhtml

什么时候使用结束标签和斜线是否合适?

<div></div>
Run Code Online (Sandbox Code Playgroud)

<div />
Run Code Online (Sandbox Code Playgroud)

小智 3

<div />如果您的 DOCTYPE 设置为 XHTML,则只能使用自结束标记,因为这是从 XML 借用的,您可以在 XML 中使用自结束标记。如果您的 DOCTYPE 设置为 HTML 4.01 或 HTML 5,则无法拥有它。

http://www.w3.org/TR/xhtml1/

我不确定你想要这个的确切用例,但如果是为了清除浮动,你可以这样做,而不必担心兼容性问题,特别是在 IE 进入模拟模式时。

<style type="text/css">
.clear-fix {
    clear: both !important;
    display: block !important;
    font-size: 0 !important;
    line-height: 0 !important;
    border: none !important;
    padding: 0 !important;
    margin: 0 !important;
    list-style: none !important;
}
</style>
<br class="clear-fix" />
Run Code Online (Sandbox Code Playgroud)