LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-15-2022, 06:32 AM   #1
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,196

Rep: Reputation: 165Reputation: 165
HTML5 tag syntax confusion


I'm confused. I see how-to examples showing trailing /> constructs on e.g.:
Code:
<input type="text" id="comment" name="comment" value="I'm a text field" />
and also <img /> and <br />, but the HTML Validator https://validator.w3.org/ tells me the '/' in /> is not needed.
Quote:
Trailing slash on void elements has no effect and interacts badly with unquoted attribute values.

From line 290, column 1; to line 290, column 47

<input type="hidden" name="haveW4P" value="Y"/>
Elsewhere I read that the /> construct is for XHTML, but I nevertheless see the /> construct in web examples for normal HTML.

What's the correct syntax? Is this officially defined somewhere?

Last edited by mfoley; 11-15-2022 at 06:38 AM.
 
Old 11-15-2022, 08:11 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 2,983

Rep: Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120

Short answer: it doesn't matter, use what you prefer.

A lot of people prefer the visual clue it provides that a tag is standalone.

Longer answer: Read https://en.wikipedia.org/wiki/HTML5#W3C_and_WHATWG_conflict and https://en.wikipedia.org/wiki/WHATWG regarding the "standard".

The "interacts badly" bit is saying that if you use unquoted attributes AND you don't put a space between the slash and last attribute, then the final attribute will include the slash in its value. Since your example follows the common practice of using both quotes and a space, it's a non-issue. Use the slash if you want to, or skip it if you don't.

 
Old 11-15-2022, 09:37 AM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,701

Rep: Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062
It is indeed not needed, but it also doesn’t hurt.
 
Old 11-15-2022, 10:42 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,540
Blog Entries: 3

Rep: Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410
Quote:
Originally Posted by dugan View Post
It is indeed not needed, but it also doesn’t hurt.
Indeed. Since it does not seem to matter one way or another, I have decided to always use them to ensure that documents are well-formed, valid XHTML so I can have the option to run an XML parser over them when needed. That turns out to be a real lifesaver on occasion.

In general, HTML5 looks like it has been subject to a lot of damage from m$ involvement in contrast to HTML 4.
 
Old 11-15-2022, 11:33 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,387
Blog Entries: 1

Rep: Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665
Basically, /> is okay in XML, not okay in SGML. Originally HTML was an instance of SGML, but some good people added XML-ish bits into it, even created another language called XHTML, which _is_ an instance of XML.
 
Old 11-20-2022, 10:44 PM   #6
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,196

Original Poster
Rep: Reputation: 165Reputation: 165
Thanks for the feedback. I've used the /> construct A LOT, but lately I've also used the https://validator.w3.org/ validator to check my html. As mentioned, that give the warning, "Trailing slash on void elements has no effect and interacts badly with unquoted attribute values." as boughtonp noted, I do use quotes on values (and even if I didn't I think I'd be smart enough to put a space after the value).

So, I'll do some experimentation. I'm used to using the /> construct and if there is a way to turn off that warning when validating I'll continue to do so. However, if I can't turn it off, it generates a lot of clutter on the validation report, in which case I'll stop using it. I have a program I'm working on now, so I'll test it and report back.

Turbocapitalist makes a good point about being able to use and XHTML validator, although I've not used one to date.
 
Old 11-21-2022, 08:50 AM   #7
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 2,983

Rep: Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120
Quote:
Originally Posted by mfoley View Post
if there is a way to turn off that warning when validating I'll continue to do so.
Press the "message filtering" button and untick the relevant checkbox.


Or, so long as the page is rendering as expected, stop worrying about validating it - pre-HTML5 it was important to have valid markup to ensure browsers displayed things consistently, but now that they've finally bothered to define how browsers should handle unexpected tags, it has become less meaningful.

 
Old 11-21-2022, 09:18 AM   #8
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,412

Rep: Reputation: 416Reputation: 416Reputation: 416Reputation: 416Reputation: 416
There's also a nice tool called "tidy". It's used to convert between various html types. By default, it removes the trailing / in tags. But if you chose an xhtml type, it will add it (if missing). It's very useful to make generated html readable. See "man tidy" after installing it - lots of options.
 
2 members found this post helpful.
Old 11-21-2022, 02:23 PM   #9
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Location: Non. Je suis propriétaire – No. I am an owner.
Distribution: Apple-selling shops, markets and direct marketing
Posts: 1,388
Blog Entries: 37

Rep: Reputation: 733Reputation: 733Reputation: 733Reputation: 733Reputation: 733Reputation: 733Reputation: 733
Quote:
Originally Posted by Guttorm View Post
There's also a nice tool called "tidy". It's used to convert between various html types.
No.

Tidy is a validator and can overwrite an initial (x)HTML or Maybe-Html document (5) with something considered “more correct”, with respect to a given DOCTYPE. This is an option (-m) but you can validate your code with tidy, see all errors and warnings and still preserve your original document.

Last edited by Michael Uplawski; 11-27-2022 at 12:28 PM. Reason: formulations, words and stuff.
 
Old 11-29-2022, 10:44 AM   #10
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,196

Original Poster
Rep: Reputation: 165Reputation: 165
I've checked my current program with two validators: https://validator.w3.org, and https://www.freeformatter.com. The w3 validator flags the "Trailing slash on void elements" as an Info message. As boughtonp points out, that message can be filtered. The freeformatter validator does not flag "Trailing slash on void elements".

So, I think I'll just keep using the /> construct since I'm been doing so for quite a while and lots of programs have this.

Thanks all for your help and feedback!
 
  


Reply

Tags
html


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help to translate this FIX Message with the Tag Name tag Number and Tag Value jtighe Linux - Newbie 3 08-18-2022 09:32 AM
[SOLVED] No sound in HTML5(and HTML5 on Youtube) Hoff123 Linux - General 3 09-19-2020 08:46 AM
HTML5 "<details>" tag display:inline, display:hidden CSS ignored (Firefox, Seamonkey) ShellyCat Programming 1 01-11-2011 11:23 PM
LXer: 15 HTML5 Demos Showcasing Prowess of HTML5 Over Adobe Flash LXer Syndicated Linux News 0 08-14-2010 03:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration