Skip to main content
Galeodan
Known Participant
September 28, 2022
Answered

New W3C Warning about self-closing tags - What to do?

  • September 28, 2022
  • 3 replies
  • 7429 views

Suddenly all my HTML docs are receiving multiple warnings when I run W3C Validation...

 

"Self-closing tag syntax in text/html documents is widely discouraged; it’s unnecessary and interacts badly with other HTML features (e.g., unquoted attribute values). If you’re using a tool that injects self-closing tag syntax into all void elements, without any option to prevent it from doing so, then consider switching to a different tool."

 

All the sources I have learned from, including W3Schools, follow this "bad practice". So how come this has suddenly become a problem?

 

Can the error be suppressed in the W3C Validator? Can anyone suggest a tool which will go through my 1,000's of lines of code and fix all these self-closed tags? 

 

I could try to ignore it but the validation reports are so full of this that it's hard to spot the errors and warnings that do "matter".

 

    This topic has been closed for replies.
    Correct answer Nancy OShea

    As suspected, you're mixing old fashioned syntax with modern code.

     

    Change this:

    <br />

     

    To this:

    <br>

     

    Change this:

    <hr />

     

    To this:

    <hr>

     

    And so on....

     

     

    3 replies

    Liam Dilley
    Inspiring
    September 30, 2022

    I think an important thing missed in the discussion is that it is only a warning. Lot's of people have commented on the message itself because NO ONE has found any way that they react badly with other elements, that is imposible. HTML5 still has everything XHTML had for example in it plus all new HTML5 elements so unless they did a fresh HTML6 (which like CSS now they do not plan to do) self enclosing tags will never have an issue.

    It is good to have valid code - 100% but as someone pointed out with all the means to render HTML through frameworks (noting I detest code like React blending style and markup inside scripted code when it should all be seperate) plus things like CMS's... Basics and having DIV's close off properly etc is all the important stuff. This stuff is not something to spend time on really.

    Galeodan
    GaleodanAuthor
    Known Participant
    September 30, 2022

    I'm reassured to hear that it's probably nothing for me to be concerned about. I'm not aiming for total purity and my documents do prompt other warnings when I run W3C Validator (some come from WowSlider) but they are few and it's really no problem. The problem with the self-closing tags is that W3C Validator, which I would like to use, throws up hundreds of warnings and it's impossible to find the ones that do matter to me. So I either stop using W3C Validator, or I stop using Prettier, which I also favour for reasons I can't remember now but will probably rediscover when I start using some other formatter.  

    Nancy OShea
    Community Expert
    Community Expert
    September 28, 2022

    Without seeing your code, it's impossible to say for sure.  I suspect you might  be mixing old syntax with current syntax.  For best results, follow the HTML5 web standards and you should be good to go.

    https://www.w3schools.com/html/

     
    <!DOCTYPE html>
    <html>
    <head>
    <title>Page Title</title>
    </head>
    <body>
    <h1>This is a Heading</h1>
    <p>This is a paragraph.</p>
    </body>
    </html>
    

     

    Nancy O'Shea— Product User & Community Expert
    Participant
    September 28, 2022

    <!DOCTYPE html>
    <html lang="en">

    Participant
    September 28, 2022

    Warning: Consider adding a lang attribute to the html start tag to declare the language of this document.

    Jon Fritz
    Community Expert
    Community Expert
    September 28, 2022

    What validator are you actually using?
    Are you seeing a "warning" or an "error"?
    Which doctype declaration is at the top of your pages?
    Does your tag end with "/>" when it should be simply ">"?

    FWIW, W3schools is NOT an authority on capital S-Standards when it comes to html. They do have mostly accurate information on their site but they are not associated with the W3C.

    Galeodan
    GaleodanAuthor
    Known Participant
    September 28, 2022

    Thanks for responding Jon.

     

    My doctype declaration is: <html lang="en">

     

    I am using the W3C Validator. Last time I validated (19Sept), using the W3C Validator, I had all valid documents (0 Warnings & 0 Errors). Today I have hundreds of warnings. So did the rules suddenly change?