Hello, young web designer! In this tutorial, we'll learn about HTML images and how to add them to your web pages. Images can make your web pages more engaging and interesting. Let's get started!
The <img>
Element
To add an image to your web page, you'll use the <img>
element. The <img>
tag is unique because it doesn't need a closing tag like most other HTML elements. Instead, it's a self-closing tag, which means it ends with />
.
Image Attributes
When using the <img>
element, there are two important attributes you need to include:
src
: This attribute specifies the location (URL) of the image file. It can be a local file in the same folder as your HTML file or a file hosted on the internet. Example: <img src="cute-kitten.jpg">
alt
: This attribute provides a text description of the image, which is helpful for accessibility (for example, screen readers for visually impaired users) and when the image cannot be displayed. Example: <img src="cute-kitten.jpg" alt="A cute kitten playing with a ball of yarn">
Step 1: Find an image you want to use. Make sure you have permission to use it, and save it to the same folder as your HTML file.
Step 2: Open the HTML file you created in the previous tutorial or create a new one.
Step 3: Add the <img>
element with the src
and alt
attributes. For example:
html<img src="cute-kitten.jpg" alt="A cute kitten playing with a ball of yarn">
Step 4: Save your file and open it in a web browser to see the image displayed on your web page!
width
and height
attributes. These attributes specify the dimensions of the image in pixels. For example:html<img src="cute-kitten.jpg" alt="A cute kitten playing with a ball of yarn" width="300" height="200">
However, keep in mind that resizing images this way may cause them to lose quality or become distorted. It's best to use an image editing tool to resize your images before adding them to your web page.
width
and height
attributes. Don't forget to include the alt
attribute for each image to provide a text description.Images are an essential part of web design, making your web pages more engaging and visually appealing. Keep practicing, and soon you'll be creating amazing web pages filled with beautiful images!