Table of Contents
ToggleHTML Images Syntax
The syntax for adding images in HTML using the<img>
tag is as follows:
<img src="image-url.jpg" alt="alternative text" width="width-in-pixels" height="height-in-pixels">
Let’s take a closer look at each of the attributes:
src
(required): This attribute specifies the URL or file path of the image. This can be a local file or a URL to an image hosted online.alt
(required): This attribute specifies the alternative text that will be displayed if the image fails to load, or if the user is using a screen reader to browse the web. This is important for accessibility purposes and can also help with SEO.width
(optional): This attribute specifies the width of the image in pixels. If you don’t specify a width, the image will be displayed at its actual size.height
(optional): This attribute specifies the height of the image in pixels. If you don’t specify a height, the image will be displayed at its actual size.
Here’s an example of using the<img>
tag to add an image to an HTML document:
<img src="https://example.com/images/picture.jpg" alt="A picture of something" width="500" height="300">
In this example, we’re using an image hosted online at the URLhttps://example.com/images/picture.jpg
. We’re also specifying alternative text, a width of 500 pixels, and a height of 300 pixels.
It’s also a good practice to include thealt
attribute even if the image is purely decorative or not essential to the content. In this case, you can set thealt
attribute to an empty string, like this:
<img src="https://example.com/images/decorative-image.jpg" alt="">
This tells screen readers and other assistive technologies that the image is decorative and can be safely ignored.
Width and Height, or Style(HTML Images)?
In HTML, you can set the width and height of an image using either attributes or CSS styles.
Using Attributes:
As I mentioned earlier, you can use thewidth
andheight
attributes to set the dimensions of an image, like this:
<img src="image-url.jpg" alt="alternative text" width="300" height="200">
Using CSS Styles:
Alternatively, you can use CSS to set the width and height of an image, like this:
<img src="image-url.jpg" alt="alternative text" style="width: 300px; height: 200px;">
HTML Images: In this example, we’re using thestyle
attribute to apply inline CSS to the image element. Thestyle
attribute contains a set of CSS declarations, separated by semicolons, that determine the image’s width and height.
Which Method to Use?
Both methods are valid, and it’s largely a matter of personal preference which one you use. However, using CSS styles provides more flexibility and control over the appearance of your web page. With CSS, you can apply other styles to the image, such as borders, margins, and padding, and you can easily change the dimensions of the image without modifying the HTML code.
In addition, using CSS to set the dimensions of images can improve the loading speed of your web page. When you use thewidth
andheight
attributes, the browser needs to download the entire image file to determine its dimensions. But when you use CSS, the browser can render the page layout without waiting for the image to download, and then retrieve the image file and scale it to the specified dimensions. Optimizing website performance by reducing page load time can significantly enhance user satisfaction and engagement.
The src Attribute(HTML Images)
Thesrc
attribute in HTML is used to specify the source (i.e., the URL or file path) of an external resource that should be embedded in an HTML document. When it comes to images, thesrc
attribute is used to specify the URL or file path of the image file that should be displayed on the web page.
Here’s an example of using thesrc
attribute to embed an image in an HTML document:
<img src="https://example.com/images/picture.jpg" alt="A picture of something">
HTML Images: In this example, thesrc
attribute specifies the URL of the image filehttps://example.com/images/picture.jpg
. Thealt
attribute provides alternative text that will be displayed if the image fails to load or if the user is using a screen reader to browse the web.
It’s important to note that thesrc
attribute is a required attribute for the<img>
tag. If you don’t specify a value for thesrc
attribute, the image will not be displayed on the web page.
HTML Images: In addition, it’s good practice to use a relative URL or file path when specifying thesrc
attribute for images that are hosted on the same server as the HTML document. This can help ensure that the image loads quickly and reduces the load on the server. If you’re linking to an external image hosted on a different server, you’ll need to use the full URL to the image file in thesrc
attribute.
The alt Attribute
Thealt
attribute in HTML is used to provide alternative text for an image. It is a required attribute for the<img>
tag, and is used to describe the content of the image in case the image cannot be displayed, or is being accessed by a user with a screen reader.
Here’s an example of using thealt
attribute in an<img>
tag:
<img src="example.jpg" alt="A picture of a mountain landscape">
HTML Images:
In this instance, the alt attribute serves as a concise description of the image, ensuring accessibility for users with vision impairments or slow internet connections. The text “A picture of a mountain landscape” is a description of the image, and should be concise, accurate and descriptive.
It’s important to provide an accurate and informativealt
text for images, as it can greatly improve the accessibility and usability of your web page. If thealt
attribute is not provided, or if it is left empty, it can be difficult for users who rely on screen readers to understand the content of the page.
HTML Images: In addition, thealt
text can also be helpful for search engine optimization (SEO), as search engines can use it to understand the content of an image and improve the relevance of search results.
Image Size – Width and Height
You can specify the size of an image in HTML using thewidth
andheight
attributes in the<img>
tag. The pixel dimensions of an image are specified by the width and height attributes.
Here’s an example:
<img src="example.jpg" width="500" height="300" alt="A picture of a mountain landscape">
HTML Images: In this example, thewidth
andheight
attributes are used to set the dimensions of the image to 500 pixels wide and 300 pixels tall.
It’s important to note that specifying thewidth
andheight
attributes can impact the page’s layout and load time. When the browser knows the dimensions of an image, it can reserve the space for the image on the page, which can help prevent layout shifts when the image is loaded. Additionally, specifying the dimensions of the image can help the browser load the page more quickly, since it knows how much space to allocate for the image before it is loaded.
However, it’s generally not recommended to use thewidth
andheight
attributes to scale images up or down. Doing so can result in pixelated or distorted images, and can also cause the page to load more slowly if the image needs to be scaled. Instead, it’s best to use image editing software to resize images to the desired dimensions before uploading them to your website.
Images in Another Folder
If your image is located in another folder or directory on your website, you can specify the file path to the image in thesrc
attribute of the<img>
tag.
Here’s an example of specifying an image located in a folder called “images”:
<img src="images/example.jpg" alt="A picture of a mountain landscape">
HTML Images: In this example, thesrc
attribute specifies the path to the image file relative to the location of the HTML file. So if the HTML file is located in the root directory of your website, and the “images” folder is also located in the root directory, you can use the file path “images/example.jpg” to reference the image.
If the image is located in a different directory that is not in the same folder as the HTML file, you would need to adjust the file path accordingly. For example, if the image is located in a folder called “pictures” inside a folder called “assets”, you would use the following file path:
<img src="assets/pictures/example.jpg" alt="A picture of a mountain landscape">
It’s important to note that when referencing images located in different folders, the file path is case-sensitive and should match the file name and folder names exactly.
Images on Another Server/Website
If you want to display an image that is located on another server or website, you can use the full URL of the image in thesrc
attribute of the<img>
tag.
Here’s an example:
<img src="https://example.com/images/example.jpg" alt="A picture of a mountain landscape">
HTML Images: In this example, the full URL of the image is specified in thesrc
attribute, including thehttps://
protocol and the domain name of the website where the image is hosted.
It’s important to note that when using images hosted on another server or website, you should make sure that you have permission to use the image and that you’re not infringing on any copyright or intellectual property rights. Additionally, using images hosted on another server can impact the performance of your website, as the image needs to be downloaded from the external server each time the page is loaded.
Image as a Link
You can use an image as a link in HTML by wrapping an<a>
tag around the<img>
tag and specifying the URL you want the image to link to in thehref
attribute of the<a>
tag.
Here’s an example:
<a href="https://example.com"> <img src="example.jpg" alt="A picture of a mountain landscape"> </a>
HTML Images: In this example, the<a>
tag wraps around the<img>
tag to create a link, and thehref
attribute specifies the URL you want the image to link to. When the user clicks on the image, they will be directed to the URL specified in thehref
attribute.
It’s important to include alternative text for the image in thealt
attribute, even when using the image as a link. This provides a description of the image for users who are unable to see it, such as those using screen readers or with images turned off in their browser.
Image Floating
You can float an image to the left or right of surrounding text in HTML by using thefloat
CSS property. Thefloat
property allows you to specify whether an element should be floated to the left or right, and any surrounding content will flow around the floated element.
Here’s an example of floating an image to the right of some text:
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam lacinia leo quis justo interdum, non euismod nunc euismod. Duis tincidunt sagittis turpis, at ullamcorper augue eleifend vel. Ut volutpat lectus in erat pulvinar, sed tincidunt felis molestie. <img src="example.jpg" alt="A picture of a mountain landscape" style="float: right; margin: 0 0 10px 10px;"> Vivamus vel sapien tellus. Morbi facilisis velit eget sem sodales, a sollicitudin tellus blandit. Nulla facilisi. Etiam id eros eu libero elementum aliquet in eu turpis. Praesent vel eros quam. </p>
HTML Images: In this example, the image is floated to the right using thefloat: right
CSS property in thestyle
attribute. Themargin
property is also used to create some space around the image, with0 0 10px 10px
specifying a margin of 0 pixels at the top and bottom, and 10 pixels on the right and left.
To float an image to the left, you can usefloat: left
instead. It’s important to note that when floating an element, you should also consider adding clear rules to prevent unwanted overlap with other elements on the page. This can be achieved using theclear
CSS property.