Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Participant ,
Sep 27, 2022 Sep 27, 2022

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".

 

7.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 28, 2022 Sep 28, 2022

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....

 

 

Translate
Community Expert ,
Sep 28, 2022 Sep 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 28, 2022 Sep 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 28, 2022 Sep 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 & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 28, 2022 Sep 28, 2022

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 28, 2022 Sep 28, 2022

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 28, 2022 Sep 28, 2022

I do, thank you.  But language varies.

 

My documents also contain meta data and OpenGraph markup among other things.

 

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">

<title>UNIQUE PAGE TITLE</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="description" content="Lorem ipsum dolor...">
<meta name="keywords" content="This, that, other things...">
<meta name="author" content="YourName">

<meta property="og:url" content="http://www.example.com">
<meta property="og:type" content="article">
<meta property="og:title" content="When Great Minds Don't Think Alike">
<meta property="og:description" content="How much does culture influence creative thinking?">
<meta property="og:image" content="http://example.com/images/facebookJumbo-v2.jpg">

 

 

Nancy O'Shea— Product User, Community Expert & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 28, 2022 Sep 28, 2022

Hi Nancy. Thanks for your help. Once again I am guilty of not providing enough information. I thought the W3C Validator would be so ubiquitous that many people would have noticed this new behaviour. I have based all my HTML code on the guidelines of W3C Schools and help from the members of this forum. Last time I used the W3C Validator (a couple of weeks ago) all my documents were valid - No Errors or Warnings. Yesterday I ran it again and it throws up a ton of warnings. For example, the following types of tags are now flagged...

 
All meta tags eg: <meta charset="utf-8" />
 
All links eg: <link href="../css/global.css" rel="stylesheet" type="text/css" />
 
All image tags eg: <img src="images/logo_helenavista.gif" alt="Logo" />
 
All break tags - <br />
 
All h-ruler tags  - <hr />
 
Example from W3 Schools: <img src="img_girl.jpg" alt="Girl in a jacket" width="500" height="600">. Does W3C now consider this bad practice?! There must be billions of lines of such code out there.
 
I know I can get around this by using another validator - For now - But who sets the standards? Will I have to recode all my documents at some stage?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 28, 2022 Sep 28, 2022

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....

 

 

Nancy O'Shea— Product User, Community Expert & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 28, 2022 Sep 28, 2022

I found the culprit. I'm pretty sure I coded the documents correctly, first time, but then I have been formatting the documents with Prettier. Even after I remove the offending extra slashes, Prettier puts them back in as soon as I format or save! So I'm disabling Prettier until I figure out how to stop it doing that. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2022 Sep 29, 2022
quote

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....

By @Nancy OShea

 

To add to what you said, instead of replacing all the tags one by one, probably that simply searching for :

 

/>

 

and replacing it by :

 

>

 

should do the trick and will include IMG tag as well avoiding complex REGEX

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 29, 2022 Sep 29, 2022

Hi Lena - I did a global search\replace and it worked - Eventually. At first it was having no effect; I still had the extra slashes there. Finally I figured out that Prettier was reinserting the extra slashes whenever a document was saved! Once I disabled Prettier everythng worked OK. I can't believe a code formatter was actually modifying the code. It also explains how the extra slashes got in there to begin with. I didn't code them in - Prettier did that.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2022 Sep 29, 2022
quote

Hi Lena - Finally I figured out that Prettier was reinserting the extra slashes whenever a document was saved! Once I disabled Prettier everythng worked OK.  I didn't code them in - Prettier did that.

By @SeanKK

 

Well, I don't use prettier myself, but as @Jon Fritz presupposes, when saving prettier should probably look at the DOCTYPE used. What is your current DOCTYPE ?

 

Out of curiosity I googled the subject, and I came across this interesting old discussion about slash that we are talking about.https://github.com/prettier/prettier/issues/5246

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 29, 2022 Sep 29, 2022

Just regular: <html lang="en"> or <html lang="es">. Not the transitional type Jon mentioned. 

Still reading the link you sent - Interesting. I don't understand why a formatter would undertake to "correct" the code - Seems a bit presumptious. I suppose if I was sufficiently proficient I wouldn't need a formatter - But I need all the help I can get 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 29, 2022 Sep 29, 2022

If you use third party extensions be prepared for the worst. I would rely more on your own ability than someone who thinks they know what is good for you.

 

Having said that l don't think self closing tags are likely to be harmful in anyway, shape or form. Not that l would suggest using them myself but if they creep in through the back door it's annoying if you don't want them but not a  major issue.

 

