Images and media

Text carries a lot of a web page, but not all of it. Photos, diagrams, illustrations, video clips, and sound are content too, and HTML has elements for each of them. This chapter is about the markup that puts media on a page: how you point to an image file, how you describe it for people who cannot see it, and how you serve the right file to the right screen without wasting anyone's data.
The image element
You put a picture on a page with the image element, written as <img>. It has no content and no closing tag. Instead, you tell it two things through attributes: which file to show, and what the picture is of.
<img src="golden-retriever.jpg" alt="A golden retriever sitting in tall grass">src is the source: the path or web address of the image file. alt is the alternative text: a short description of the picture in words. If the image cannot load, or if someone is listening to the page with a screen reader instead of looking at it, the alt text is what they get. So write it as if you were describing the picture to a friend on the phone.
src, which is the file to show, and alt, which describes the picture in words. Write the alt as if you were telling someone on the phone what is in the photo. It is the bit that gets skipped most, and it is the bit that matters most. Responsive images
A photo that looks sharp on a laptop can be far bigger than a phone needs. Sending that full-size file to a small screen wastes the visitor's data and slows the page down. HTML lets you offer the browser a few versions of the same picture and let it pick the one that fits.
You do not need this for every image while you are starting out. A single <img> with a sensible file is fine. But it helps to know the idea exists: the same photo can come in several sizes, and the browser chooses.
<img src="beach-800.jpg" alt="Waves rolling onto a quiet beach at sunset"><img> is fine. The option is there for when a page starts feeling heavy on mobile. Video and audio
HTML can play video and sound without any plug-ins. For a clip, you use the <video> element; for sound, <audio>. Add the controls attribute and the browser gives you a play button, a timeline, and a volume control for free.
<video src="tutorial.mp4" controls></video>
<audio src="podcast-episode.mp3" controls></audio>Unlike an image, these have a closing tag, because you can put fallback text or extra settings between the opening and closing tags. The most important thing to remember early on: always include controls, or the visitor has no way to press play.
<video> plays a clip and <audio> plays sound, no plug-ins needed. Add controls or there is no play button and the visitor is stuck. Both have closing tags, because you can tuck settings and fallback text inside them, unlike an image. Sizing, lazy loading, and performance
When an image loads, it can push the rest of the page around. You are reading a paragraph, a photo finishes loading above it, and suddenly the text jumps down. That jump is annoying, and you can prevent it: tell the browser the image's size up front with width and height.
<img src="team-lunch.jpg" alt="The team sharing lunch on the office balcony"
width="800" height="600">With those numbers, the browser reserves the right amount of space before the image arrives, so nothing jumps when it does. You give the real pixel dimensions of the file, and CSS can still resize it on screen.
width and height and the page stops jumping around as photos load in. Use the real pixel size of the file; CSS can still shrink it on screen. This one habit fixes the most annoying loading glitch there is. 
