{"id":77,"date":"2026-11-18T12:03:17","date_gmt":"2026-11-18T12:03:17","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=77"},"modified":"2026-11-18T12:03:47","modified_gmt":"2026-11-18T12:03:47","slug":"html-img-tag","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/html-img-tag\/","title":{"rendered":"HTML img Tag: src, alt, Size, Lazy Loading with Examples"},"content":{"rendered":"<p>Hello friends! A web page without images feels plain and boring, right? In HTML, adding a picture is very simple. The HTML <code>&lt;img&gt;<\/code> tag puts an image on a web page. It is an <strong>empty element<\/strong>: it has no closing tag and no content inside it. Everything is controlled through attributes, the two most important being <code>src<\/code> (where the image is) and <code>alt<\/code> (a text description of it). Let&#8217;s start with a quick look at all the attributes you will use.<\/p>\n<pre class=\"wp-block-code\"><code>&lt;img src=\"taj-mahal.jpg\" alt=\"The Taj Mahal in Agra at sunrise\"&gt;<\/code><\/pre>\n<h2>Important Attributes of the img Tag<\/h2>\n<table>\n<thead>\n<tr>\n<th>Attribute<\/th>\n<th>What it does<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>src<\/code><\/td>\n<td>Path or URL of the image file (required)<\/td>\n<\/tr>\n<tr>\n<td><code>alt<\/code><\/td>\n<td>Text shown if the image can&#8217;t load, and read aloud by screen readers (always add it)<\/td>\n<\/tr>\n<tr>\n<td><code>width<\/code>, <code>height<\/code><\/td>\n<td>Size in pixels; also lets the browser reserve space before the image loads<\/td>\n<\/tr>\n<tr>\n<td><code>loading=\"lazy\"<\/code><\/td>\n<td>Loads the image only when the user scrolls near it, which makes pages faster<\/td>\n<\/tr>\n<tr>\n<td><code>title<\/code><\/td>\n<td>Tooltip shown when you hover over the image<\/td>\n<\/tr>\n<tr>\n<td><code>srcset<\/code>, <code>sizes<\/code><\/td>\n<td>Give the browser different image sizes for different screens<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>src: Relative and Absolute Paths<\/h2>\n<pre class=\"wp-block-code\"><code>&lt;!-- image in the same folder as the HTML file --&gt;\n&lt;img src=\"logo.png\" alt=\"School logo\"&gt;\n\n&lt;!-- image inside an \"images\" folder --&gt;\n&lt;img src=\"images\/logo.png\" alt=\"School logo\"&gt;\n\n&lt;!-- image from a full URL --&gt;\n&lt;img src=\"https:\/\/example.com\/photos\/logo.png\" alt=\"School logo\"&gt;<\/code><\/pre>\n<p>If the image does not show, the path is wrong in most cases. Check the folder name, the spelling and the extension (<code>.jpg<\/code> vs <code>.jpeg<\/code> vs <code>.png<\/code>). On most web servers file names are case-sensitive, so <code>Logo.PNG<\/code> and <code>logo.png<\/code> are different files.<\/p>\n<h2>Why alt Text Matters<\/h2>\n<p>Many beginners skip the <code>alt<\/code> attribute, but it is more important than it looks. Here is why:<\/p>\n<ul>\n<li><strong>Accessibility:<\/strong> screen readers read the alt text to people who cannot see the image.<\/li>\n<li><strong>Broken images:<\/strong> if the image fails to load, the alt text is shown instead.<\/li>\n<li><strong>SEO:<\/strong> search engines use alt text to understand what the image shows.<\/li>\n<\/ul>\n<p>Describe the image in a short, useful way: <code>alt=\"Students planting trees in the school garden\"<\/code> is far better than <code>alt=\"image1\"<\/code>. For purely decorative images, use an empty alt (<code>alt=\"\"<\/code>) so screen readers skip them.<\/p>\n<h2>Setting the Size<\/h2>\n<pre class=\"wp-block-code\"><code>&lt;img src=\"tiger.jpg\" alt=\"Bengal tiger resting in the grass\" width=\"400\" height=\"300\"&gt;<\/code><\/pre>\n<p>Always give both <code>width<\/code> and <code>height<\/code> that match the image&#8217;s real proportions. The browser then reserves the right space, so the page does not jump while images load. To make an image shrink on small screens, add CSS:<\/p>\n<pre class=\"wp-block-code\"><code>img {\n  max-width: 100%;\n  height: auto;\n}<\/code><\/pre>\n<h2>Lazy Loading for Faster Pages<\/h2>\n<p>If your page has many images, here is a simple trick to make it load faster:<\/p>\n<pre class=\"wp-block-code\"><code>&lt;img src=\"gallery-photo.jpg\" alt=\"Annual day group photo\" width=\"800\" height=\"533\" loading=\"lazy\"&gt;<\/code><\/pre>\n<p>Images further down the page are loaded only when needed. Don&#8217;t lazy-load the main image at the very top of the page, because it should load immediately.<\/p>\n<h2>Making an Image a Link<\/h2>\n<pre class=\"wp-block-code\"><code>&lt;a href=\"https:\/\/gyanipandit.com\/programming\/\"&gt;\n  &lt;img src=\"logo.png\" alt=\"GyaniPandit Programming home\"&gt;\n&lt;\/a&gt;<\/code><\/pre>\n<p>Read more: <a href=\"https:\/\/gyanipandit.com\/programming\/html-image-link\/\">HTML image link<\/a><\/p>\n<h2>Image with a Caption: figure and figcaption<\/h2>\n<pre class=\"wp-block-code\"><code>&lt;figure&gt;\n  &lt;img src=\"red-fort.jpg\" alt=\"Red Fort in Delhi\" width=\"600\" height=\"400\"&gt;\n  &lt;figcaption&gt;The Red Fort, Delhi&lt;\/figcaption&gt;\n&lt;\/figure&gt;<\/code><\/pre>\n<h2>Which Image Format Should You Use?<\/h2>\n<p>You may have noticed that images come in different formats like JPG, PNG and WebP. So which one should you pick? Here is a quick guide:<\/p>\n<table>\n<thead>\n<tr>\n<th>Format<\/th>\n<th>Best for<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>JPEG \/ JPG<\/td>\n<td>Photos<\/td>\n<\/tr>\n<tr>\n<td>PNG<\/td>\n<td>Screenshots, images needing a transparent background<\/td>\n<\/tr>\n<tr>\n<td>WebP \/ AVIF<\/td>\n<td>Modern formats with much smaller files; supported by current browsers<\/td>\n<\/tr>\n<tr>\n<td>SVG<\/td>\n<td>Logos and icons that must stay sharp at any size<\/td>\n<\/tr>\n<tr>\n<td>GIF<\/td>\n<td>Simple short animations (video formats are often smaller)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Common Mistakes<\/h2>\n<p>Before we finish, watch out for these common mistakes:<\/p>\n<ul>\n<li>Writing a closing tag: <code>&lt;\/img&gt;<\/code> is not needed and is invalid.<\/li>\n<li>Leaving out <code>alt<\/code>, or filling it with keywords instead of a real description.<\/li>\n<li>Uploading a huge 5 MB photo and shrinking it with <code>width<\/code>. The user still downloads the full file. Resize and compress images before uploading.<\/li>\n<li>Using spaces in file names. Prefer <code>red-fort.jpg<\/code> over <code>red fort.jpg<\/code>.<\/li>\n<\/ul>\n<p>So friends, to sum up: the <code>&lt;img&gt;<\/code> tag has no closing tag, so both <code>&lt;img ...&gt;<\/code> and <code>&lt;img ... \/&gt;<\/code> work. Always add an <code>alt<\/code> text, because it is used by screen readers, shown when the image fails to load, and read by search engines. And if your image is not showing, the <code>src<\/code> path is usually the culprit, so check the folder, spelling, capital letters and the file extension. Add a few images to your own page and try these attributes yourself.<\/p>\n<p>Related: <a href=\"https:\/\/gyanipandit.com\/programming\/html-attributes\/\">HTML attributes<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/anchor-tag-html\/\">Anchor tag<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/css-images\/\">CSS images<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello friends! A web page without images feels plain and boring, right? In HTML, adding a picture is very simple. The HTML &lt;img&gt; tag puts an image on a web page. It is an empty element: it has no closing tag and no content inside it. Everything is controlled through attributes, the two most important &#8230; <a title=\"HTML img Tag: src, alt, Size, Lazy Loading with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/html-img-tag\/\" aria-label=\"Read more about HTML img Tag: src, alt, Size, Lazy Loading with Examples\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-html-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":4,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":6145,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/77\/revisions\/6145"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}