 
    
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>img Archives - Complete, Concrete, Concise</title>
	<atom:link href="https://complete-concrete-concise.com/tag/img/feed/" rel="self" type="application/rss+xml" />
	<link>https://complete-concrete-concise.com/tag/img/</link>
	<description>Practical Information Without The Bloat</description>
	<lastBuildDate>Fri, 30 Mar 2018 07:25:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
	<item>
		<title>Images in HTML</title>
		<link>https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/images-in-html/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Fri, 30 Mar 2018 07:25:52 +0000</pubDate>
				<category><![CDATA[Front-End Basics]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[img]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[webdev]]></category>
		<guid isPermaLink="false">https://complete-concrete-concise.com/?p=3493</guid>

					<description><![CDATA[<p>Images are an important part of the World Wide Web.</p>
<p>The post <a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/images-in-html/">Images in HTML</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="preamble">
While images were not part of the original HTML specification, it is hard to image the World Wide Web with them.
</div>
<h2>Contents</h2>
<ol type="1">
<li><a href="#img-element"><span class="kbd">&lt;img&gt;</span> Element</a>
<ol type="1">
<li><a href="#src">src</a></li>
<li><a href="#alt">alt</a></li>
</ol>
</li>
<li><a href="#practical-considerations">Practical Considerations</a></li>
<li><a href="#summary">Summary</a></li>
<li><a href="#playing-around">Playing Around</a></li>
<li><a href="#references">References</a></li>
</ol>
<h2 id="img-element"><span class="kbd">&lt;img&gt;</span> Element</h2>
<p>Unlike the other HTML tags we have seen, the <span class="kbd">&lt;img&gt;</span> tag is completely self contained &#8211; there is no closing tag for it. This is because it doesn’t contain any content &#8211; it directly embeds an image into the document:</p>
<div id="cb1" class="sourceCode">
<pre class="sourceCode html"><code class="sourceCode html"><a id="cb1-1" class="sourceLine" data-line-number="1"></a><span class="kw">&lt;img&gt;</span></code></pre>
</div>
<p>The example above is the simplest (and incorrect) usage and doesn’t display anything because there is no image associated with the tag.<br />
How the <span class="kbd">&lt;img&gt;</span> element is rendered by the <strong>client</strong> is controlled by various <strong>attributes</strong></p>
<h3 id="src">src</h3>
<p>The attribute <strong>src</strong> is mandatory. Its value is a URL that identifies the image resource to be loaded. It can be:</p>
<ul>
<li><strong>absolute</strong> &#8211; the full path to the image is specified. For example, <span class="kbd">http://example.com/image.jpg</span></li>
<li><strong>relative</strong> &#8211; the relative path from to the image from the current document is specified. For example, <span class="kbd">img/image.jpg</span> says to locate the file <strong>image.jpg</strong> located in the folder <strong>img</strong> which is a subfolder in the directory from which this page is being served.</li>
</ul>
<div id="cb2" class="sourceCode">
<pre class="sourceCode html"><code class="sourceCode html"><a id="cb2-1" class="sourceLine" data-line-number="1"></a><span class="kw">&lt;img</span><span class="ot"> src=</span><span class="st">"image.jpg"</span><span class="kw">&gt;</span>
<a id="cb2-2" class="sourceLine" data-line-number="2"></a>
<a id="cb2-3" class="sourceLine" data-line-number="3"></a><span class="kw">&lt;img</span><span class="ot"> src=</span><span class="st">"img/image.jpg"</span><span class="kw">&gt;</span>
<a id="cb2-4" class="sourceLine" data-line-number="4"></a>
<a id="cb2-5" class="sourceLine" data-line-number="5"></a><span class="kw">&lt;img</span><span class="ot"> src=</span><span class="st">"http://example.com/image.jpg"</span><span class="kw">&gt;</span></code></pre>
</div>
<p>Let’s explore each of these uses:</p>
<div id="cb3" class="sourceCode">
<pre class="sourceCode html"><code class="sourceCode html"><a id="cb3-1" class="sourceLine" data-line-number="1"></a><span class="kw">&lt;img</span><span class="ot"> src=</span><span class="st">"~/image.jpg"</span><span class="kw">&gt;</span></code></pre>
</div>
<p>In this case, the file <strong>image.jpg</strong> is assumed to be in the same location as the <strong>.html</strong> file loading this image. In other words, both your HTML and image file are in the same directory.</p>
<div id="cb4" class="sourceCode">
<pre class="sourceCode html"><code class="sourceCode html"><a id="cb4-1" class="sourceLine" data-line-number="1"></a><span class="kw">&lt;img</span><span class="ot"> src=</span><span class="st">"img/image.jpg"</span><span class="kw">&gt;</span></code></pre>
</div>
<p>In this case, the file <strong>image.jpg</strong> is in a subfolder called <strong>images</strong> that is located in the same folder as the <strong>.html</strong> file loading the image. In other words, <strong>img/</strong> is a folder in the directory with your HTML file and the file <strong>image.jpg</strong> is located in that folder.<a id="fnref1" class="footnote-ref" href="#fn1"><sup>1</sup></a></p>
<div id="cb5" class="sourceCode">
<pre class="sourceCode html"><code class="sourceCode html"><a id="cb5-1" class="sourceLine" data-line-number="1"></a><span class="kw">&lt;img</span><span class="ot"> src=</span><span class="st">"http://example.com/image.jpg"</span><span class="kw">&gt;</span></code></pre>
</div>
<p>In this case, the file <strong>image.jpg</strong> is being loaded from the domain <strong>example.com</strong>.<a id="fnref2" class="footnote-ref" href="#fn2"><sup>2</sup></a><br />
As your webpages become more complex, it is a good idea to structure and organize your files. Having a separate <strong>images</strong> or <strong>img</strong> directory is a good idea.<br />
Having said that, it is easy to go overboard in trying to structure and organize your files. If you have a handful of files, there is no problem in keeping them all in the same directory. If you have dozens of images, it might be a good idea to put them in their own subfolder.<br />
For now, I recommend you either:</p>
<ol type="1">
<li>Place the images in the same folder as the <strong>.html</strong> file.</li>
<li>Place the images in a subfolder called <strong>/img</strong> or some other meaningful name.</li>
</ol>
<p>As your webpages become more complex and you start creating sites with many pages, you will need to think about how to structure your resources.</p>
<h3 id="alt">alt</h3>
<p>The <strong>alt</strong> attribute provides a text alternative for the image if it cannot be loaded. The image might not be loaded because it is unavailable or because the document is being viewed in a <strong>client</strong> that cannot display images (for example, a text reader for the visually impaired).</p>
<div id="cb6" class="sourceCode">
<pre class="sourceCode html"><code class="sourceCode html"><a id="cb6-1" class="sourceLine" data-line-number="1"></a><span class="kw">&lt;img</span><span class="ot"> src=</span><span class="st">"image.jpg"</span><span class="ot"> alt=</span><span class="st">"some text"</span><span class="kw">&gt;</span></code></pre>
</div>
<p>This leads us to an interesting behaviour of images, they are not embedded as a separate block into the document but into the flow of the document. That is to say, an image is treated the same way as text:</p>
<div id="cb7" class="sourceCode">
<pre class="sourceCode html"><code class="sourceCode html"><a id="cb7-1" class="sourceLine" data-line-number="1"></a><span class="kw">&lt;p&gt;</span>This is text with an <span class="kw">&lt;img</span><span class="ot"> src=</span><span class="st">"image.jpg"</span><span class="kw">&gt;</span> embedded
<a id="cb7-2" class="sourceLine" data-line-number="2"></a>   in it<span class="kw">&lt;/p&gt;</span></code></pre>
</div>
<p>In the above example, the image is embedded in the flow of the text, not apart from it. If the image is not available, and <strong>alt</strong> text is available, it is displayed in place of the image:</p>
<div id="cb8" class="sourceCode">
<pre class="sourceCode html"><code class="sourceCode html"><a id="cb8-1" class="sourceLine" data-line-number="1"></a><span class="kw">&lt;p&gt;</span>This is text with an <span class="kw">&lt;img</span><span class="ot"> src=</span><span class="st">"image.jpg"</span><span class="ot"> alt=</span><span class="st">"image"</span><span class="kw">&gt;</span>
<a id="cb8-2" class="sourceLine" data-line-number="2"></a>  embedded in it.<span class="kw">&lt;/p&gt;</span></code></pre>
</div>
<p>The following rules govern how images and <strong>alt</strong> text are displayed by <strong>clients</strong> (or <strong>user agents</strong>):</p>
<ol type="1">
<li>If <strong>src</strong> is set and <strong>alt</strong> is set to “” (an empty string):
<ul>
<li>The image is decorative or supplemental to the content.</li>
<li>If the image is available and the user agent is configured to display that image, then the image is displayed.</li>
<li>Otherwise, the element represents nothing, and may be omitted completely.</li>
</ul>
</li>
<li>If <strong>src</strong> is set and <strong>alt</strong> is set to a non-empty string:
<ul>
<li>The image is an important part of the content; the <strong>alt</strong> attribute gives a text equivalent for the image.</li>
<li>If the image is available and the user agent is configured to display that image, then the image is displayed.</li>
<li>Otherwise, the element represents the text assigned to the <strong>alt</strong> attribute.</li>
</ul>
</li>
<li>If <strong>src</strong> is set and there is no <strong>alt</strong> attribute:
<ul>
<li>There is no textual equivalent for the image.</li>
<li>If the image is available and the user agent is configured to display that image, then the image is displayed.</li>
<li>Otherwise, the user agent should display some indication that there is an image that is not being displayed.</li>
</ul>
</li>
<li>If there is no <strong>src</strong> attribute and the <strong>alt</strong> attribute is set to a non-empty string:
<ul>
<li>The value of the <strong>alt</strong> attribute is displayed.</li>
</ul>
</li>
<li>If there is no <strong>src</strong> attribute and the <strong>alt</strong> attribute is set to an empty string (or there is no <strong>alt</strong> attribute):
<ul>
<li>There is nothing to display.</li>
</ul>
</li>
</ol>
<h2 id="practical-considerations">Practical Considerations</h2>
<p>It should be obvious that, except for images like emojis or complex mathematical expressions, we, generally, do not embed images into the flow of the text. So the normal way to display images is to enclose them in their own paragraph tag (or some other tag to enclose them):</p>
<div id="cb9" class="sourceCode">
<pre class="sourceCode html"><code class="sourceCode html"><a id="cb9-1" class="sourceLine" data-line-number="1"></a><span class="kw">&lt;p&gt;&lt;img</span><span class="ot"> src=</span><span class="st">"image.jpg"</span>
<a id="cb9-2" class="sourceLine" data-line-number="2"></a><span class="ot">  alt=</span><span class="st">"a summary description of the image"</span><span class="kw">&gt;&lt;/p&gt;</span></code></pre>
</div>
<p>This way, if you display multiple images, they stack up one on top of the other instead of flowing, like text, one after the other.<br />
If the image is important to the document, it should have an <strong>alt</strong> attribute which gives a textual representation of the image. This way, if the image cannot be displayed, the user can still get a textual representation of the image.<br />
If the image is only for decoration or, otherwise, unimportant, then set the <strong>alt</strong> attribute equal to the empty string (<strong>“”</strong>). This way, if the image cannot be displayed, the user won’t know about it (otherwise, the browser will indicate a missing image).</p>
<h2 id="summary">Summary</h2>
<ul>
<li>The <span class="kbd">&lt;img&gt;</span> tag is a single tag with no closing tag and no content.</li>
<li>The <span class="kbd">&lt;img&gt;</span> tag <strong>embeds</strong> the specified source image into the flow of the document content.</li>
<li>The <strong>src</strong> attribute specifies the name of the image and its location.</li>
<li>The <strong>alt</strong> attribute specifies text to be displayed if the image cannot be displayed.</li>
<li>To ensure images stack one on top of the other, place the image inside a paragraph.</li>
</ul>
<h2 id="playing-around">Playing Around</h2>
<p>The best way to learn is to play around with what you have learned.<br />
Some webpage ideas are:</p>
<ul>
<li>update one of your previous webpages to include an image &#8211; or several images</li>
<li>create a rebus<a id="fnref3" class="footnote-ref" href="#fn3"><sup>3</sup></a></li>
</ul>
<h2 id="references">References</h2>
<ol type="1">
<li><a href="https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element"><span class="kbd">&lt;img&gt;</span> tag</a></li>
<li><a href="https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-src"><strong>src</strong> attribute</a></li>
<li>[<strong>alt</strong> attribute]https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-alt)</li>
</ol>
<div class="prev">
<a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/a-recipe-example-using-lists">Prev</a>
</div>
<div class="next">
<a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/a-recipe-example-with-an-image">Next</a>
</div>
<div class="clear"></div>
<section class="footnotes">
<hr />
<ol>
<li id="fn1">Relative paths follow the Unix / Linux convention:
<ul>
<li><strong>.</strong> &#8211; the current path. The paths <strong>image.jpg</strong> and <strong>./image.jpg</strong> both refer to the file <strong>image.jpg</strong> in the current directory.</li>
<li><strong>..</strong> &#8211; one level up. So <strong>../img/image.jpg</strong> refers to the file <strong>image.jpg</strong> which is located in the <strong>img</strong> folder which can be accessed by going up one directory level. In other words, the current directory and the <strong>img</strong> directory are siblings.</li>
</ul>
<p><a class="footnote-back" href="#fnref1">↩</a></li>
<li id="fn2">You should avoid making up domain names for illustrative purposes. The domain <strong>example.com</strong> has been permanently reserved for use as an example URL.<a class="footnote-back" href="#fnref2">↩</a></li>
<li id="fn3">A rebus is a puzzle that uses pictures to represent words or parts of words. Consider the following:
<p style="font-size: 2em;"><img decoding="async" style="display: inline;" src="https://complete-concrete-concise.com/wp-content/uploads/2018/03/can.png" alt="Can" width="37" height="64" /> U <img decoding="async" style="display: inline;" src="https://complete-concrete-concise.com/wp-content/uploads/2018/03/plane.png" alt="plane" width="96" height="48" /> &#8211; N 2 + <img decoding="async" style="display: inline;" src="https://complete-concrete-concise.com/wp-content/uploads/2018/03/sundae.png" alt="sundae" width="39" height="48" /> &#8211; <img loading="lazy" decoding="async" style="display: inline;" src="https://complete-concrete-concise.com/wp-content/uploads/2018/03/sun.png" alt="sun" width="46" height="48" />?</p>
<p>Can U (plane &#8211; N) (two + sundae &#8211; sun)? = Can you play today?<a class="footnote-back" href="#fnref3">↩</a></li>
</ol>
</section>
<p>The post <a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/images-in-html/">Images in HTML</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
