DEV Community

Cover image for HTML Images
johar ali
johar ali

Posted on

HTML Images

The img tag is used to add images to a webpage or website in HTML. This is an empty tag, meaning it has no closing tag, that is, there is no need to close this tag by writing tag. Img tag is a self-closing tag. Images are added to HTML to improve the design or structure of a webpage as well as to improve its quality. Nowadays, a website no longer adds images directly to a web page, because images are attached to web pages using the img tag which holds space for the image.

<img src=”image path with extension or URL”,..>
Enter fullscreen mode Exit fullscreen mode

Attributes of Img tag

src and alt, these two attributes are necessary for image tag.

src attribute: This indicates the path of the image. It means that the entire URL is written in the src attribute. There is src attribute, without it the image will not be displayed.

alt attribute: It is used to give a name to the image.

height and width attribute: To display the image properly in the web page, its length and width have to be set. Width and height attributes are used for that.

Here's an example:

<img src="example.jpg" alt="A beautiful landscape" width="400" height="300">
Enter fullscreen mode Exit fullscreen mode

Images can be in various formats, such as JPEG, PNG, GIF, or SVG, and you should choose the appropriate format depending on the content and your specific needs. Additionally, you can style images using CSS to control their appearance, position, and behavior on a web page.

Top comments (0)