HTML5: How to specify the width of an image?
Solution:
HTML Code:
Explanation:
- This HTML code demonstrates how to specify the width of an image using the width 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/layout.gif".
- 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 tag example".
- The width attribute specifies the width of the image. In this example, it is set to 300 pixels.
- The height attribute specifies the height of the image. In this example, it is set to 200 pixels.
- When the HTML document is rendered, the browser displays the image with the specified dimensions.
Live Demo:
See the solution in the browser
Supported browser
![]() |
![]() |
![]() |
![]() |
![]() |
Yes | Yes | Yes | Yes | Yes |
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.