June 1, 2024
Adding Visual Appeal with Images and Media
Images and media can significantly enhance the visual appeal and interactivity of your web pages. In this chapter, you will learn how to use HTML to add images, make them responsive, and embed audio and video.
The <img>
tag is used to embed images in an HTML document. The src
attribute specifies the path to the image, and the alt
attribute provides alternative text for accessibility.
Example:
<img src="path/to/image.jpg" alt="Description of the image">
To make images responsive and adaptable to different screen sizes, you can use the srcset
and sizes
attributes.
Example:
<img src="image-small.jpg" srcset="image-small.jpg 500w, image-medium.jpg 1000w, image-large.jpg 1500w" sizes="(max-width: 600px) 480px, (max-width: 1200px) 800px, 1200px" alt="Description of the image">
HTML5 introduced the <audio>
tag to embed audio files. You can provide multiple audio sources for better compatibility across different browsers.
Example:
<audio controls> <source src="audio-file.mp3" type="audio/mpeg"> <source src="audio-file.ogg" type="audio/ogg"> Your browser does not support the audio element. </audio>
The <video>
tag is used to embed video files. Like the <audio>
tag, you can provide multiple sources.
Example:
<video controls> <source src="video-file.mp4" type="video/mp4"> <source src="video-file.ogg" type="video/ogg"> Your browser does not support the video tag. </video>
media.html
.<img>
tag to add an image.srcset
and sizes
attributes.<audio>
tag to embed an audio file with multiple sources.<video>
tag to embed a video file with multiple sources.Example:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Images and Media</title> </head> <body> <h1>Images and Media</h1> <h2>Image</h2> <img src="path/to/image.jpg" alt="Description of the image"> <h2>Responsive Image</h2> <img src="image-small.jpg" srcset="image-small.jpg 500w, image-medium.jpg 1000w, image-large.jpg 1500w" sizes="(max-width: 600px) 480px, (max-width: 1200px) 800px, 1200px" alt="Description of the image"> <h2>Audio</h2> <audio controls> <source src="audio-file.mp3" type="audio/mpeg"> <source src="audio-file.ogg" type="audio/ogg"> Your browser does not support the audio element. </audio> <h2>Video</h2> <video controls> <source src="video-file.mp4" type="video/mp4"> <source src="video-file.ogg" type="video/ogg"> Your browser does not support the video tag. </video> </body> </html>
Save and open this file in your browser to see the images and media in action.
<img>
tag is used to embed images in HTML.srcset
and sizes
attributes.<audio>
and <video>
tags are used to embed audio and video files, respectively.Next Steps
In the next chapter, we’ll explore how to create and style tables in HTML, making your data more organized and visually appealing. Keep practicing to master embedding images and media in your web pages.
Please Sign In to post a comment.