Performance: Self-closing tags
Self-closing tags affect the parsing speed and DOM construction during page load.
<img src="image.jpg" /> <br /> <input type="text" />
<img src="image.jpg"> <br> <input type="text">
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Incorrect closing tags on void elements | Extra parsing and DOM fixes | Possible reflows if DOM is corrected | Minimal | [X] Bad |
| Proper self-closing tags for void elements | Straightforward DOM creation | No extra reflows | Minimal | [OK] Good |
</tag>.<br> inserts a line break and is self-closing. The others require closing tags.img tag in HTML5?img can be written with or without a slash before the closing angle bracket, but the slash is optional and allowed.<img src='image.jpg' /> is correct and valid HTML5 syntax. <img src='image.jpg'></img> wrongly adds a closing tag. <img src='image.jpg'> is also valid but less explicit. <img src='image.jpg'</> is invalid syntax.<p>Hello<br>World</p>
<br> tag inserts a line break, moving content after it to a new line.<img src='photo.jpg'></img>
<img> tag is self-closing and does not require a closing tag like </img>.</img> which is invalid HTML and should be removed.<hr> and <br> do not require a slash before the closing bracket.<p>First paragraph.</p><hr><br><p>Second paragraph.</p> uses <hr> and <br> without slashes, which is correct. Options B and C mix slash and no slash inconsistently. <p>First paragraph.</p><hr/><br><p>Second paragraph.</p> uses slash on <hr/> but not on <br>, inconsistent but valid in XHTML, not preferred in HTML5.Pass the test and this pattern moves unknown