HTML - Images



Images in HTML provide visual content for web pages, enhancing user experiences and conveying information. They can be photographs, graphics, icons, or illustrations.

HTML offers various elements for embedding, manipulating, and controlling images, contributing to the aesthetics and functionality of websites. Understanding image tags, attributes, and responsive design principles is essential for effective web development.

Insert Image

You can insert any image in your web page by using <img> tag. Following is the syntax to use this tag.

<img src="Image URL" ... attributes-list/>

The <img> tag is an empty tag, which means that, it can contain only list of attributes and it has no closing tag.

To learn about attributes in Images click here.

Example

To try following example, let's keep our HTML file test.htm and image file test.png in the same directory −

<DOCTYPE html>
<html>
<head>
   <title>Using Image in Webpage</title>
</head>
<body>
   <p>Simple Image Insert</p>
   <img src="/html/images/test.png" alt="Test Image" />
</body>
</html>

You can use PNG, JPEG, or GIF image files based on your comfort but make sure you specify the correct image file name in src attribute. The image name is always case sensitive.

The alt attribute is a mandatory attribute that specifies an alternate text for an image if the image cannot be displayed.

Set Image Location

Usually, we keep all the images in a separate directory. So let's keep HTML file test.htm in our home directory and create a subdirectory images inside the home directory where we will keep our image test.png.

Example

Assuming our image location is "/html/images/test.png", try the following example −

<!DOCTYPE html>
<html>
<head>
   <title>Using Image in Webpage</title>
</head>
<body>
   <img src="/html/images/test.png" alt="Test Image" />
</body>
</html>

Set Image Width/Height

You can set image width and height based on your requirement using width and height attributes. You can specify width and height of the image in terms of either pixels or percentage of its actual size.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Set Image Width and Height</title>
</head>
<body>
   <p>Setting image width and height</p>
   <img src="/html/images/test.png" alt="Test Image" width="450" height="200" />
</body>
</html>

Set Image Border

By default, image will have a border around it, you can specify border thickness in terms of pixels using border attribute. A thickness of 0 means, no border around the picture.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Set Image Border</title>
</head>
<body>
   <p>Setting image Border</p>
   <img src="/html/images/test.png" alt="Test Image" border="3" />
</body>
</html>

Set Image Alignment

By default, image will align at the left side of the page, but you can use align attribute to set it in the center or right.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Set Image Alignment</title>
</head>
<body>
   <p>Setting image Alignment</p>
   <img src="/html/images/test.png" alt="Test Image" border="3" align="right" />
</body>
</html>

Free Web Graphics

For Free Web Graphics including patterns you can look into Free Web Graphics

Advertisements