Validation should be taken with a pinch of salt these days as it's fairly meaningless and isn't considered very critical by professionals who use advanced methods to create websites. The validation service fits in nicely if youre producing websites using the year 2000 workflows

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2022 Sep 29, 2022
quote

Just regular: <html lang="en"> or <html lang="es">. Not the transitional type Jon mentioned. 🙂


By @Galeodan


It appears you are a bit confused by doctype declaration vs html tag.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
...is the old XHTML 1.0 Transitional Doctype Declaration. It is no longer used in modern sites.

<!DOCTYPE html>
...is the current HTML5 Standard Doctype Declaration and needs to be the first line of your code.

<html lang="en">
...is your site's starting html tag with a language attribute set to the "en" (English) property. Your site also needs this (not necessarily the "en" part), just after the Doctype Declaration.

The first two lines of your code should be...

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

...anything else could be confusing your extensions/program.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 29, 2022 Sep 29, 2022

Pardon - Not the first time I've gotten confused. The first 2 lines are: 

<!DOCTYPE html>
<html lang="en">
 
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 23, 2023 May 23, 2023

Is it true, that DW does not offer an option to eliminate all closing slashes in all void elements in all files of a site?

Is it true, that you have to that manually and laborius with regular expression?

Is it true, that you can't choose the behaviour of DW to use or not use slashes in void elements for new documents?



Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 23, 2023 May 23, 2023

No, when using the correct doctype declaration, DW will not add trailing slashes on self closing elements.
No, not unless you have old code that you are converting to HTML5, which is the same for every editor I've used.
No, you choose DW's behavior by changing the doctype declaration for a page.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 23, 2023 May 23, 2023
LATEST

@Aturob,

Show us code lines 1 -10 from your document. 

 

For coding questions, I recommend you post at StackOverflow where the experts there can help you. 

https://stackoverflow.com/questions/67987722/unicode-and-html-emtities

 

Nancy O'Shea— Product User, Community Expert & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 28, 2022 Sep 28, 2022
quote
Example from W3 Schools: <img src="img_girl.jpg" alt="Girl in a jacket" width="500" height="600">. Does W3C now consider this bad practice?! 
 

By @Galeodan

 

No because its NOT a self closing image tag i.e. if it were it would be: (note the additional self closing red slash Ive added below)

 

<img src="img_girl.jpg" alt="Girl in a jacket" width="500" height="600"/ >

 

Self closing tags are no longer a requirement in modern coding.

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 28, 2022 Sep 28, 2022
quote

Self closing tags are no longer a requirement in modern coding.


By @osgood_

========

Self-closing tags were never in HTML5.  They are leftover from X/HTML docs.  Code soup is what invariably happens when converting from old doc types to modern ones without starting over.

 

I suppose the new Validation Service is getting stickier about proper code syntax.  Either that or the code was changed since running last validation checks.

 

 

Nancy O'Shea— Product User, Community Expert & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 28, 2022 Sep 28, 2022
quote
quote

I suppose the new Validation Service is getting stickier about proper code syntax.  Either that or the code was changed since running last validation checks.

 

By @Nancy OShea

 

Dont know, validating code these days is pointless as it cant keep up with what all these new frameworks throw at it. You know when your code its good to go, without the necessity for checking.

 

Not sure what it proves these days, most large websites, spending 1000's of pounds on development costs and top devs, will have errors/warnings, sometimes running into the high 100s

 

I was never ever an advocate of validation and so its proven to be correct many, many, many years later.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 28, 2022 Sep 28, 2022
quoteYou know when your code its good to go, without the necessity for checking.

By @osgood_

===========

I do and you do but the majority of Dreamweaver users probably don't.

 

I've always said clean code is easier to work with than messy code.  But sometimes you have to break rules to make browsers behave.  There's a fine line between knowing which rules you can break and get away with and which one's you can't. 

 

I think self-closing tags falls into the former category because browsers are so forgiving.

 

Nancy O'Shea— Product User, Community Expert & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 28, 2022 Sep 28, 2022

OK - I think I can see whats happened here. It IS just me, and probably a few others, doing it wrong. I remember a time I didn't put the closing slashes everywhere, but some editor started adding them in for me. I noticed it happening for simple tags, like <br> changing to <br/> and <hr> changing to <hr/>, but I never noticed it also happening to the others like <meta> <img> etc. I did all my earliest work in DW but lately have been using VSCode more - I'll have to check which editor was doing that (maybe neither is now). I have also been using snippets and DIY templates extensively, so once the gremmlin was in it got to go everywhere. Now to decide how to fix it with the least pain. The W3C Validator has definitely gotten stricter.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines