, otherwise - or
When you take some tutorial from the Internet, I see that many people leave the t...">

Html convention - self-close tag,> or / ">, otherwise - or <br/">

When you take some tutorial from the Internet, I see that many people leave the tags open, like <link ..> , <img ..> . But when I use Netbeans to edit them (HTML / JSP pages), it shows a red background on those tags until I add a slash to them. <br> โ†’ <br/> .

What is the correct way to write HTML based code?

+8
html coding-style
source share
3 answers

Both are great for HTML. Although not for XHTML, which is a dialect of XML.

Some elements do not need a closing tag ( /> ) - in particular, empty elements (those that have no content). Examples are <hr> and <br> . They can also be automatically closed ( <hr /> and <br /> , respectively). This self-closing is equivalent to having a close tag immediately after an open tag.

For XML, such a closing tag is invalid - it must be closed, either closed itself or has a closing tag. Therefore, <hr> not valid XML, but <hr /> and <hr></hr> .

HTML is not XML, but for better compatibility, some tools try to emit as much XML as possible, such as HTML.

+6
source share

It depends on the DOCTYPE you are using. If you use HTML 4, you should not use self-closing tags, if XHTML should then make valid XML, and if HTML 5, then closing the slash is optional, but not required.

The W3C HTML Validator will issue a warning if you try to use closing tags in HTML 4:

The sequence can be interpreted in at least two different ways, depending on the DOCTYPE document. For HTML 4.01 Strictly, '/' completes the tag '). However, since many browsers do not interpret it this way, even with HTML 4.01 Strict DOCTYPE, it is best to avoid it completely in pure HTML documents and reserve its use exclusively for those written in XHTML.

+4
source share

> true for HTML but not true for XHTML . Check out DOCTYPE.

  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 

for HTML strict and

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

for XHTML strict

0
source share

All Articles