Results 1 to 6 of 6

Thread: Closing html properly

  1. #1
    Join Date
    Mar 2011
    Posts
    2,055
    Thanks
    59
    Thanked 108 Times in 106 Posts
    Blog Entries
    4

    Default Closing html properly

    Hello everyone,
    In this situation, is there any reason to actually do the latter apart from modern-day standards

    HTML Code:
    <br>
    compared to

    HTML Code:
    <br />
    same for img, hr, etc.

  2. #2
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    No reason other than conforming to w3c standards for XHTML, it's just closing tags, for neatness.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    In XHTML, yes. In [other] HTML, no.

    In HTML, tags that go around content require a preceding 'open' tag and a following 'close' tag, while tags that are the content (for example, an image) have a single tag, which doesn't need a separate close tag.

    In XHTML, the idea was simply to merge the open+closer tags so that it is <img /> rather than <img> without an </img> anywhere.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The situation is a little more complex. Unless your XHTML page is actually being served as application/xml, it will be error corrected to HTML by the browser.

    It likely isn't being served as application/xml, because if it were, it wouldn't load in IE 8 and less.

    You really shouldn't use XHTML unless you are serving application/xml. If you do, you need a valid XML prologue. And as I say the page won't load in IE 8 and less.

    Use HTML 4.01 or HTML 5. In 4.01, self closing tags are not allowed, in 5 they are, but are not required.

    About the worst thing you can do in either HTML or XHTML is to self close a tag that requires a closing tag. The iframe tag is a good example. But any tag requiring a closing tag can act oddly if you try to get away with self closing it rather than using its required closing tag. At the same time you need to be aware that, for scripting purposes, some browser will erroneously self close some tags. This can throw off your element count (say if you're using getElementsByTagName('*') or childNodes()) in those browsers even when your code is valid.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    In XHTML, self closing a tag that requires a closing tag yields an error anyway, so it's within the XHTML standards to do that. I personally maintain XHTML standards on regular HTML pages, for the sake of keeping up readability and coding habits.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Self closing tags on HTML pages are invalid unless it's HTML 5. But just as browsers error correct most "XHTML" to HTML, they will usually do the same with HTML written as XHTML. But there could be exceptions.

    It's generally best to follow the standards of the DOCTYPE you're using.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •