<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alex Arriaga</title>
    <description>The latest articles on DEV Community by Alex Arriaga (@alex_arriaga).</description>
    <link>https://dev.to/alex_arriaga</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F444793%2Fd1c7d2cb-de33-4d52-a88d-5a36a554b4dd.jpeg</url>
      <title>DEV Community: Alex Arriaga</title>
      <link>https://dev.to/alex_arriaga</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alex_arriaga"/>
    <language>en</language>
    <item>
      <title>Should I "close" &lt;img &gt; and other HTML tags?</title>
      <dc:creator>Alex Arriaga</dc:creator>
      <pubDate>Fri, 28 Aug 2020 00:19:59 +0000</pubDate>
      <link>https://dev.to/alex_arriaga/should-i-close-img-and-other-html-tags-3727</link>
      <guid>https://dev.to/alex_arriaga/should-i-close-img-and-other-html-tags-3727</guid>
      <description>&lt;h3&gt;
  
  
  TL;DR
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;If using XHTML, close them, period.&lt;/li&gt;
&lt;li&gt;Even if using HTML5, we &lt;strong&gt;should&lt;/strong&gt; close them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mjC2cMnt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/0an26kt9bpaixhucpsjk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mjC2cMnt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/0an26kt9bpaixhucpsjk.png" alt="Alt Text" width="800" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The long story...
&lt;/h3&gt;

&lt;h4&gt;
  
  
  XHTML and the origin of HTML5
&lt;/h4&gt;

&lt;p&gt;If we would have a travel machine and we would go to the year of 2000, we would find the very first release of &lt;em&gt;XHTML&lt;/em&gt;, which was basically a reformulation of the three HTML 4 document types were supported by web browsers back then.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;XHTML&lt;/strong&gt; was created as an extension of &lt;em&gt;HTML 4.x&lt;/em&gt;, in order to make HTML documents compatible with XML strict-tree-like structures. That provided the advantage of using common XML tools to handle and work with HTML documents (as well-formed XML documents).&lt;/p&gt;

&lt;p&gt;However, during some years and until HTML5 came into play in 2014 as an actual W3C recommendation, there was some uncertainty about what to use or how to proceed in terms of documents types and, therefore, &lt;strong&gt;correct ways to create HTML documents&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We are now in 2020, things are clearer and more mature in terms of standards and web browsers support. However, &lt;strong&gt;we shouldn't underestimate XHTML's actual usage in enterprise systems and processes&lt;/strong&gt;, because sometimes the larger the project/scope is, the more likely we are going to work with XHTML over HTML.&lt;/p&gt;

&lt;p&gt;For instance, tasks such as &lt;em&gt;parsing, cleaning or crawling&lt;/em&gt; HTML documents become way easier if we have well-formed XML trees.&lt;/p&gt;

&lt;p&gt;My following examples of that kind of tasks are more anecdotal, than hard data and facts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The process of working with PDF libraries in Java that transform HTML documents to PDF such as &lt;a href="https://itextpdf.com/en"&gt;iText&lt;/a&gt; becomes less error-prone, due to most common used XML parsers work better with XML trees.&lt;/li&gt;
&lt;li&gt;Even if using something like &lt;a href="https://jsoup.org/"&gt;jsoup&lt;/a&gt; -the Java HTML Parser for excellence- to auto-close our tags, that will add some processing time to parsing/traversing tasks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  But, what if my authors/tools don't "close" all void elements?
&lt;/h4&gt;

&lt;p&gt;First of all, &lt;strong&gt;what is a "void" element?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the HTML spec a &lt;strong&gt;void&lt;/strong&gt; element is an element that only has a start tag.&lt;/p&gt;

&lt;p&gt;Void elements are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;area, 
base, 
br, 
col, 
embed, 
hr, 
img, 
input, 
link, 
meta, 
param, 
source, 
track, 
wbr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please check this page for more information: &lt;a href="https://html.spec.whatwg.org/#void-elements"&gt;https://html.spec.whatwg.org/#void-elements&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And per the W3C HTML spec:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;Then, if the element is one of the void elements, or if the element is a foreign element, then there may be a single U+002F SOLIDUS character (/). This character has no effect on void elements, but on foreign elements it marks the start tag as self-closing.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Having said that, even if a self-closing void element is perfectly valid in HTML5 (see &lt;a href="https://html.spec.whatwg.org/#start-tags"&gt;https://html.spec.whatwg.org/#start-tags&lt;/a&gt;), we should close our &lt;strong&gt;void&lt;/strong&gt; elements, because it is not going to affect anything in a bad way and it will give us a more XML-tree-like structure, which will be faster and more "secure" to parse.&lt;/p&gt;

&lt;p&gt;So, instead of having:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/path/to/image.png"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"Image"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should have the following tag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/path/to/image.png"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"Image"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  How about my current website which has a XML strict doctype?
&lt;/h4&gt;

&lt;p&gt;Imagine you have this &lt;strong&gt;Doctype&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That means, you MUST close &lt;strong&gt;void&lt;/strong&gt; elements until you decide to switch to HTML5, which may open a new question in your mind: &lt;strong&gt;what are the reasons I am using XHTML in strict mode?&lt;/strong&gt; The answer will depend on your particular use case, however, from my personal experience, HTML5 is the right doctype to be used in most scenarios.&lt;/p&gt;

&lt;p&gt;In a nutshell, my personal recommendation is to switch to HTML5 and always close your tags, for instance:&lt;/p&gt;

&lt;p&gt;This kind of tag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"search"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Should be transformed to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"search"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any thoughts? I will be glad to know your opinion.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
