SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 33
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    <meta> self-closing tags?

    Hello,

    Would both be correct, or is one better than the other?

    Code:
    <meta charset="utf-8">
    <meta charset="utf-8" />
    Cheers.

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,297
    Mentioned
    460 Post(s)
    Tagged
    8 Thread(s)
    The first one is the standard for HTML5, used like so:

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title></title>
    </head>
    The second would be used for XHTML5:

    Code:
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta charset="UTF-8" />
    <title></title>
    </head>

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would it be ok to close it in standard html5?

  4. #4
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,297
    Mentioned
    460 Post(s)
    Tagged
    8 Thread(s)
    No, I don't think so. The closing slash like that always was invalid HTML—but something the browsers have tolerated.

  5. #5
    Robert Wellock silver trophybronze trophy xhtmlcoder's Avatar
    Join Date
    Apr 2002
    Location
    A Maze of Twisty Little Passages
    Posts
    6,316
    Mentioned
    60 Post(s)
    Tagged
    0 Thread(s)
    HTML5 is like a complete slut; it allows both methods it swings both ways so you could use either for 'text/html' version (obviously the first version being more correct). Though obviously you need the closing slash version for XHTML5 to conform within well-formedness constraints.

    So yes actually BOTH methods can be used in the HTML version! Whereas only version two is allowed for the XHTML version.

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it the same for <link rel=...>?

  7. #7
    SitePoint Zealot Gar onn's Avatar
    Join Date
    Feb 2011
    Location
    Belgium
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes,
    you can omit the trailing slash from all elements that have no content

  8. #8
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by xhtmlcoder View Post
    So yes actually BOTH methods can be used in the HTML version!
    Are you sure abuot this? It is my understanding that this is simply because browsers generally doesn't understand null end tags (as in HTML 4.01 and below), not because it's actually valid.
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  9. #9
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My question was rather: can I add it (in html5)?

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,182
    Mentioned
    16 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by C. Ankerstjerne
    Are you sure abuot this? It is my understanding that this is simply because browsers generally doesn't understand null end tags (as in HTML 4.01 and below), not because it's actually valid.
    It is correct, the new book by Sitepoint on HTML5 covers this and other "forgiving" aspects in comparison to HTML4. Though I will be leaving out the trailing slash for the time being with HTML5. Mainly because I write HTML4, validate against it then swap the doctype, but thats another story.

    Quote Originally Posted by rhgiant
    My question was rather: can I add it (in html5)?
    You can either use a trailing slash or omit it. Good practice though is to omit it when writing HTML5 just as you were writing HTML4.
    The only code I hate more than my own is everyone else's.

  11. #11
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,283
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    Would it be ok to close it in standard html5?
    Who knows, maybe some day the Web Standards Evangelical Army's black-clad jack-booted storm troopers will come into your bedroom in the night-time and stuff you in a sack and drag you away for closing EMPTY elements in "standard" HTML5. But I doubt it. No browser cares, that's for sure.

  12. #12
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The space in front of the slash is unnecessary unless you still need to support Netscape 4.

    I would recommend leaving out the slash unless you intend to start serving the page as XHTML as soon as IE8 use falls low enough to ignore (since IE8 and earlier don't support XHTML).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  13. #13
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    I actually prefer the space just for ease of reading. Makes the code clearer to me.

    But then I'll often format really long tags as attribute per line indented with the /> on it's own line for clarity. Beats the ever living tar out of the hard to read run-on lines you see in a lot of code.

    one of the reasons I prefer XHTML formatting in the first place... with just > you're never sure if it's the end of a tag or the start of one.. with /> it's always clear.

  14. #14
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    with just > you're never sure if it's the end of a tag or the start of one.. with /> it's always clear.
    When would a tag ever start with a >?
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  15. #15
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by C. Ankerstjerne View Post
    When would a tag ever start with a >?
    Methinks you failed to grasp what I was saying. there are two types of tags, those that have content, and those that do not... when I say tag I'm referring to the entire element -- the opening and closing of the tag with it's content; basically in the DOM sense of it.

    Code:
    <label
      for="someBloodyInput"
      id="someBloodyLabel"
      class="error"
    >
    > means this is a starting tag. </label> would be the end of the tag.

    Code:
    <input
      type="text"
      id="someBloodyInput"
      class="error"
    />
    /> is the close of the tag.

    If I've scrolled to where I can't see the opening, using HTML 4 I don't know what tag it is so I can't be sure if it's an opening tag or a self-closing. Always using /> means I can always make that determination even if /> is all I can see... saving me the bother of scrolling up to find out.

    A common formatting method if you practice the 76 rule -- which is why real programming languages that have a block structure are CONSISTENT in it's application. Something HTML is a miserable failure at and really the only thing XHTML gives us that's an improvement.

  16. #16
    Robert Wellock silver trophybronze trophy xhtmlcoder's Avatar
    Join Date
    Apr 2002
    Location
    A Maze of Twisty Little Passages
    Posts
    6,316
    Mentioned
    60 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by C. Ankerstjerne
    Are you sure about this? [...]
    Yes, for the new void elements it is the case. Obviously you cannot do something like: <p /> or <br></br> for Fred 'text/html'.

    We digress as we are only looking at the META or other VOID elements only for this thread. Like I said it is "bad practice" for the 'text/html' version but legal.

    For you reading pleasure: http://www.w3.org/TR/html-markup/syn...yntax-elements

    Off Topic:

    I believe that SP Book has some outstanding errata waiting regarding certain wording upon p54. ;-)

  17. #17
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,297
    Mentioned
    460 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by deathshadow60 View Post
    one of the reasons I prefer XHTML formatting in the first place... with just > you're never sure if it's the end of a tag or the start of one.. with /> it's always clear.
    I still can't get out of my head that /> is invalid HTML.

  18. #18
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use either. If it's a tag without a content then the tag is ideal.

  19. #19
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ralph.m View Post
    I still can't get out of my head that /> is invalid HTML.
    While for me, HTML's "formatting rules" (aka WHAT RULES!?!) are so loose, permissive and idiotic that I could give a flying purple fish what it considers valid or invalid... Hence why I like XHTML as it has more consistent rules... and thing HTML5's "just slap together anything" is so much of a colossal step in reverse.

    Rules are good. They tend to keep you out of trouble in the first place -- as opposed to just sleazing stuff together any old way... which ever since HTML 3 seems to be 'the norm'. Again, see all the people STILL vomiting up HTML 3.2 and slapping a 4 tranny or 5 lip-service on it.

  20. #20
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why would <meta... /> and <link rel... /> be "bad practice" when <img... /> isn't?

    Btw, is <link rel... /> to be considered like <meta> in terms of bad practice/self-closing-ness?


  21. #21
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deathshadow60 View Post
    Code:
    <input
      type="text"
      id="someBloodyInput"
      class="error"
    />
    /> is the close of the tag.
    Well, yes and no. In XML-based languages, such as true XHTML, you are correct. In HTML, the input tag is closed by the /. The > is content.
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  22. #22
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rhgiant View Post
    Why would <meta... /> and <link rel... /> be "bad practice" when <img... /> isn't?
    I think you're really confused and/or totally failing to grasp this, as <meta /> and <link /> are REQUIRED if you want valid code when making XHTML just like <img /> -- no "good or bad practice" involved... in HTML, /> is invalid markup just as > on standlone tags is invalid in XHTML. The syntax rules are consistent within the doctype you choose.

    To clarify, "standalone tags" -- that's META, LINK, IMG, BR, HR, INPUT, ISINDEX, BASE, COL, AREA, PARAM and FRAME. (I think that's all of them).

    If you're writing XHTML, those tags MUST be closed with />.... if you're writing HTML, those tags should be closed with just >.

  23. #23
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by C. Ankerstjerne View Post
    Well, yes and no. In XML-based languages, such as true XHTML, you are correct. In HTML, the input tag is closed by the /. The > is content.
    Uhm... Isn't that backwards?? Well, not backwards, but ... really wrong?

    I thought in HTML the / was an invalid character ignored as an unrecognized attribute (since any element inbetween <tagname and > is treated as an attribute -- meaning > is still closing them just like it closes them when the / isn't there in the first place. I'm pretty sure a forward slash doesn't close jack in HTML.

    <img src="image.png" alt="test">
    Open and close. Valid HTML 4, invalid XHTML.

    <img src="image.png" alt="test" />
    Open and close, valid XHTML, invalid HTML but still works because the / is treated as a unrecognized attribute... just as you can type in any amount of fantasy land attributes or tag names and HTML plods on as if nothing is wrong... since that's one of the ways HTML maintains backwards compatibility.

    <img src="image.png" html5="bloatedBS" / boxed noclub cluck="clucked" dyson="sucks" alt="test">

    Invalid HTML 4... will still show the image and still parse the recognized attributes (src and alt).

  24. #24
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deathshadow60 View Post
    I think you're really confused and/or totally failing to grasp this, as <meta /> and <link /> are REQUIRED if you want valid code when making XHTML just like <img /> -- no "good or bad practice" involved... in HTML, /> is invalid markup just as > on standlone tags is invalid in XHTML. The syntax rules are consistent within the doctype you choose.

    To clarify, "standalone tags" -- that's META, LINK, IMG, BR, HR, INPUT, ISINDEX, BASE, COL, AREA, PARAM and FRAME. (I think that's all of them).

    If you're writing XHTML, those tags MUST be closed with />.... if you're writing HTML, those tags should be closed with just >.
    Oh, I finally get it. That was the answer Thanks.

    html5: <img> <meta> <link>
    xhtml: <img /> <meta /> <link />


  25. #25
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rhgiant View Post
    Oh, I finally get it. That was the answer Thanks.

    html5: <img> <meta> <link>
    xhtml: <img /> <meta /> <link />

    ALMOST.

    html4: <img> <meta> <link>
    xhtml: <img /> <meta /> <link />
    html5: mix and match however you want -- who cares about formatting rules, clear syntax or trying to prevent people from making mistakes in the first place, just vomit it up any old way -- nobody understood the advantages of strict anyways and just wants to sleaze by on as little effort as possible. We want all the gee whiz shiny things now, now, now -- who cares what it costs us in the future. It's the philosophy of most people's spending -- why should coding be any different?

    HTML 5 -- explosive diarrhea in the swimming pool on a hot July day.


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
  •