w3resource

HTML5: How to specify the URL of an image?

Go to Exercise page

Solution :

HTML Code:

<!DOCTYPE html><!-- Declaration specifying the document type -->
<html><!-- Opening tag for the root element of the HTML document -->
<head><!-- Opening tag for the document's header section -->
<meta charset="utf-8"><!-- Metadata tag specifying the character encoding of the document -->
<title>How to specify the URL of an image</title><!-- Title of the HTML document -->
</head><!-- Closing tag for the document's header section -->
<body><!-- Opening tag for the document's body section -->
<p><!-- Opening tag for a paragraph element -->
<img src="https://www.w3resource.com/images/w3resource-logo.png" alt="html img src tag example" width="50%" height="50"><!-- Image element specifying the source URL of the image, alternate text, and dimensions -->
</p><!-- Closing tag for the paragraph element -->
</body><!-- Closing tag for the document's body section -->
</html><!-- Closing tag for the root element of the HTML document -->

Explanation:

  • This HTML code demonstrates how to specify the URL of an image using the src attribute within the <img> element.
  • The <img> element is a self-closing tag used to embed images into an HTML document.
  • The src attribute specifies the URL of the image to be displayed. In this example, it points to "https://www.w3resource.com/images/w3resource-logo.png".
  • The alt attribute provides alternate text for the image, which is displayed if the image cannot be loaded or for accessibility purposes. In this case, it is set to "html img src tag example".
  • The width attribute specifies the width of the image. In this example, it is set to 50% of the viewport width.
  • The height attribute specifies the height of the image. In this example, it is set to 50 pixels.
  • When the HTML document is rendered, the browser displays the image with the specified URL and dimensions.
  • The image is wrapped inside a <p> (paragraph) element, which is commonly used to group content together.

Live Demo:

See the Pen img-src-answer by w3resource (@w3resource) on CodePen.


See the solution in the browser

Supported browser

Firefox logo Chrome logo Opera logo Safari logo Internet Explorer logo
YesYesYesYesYes

Go to Exercise page

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.