Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Images | Working with Advanced Elements
HTML for Beginners

book
Images

Images can be created with the <img> tag. Unlike most tags you have met, the <img> tag doesn't have a closing tag.

The src attribute is used to specify the file name (link to the file) of the image you add to the web page.

html

index.html

copy
<!DOCTYPE html>
<html>
<head> </head>
<body>
<img
src="https://gardenerspath.com/wp-content/uploads/2017/10/Cozy-Up-with-Chamomile.jpg"
alt="Beautiful white flower"
width="300"
height="200"
/>
</body>
</html>

Here is a list of common image formats supported by all browsers: GIF, JPEG, PNG, SVG.

Picture element

Sometimes, you want to display different images for devices like desktops and mobile phones. The <picture> element allows us to select several sources for various screen sizes.

To do the above task, we will use the <source> tag along with media and srcset attributes.

html

index.html

copy
<!DOCTYPE html>
<html>
<head> </head>
<body>
<picture>
<source
media="(min-width: 650px)"
srcset="https://kvitkainfo.com/uploads/images/default/romashka_velika.jpg" />
<source
media="(min-width: 465px)"
srcset="https://phytosvit.com/wp-content/uploads/2021/07/romas-1.jpg" />
<img
src="https://gardenerspath.com/wp-content/uploads/2017/10/Cozy-Up-with-Chamomile.jpg" />
</picture>
</body>
</html>

As you can see, we use the <source> tag inside the picture tag. Then we use the media and srcset attribute to specify which image to be shown based on the device size. Usually, we use the screen size to separate different types of devices like mobile phones, tabs, and laptops.

question mark

What is the incorrect attribute to be used with tag?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3
We use cookies to make your experience better!
some-alt