在HTML5中关闭标签 [英] Closing tags in HTML5

查看:94
本文介绍了在HTML5中关闭标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在HTML5中,使用< br /> 关闭< br> 标记仍然合适?



这显然也适用于所有其他单个标签。我意识到这实际上并不影响东西的工作方式,但是这里最好的做法是什么?
$ b

 一些txt< br> //你怎么看到很多人在做
然后一些< br /> // XHTML样式!我还应该这样做吗?
我还没完成..< br> < / BR> //没有人这样做对吗?对?!


解决方案

适用于HTML的W3C规范


  • 以下是HTML中的void元素的完整列表:


    • area base br col 命令嵌入 hr img 输入 keygen link meta param source track wbr


  • Void元素只有一个开始标签;不得为void元素指定结束标记。


  • 开始标记由以下部分组成,完全按以下顺序排列:




    • A < 字符

    • 元素的标签名称
    • li>
    • 可选地,可以包含一个或多个属性,每个属性前面必须有一个或多个空格字符。 li>
    • 可选地,一个 / 字符,只有当元素是一个void元素时才可能存在。

    • > 字符。




    由此看来,我们可以使用< br> < br /> 。不过,结束标记< / br> 不是有效的,所以请勿使用< br> < / br>



    通过 HTML验证服务表示尽可能多。

     <!DOCTYPE html> 
    < html>
    < head>
    < meta charset =utf-8>
    < title>测试void元素< / title>
    < / head>
    < body>
    一些txt行< br>
    然后一些< br />
    我还没有完成...< br> < / BR> <! - 该行生成错误 - >
    < / body>
    < / html>

    因此,请使用< br> < br /> ,只需确保一致使用它们。



    修改:正如Brad所指出的,< br /> 也是有效的XHTML,因此最好选择这种形式。


    In HTML5 is it still appropriate to close a <br> tag with <br />?

    This obviously also applies to all other single tags. I realize this doesn't actually affect how stuff works, but what is best practice here?

    someLines of txt <br>            // How you see a lot of people doing it
    and then some <br />             // XHTML style! Should I still be doing this? 
    ow im not done yet.. <br> </br>  // No one does this right? RIGHT?!
    

    解决方案

    From the W3C specification for HTML:

    • The following is a complete list of the void elements in HTML:

      • area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr
    • Void elements only have a start tag; end tags must not be specified for void elements.

    • Start tags consist of the following parts, in exactly the following order:

      • A < character.
      • The element’s tag name.
      • Optionally, one or more attributes, each of which must be preceded by one or more space characters.
      • Optionally, one or more space characters.
      • Optionally, a / character, which may be present only if the element is a void element.
      • A > character.

    From this it seems that we can use either <br> or <br/>. However, the end tag </br> is not valid, so don't use <br> </br>.

    Running the following through the HTML Validation Service indicates as much.

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>Testing void elements</title>
    </head>
    <body>
        some lines of txt <br>
        and then some more <br />
        I'm not done yet... <br> </br> <!-- This line generates an error -->
    </body>
    </html>
    

    So use either <br> or <br/>, just make sure you use them consistently.

    Edit: As noted by Brad, <br /> is also valid XHTML, so perhaps it is best to choose this form.

    这篇关于在HTML5中关闭标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