Definition
The <img>
HTML element is used to insert an image into a web page. It is a self-closing element, meaning it does not have a closing tag. The src
attribute is required and specifies the URL or file path of the image to be displayed.
Here’s an example of how to use the <img>
element:
<img src="image.jpg" alt="Description of the image" />
In this example, the src
attribute specifies the path or URL of the image file “image.jpg”. The alt
attribute provides alternative text that is displayed when the image cannot be loaded or for accessibility purposes. It is recommended to always include the alt
attribute with a descriptive text that conveys the meaning or content of the image.
You can also specify additional attributes to control the size, alignment, and other properties of the image. For instance:
<img
src="image.jpg"
alt="Description of the image"
width="300"
height="200"
class="my-image"
style="border: 1px solid black;"
/>
In this example, the width
and height
attributes set the dimensions of the image, while the class
attribute assigns a CSS class to the image for styling purposes. The style
attribute allows inline CSS to be applied directly to the image.
It’s important to ensure that the image file is available at the specified URL or path. Additionally, consider optimizing the image file size and using appropriate image formats (such as JPEG, PNG, or SVG) to balance quality and performance.