Image tag closing

前端 未结 5 801
野趣味
野趣味 2020-12-12 08:10

How to close image tag in HTML?


or


or

相关标签:
5条回答
  • 2020-12-12 08:31

    As per this HTML5 standard: http://www.w3.org/TR/html-markup/syntax.html#void-element

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

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

    The img is a void element and hence the part #5 would apply with a caveat that "a '/' character, which may be present..."

    <img id="..." src="..." />
    

    And so, you may omit the part #5 i.e. the closing "/", and hence this is also valid:

    <img id="..." src="..." >
    

    Further down the spec says:

    Void elements only have a start tag; end tags must not be specified for void elements.

    So, no end tag is required.

    0 讨论(0)
  • 2020-12-12 08:31

    HTML 4.01 and earlier:

    <img> <!-- end tag forbidden -->
    

    XHTML

    <img/> <!-- self closing tag syntax -->
    <img></img>
    

    HTML Compatible XHTML

    <img /> <!-- self closing tag syntax with a space -->
    

    HTML 5

    <img> <!-- end tag forbidden -->
    <img /> <!-- end tag forbidden but a / allowed for people addicted to XML -->
    
    0 讨论(0)
  • 2020-12-12 08:37

    in xhtml:

    <img src="#" alt="some explaining" />
    

    the closing / is for backwards compatibility

    in html5

    <img src="#" alt="some explaining">
    

    no need anymore to close the <img> tag, (the browser knows that it is self-closing)

    more info here: http://www.w3.org/TR/html5/embedded-content-0.html#the-img-element (thanks to Chris)

    0 讨论(0)
  • 2020-12-12 08:42

    Like this:

    <img src="#" />
    

    An image tag can't have any content, but you do need to close it in HTML4. In HTML5 this is not required any more.

    So then this is also allowed:

    <img src="#">
    
    0 讨论(0)
  • 2020-12-12 08:43

    It is an empty element so you can close the start tag itself like this

    <img src="YOUR_FILE"/>
    

    or you can even not closing it like this

    <img src="YOUR_FILE">
    
    0 讨论(0)
提交回复
热议问题