In HTML, the <img>
element is used to embed images into a web page. Here's a basic example of how the <img>
tag is used:
html<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Example</title>
</head>
<body>
<h1>My Web Page</h1>
<img src="example.jpg" alt="A descriptive text about the image">
</body>
</html>
In this example:
<img>
tag is used to embed an image.src
attribute specifies the source (URL or file path) of the image. In this case, it's "example.jpg".alt
attribute provides alternative text for the image. This text is displayed if the image cannot be loaded or for accessibility purposes.It's important to always include the alt
attribute to provide meaningful alternative text for accessibility and SEO.
You can also set the width and height of the image using the width
and height
attributes:
html<img src="example.jpg" alt="A descriptive text about the image" width="300" height="200">
Additionally, you can use the style
attribute to apply inline styles to the image:
html<img src="example.jpg" alt="A descriptive text about the image" style="border: 1px solid #ccc; padding: 10px;">
Remember to replace "example.jpg" with the actual path or URL of your image.