Overview
The simplest way to place an image only requires the src attribute and the alt attribute. The src is the location of the image and the alt specifies an alternate text for an image if the image cannot be displayed and for screen readers to use for visually impaired users.
<img decoding="async" src="/images/logo.png" alt="1SMARTchicken logo" />
A better way to place an image is to also specify the width and height of the image. You can specify the width and height in terms of either pixels or percentage of its actual size.
Specifying the size of your image in the <img> tag, is a best practice, as it will stop the screen from reflowing the text as the image is placed. It makes for a more pleasant experience.
<img loading="lazy" decoding="async" src="/images/logo.png" alt="1SMARTchicken logo" width="100" height="85" loading="lazy" />
<img decoding="async" src="/images/logo.png" alt="1SMARTchicken logo" width="75%" height="75%" loading="lazy" />
Note
When specifying the size of the image in pixels, there is no need to use px (100px).
Lazy Loading Images
There is a loading attribute that can also be added to the <img> tag. The loading attribute specifies whether a browser should load an image immediately or whether it should defer loading of off-screen images until they are scrolled to. However, not all browsers will honor it.
<img decoding="async" src="/images/logo.png" alt="1SMARTchicken logo" loading="lazy" />
Note
The loading attribute should only be added to images which are initially positioned out of view.
Attributes
The following attributes can be used within the <img> tag.
Attribute | Value | Description |
---|---|---|
alt | text | Specifies an alternate text for an image (required) |
crossorigin | anonymous use-credentials | Allow images from third-party sites that allow cross-origin access to be used with canvas |
height | pixels | Specifies the height of an image |
ismap | ismap | Specifies an image as a server-side image map |
loading | eager lazy | Specifies whether a browser should load an image immediately or to defer loading of images until some conditions are met |
longdesc | URL | Specifies a URL to a detailed description of an image |
referrerpolicy | no-referrer no-referrer-when-downgrade origin origin-when-cross-origin unsafe-url | Specifies which referrer information to use when fetching an image |
sizes | sizes | Specifies image sizes for different page layouts |
src | URL | Specifies the path to get the image |
srcset | URL-list | Specifies a list of image files to use in different situations |
usemap | #mapname | Specifies an image as a client-side image map |
width | pixels | Specifies the width of an image |
HTML Notes:
- In our HTML section the term “tag” and “element” are often used interchangeably to refer to both the tag used to create a page element and the element created by the tag (<p> tag = <p> element = paragraph on the page)
- HTML5 is not case sensitive; so <P> is the same as <p>, <H1> is the same as <h1>
- Global attributes can be used with all HTML tags and are therefore not mentioned on every tag page
- To write clean, readable HTML code, it is best to use indentation whereas elements within elements are indented (tabbed or spaces) to create something that looks like a project outline
- The browser will automatically remove any extra spaces and lines in your HTML code when the page is displayed
- Double quotes or single quotes can be used around HTML attribute values, but when the attribute value itself contains one form of quote, it will be necessary to use the other around the attribute
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.