Image in HTML
Image in HTML
HTML img tag is used to display image on the web page. Images can
improve the design and the appearance of a web page.
Using images in HTML is a fundamental aspect of web development, allowing you to
enhance the visual appeal and informational content of your web pages.
Images can be anything from photographs and illustrations to logos and icons.
HTML img tag is an empty tag that contains attributes only, closing
tags are not used in HTML image element.
Images are not technically inserted into a web page; images are linked to
web pages. The <img> tag creates a holding space for the referenced image.
The <img> tag is empty, it contains attributes only, and does not have a
closing tag.
The `<img>` tag has several attributes that control various aspects of how the image is
displayed:
- `src`: Specifies the source URL or path of the image file. This attribute is required.
- `alt`: Provides alternative text for the image, which is displayed if the image cannot
be loaded or for accessibility purposes.
- `title`: Provides additional information about the image when the user hovers over it.
- `width` and `height`: Sets the dimensions of the image. It's recommended to use
these attributes to prevent layout shifts when images load.
- `align`: Specifies how the image aligns with the surrounding content. However, this
attribute is no longer supported in HTML5, and CSS should be used for alignment.
2. Image Sources:
- Data URLs: Embedding images directly into HTML using base64-encoded data.
3. Common Image Format: Here is the commonly used image file format
that is supported by all the browsers.
Abbreviatio
S.No. n File Type Extension
The `alt` attribute in the `<img>` tag is crucial for accessibility. Screen readers use the alt
text to describe the image to visually impaired users.
Images can be placed within various HTML elements, including paragraphs, headings, lists,
and more. You can use CSS to control the styling and layout of images, such as margins,
borders, shadows, and responsiveness.
<!DOCTYPE>
<html>
<body>
<h2>HTML Image Example</h2>
<img src="good-morning.jpg" alt="Good Morning Friends"/>
</body>
</html>
Output:
Example:
<!DOCTYPE html>
<html>
<head>
<title>Image tag</title>
</head>
<body>
<img src="https://static.javatpoint.com/htmlpages/images/animal.jpg"
height="180" width="300" alt="animal image">
</body>
</html>
Output:
HTML image example with height and width
Example:
<!DOCTYPE html>
<html>
<head>
<title>Image tag</title>
</head>
<body>
<h2>Use image as a link</h2>
<a href="https://www.javatpoint.com/what-is-robotics"><img
src="https://static.javatpoint.com/htmlpages/images/robot.jpg"
height="100" width="100"></a>
</body>
</html>
Output:
<!DOCTYPE html>
<html>
<head>
<style>
img {
width: 100%;
}
</style>
</head>
<body>
</body>
</html>