You can use the width
and height
attributes in HTML to specify the size of an image that is embedded in a web page. These attributes allow you to control the width and height of the image in pixels.
Here’s an example of how to use the width
and height
attributes to specify the size of an image:
<img src="https://www.example.com/images/kitten.jpg" alt="A cute kitten" width="400" height="300">
In this example, the width
attribute is set to 400
pixels and the height
attribute is set to 300
pixels. This will make the image 400 pixels wide and 300 pixels tall when displayed in the web page.
Note that when you specify the size of an image using the width
and height
attributes, the image may be distorted if the aspect ratio of the original image is not preserved. To avoid distortion, you can either specify both the width
and height
attributes, or specify only one of the attributes and let the other attribute be automatically calculated based on the aspect ratio of the original image.
It’s generally a good practice to specify the size of your images using the width
and height
attributes, as this can improve the performance of your web page by allowing the browser to reserve space for the image before it is loaded. This can help to avoid layout shifts and improve the perceived speed of your web page.