HTML
Responsive Images and Video
Embed responsive images with srcset, video, audio, and figure in HTML.
By EZ4Code Team
mediaimageintermediate
Code
<!-- Responsive image with srcset -->
<img
src="default.jpg"
srcset="small.jpg 480w, medium.jpg 800w, large.jpg 1200w"
sizes="(max-width: 600px) 480px, 800px"
alt="A scenic landscape"
loading="lazy"
/>
<!-- Video with multiple sources -->
<video controls width="640" poster="cover.jpg">
<source src="movie.mp4" type="video/mp4" />
<source src="movie.webm" type="video/webm" />
Your browser does not support video.
</video>
<!-- Audio -->
<audio controls>
<source src="song.mp3" type="audio/mpeg" />
</audio>
<!-- Figure with caption -->
<figure>
<img src="chart.png" alt="Sales chart" />
<figcaption>Q4 sales growth</figcaption>
</figure>Explanation
Uses srcset and sizes to serve appropriately sized images based on viewport width, improving load performance. The video and audio elements support multiple source formats for cross-browser compatibility. figure with figcaption associates media with descriptive captions.
More HTML Snippets
Accessible Form with Inputs
Build an accessible HTML form with labels, inputs, select, and checkbox.
Table with Thead Tbody Tfoot
Structure tabular data with thead, tbody, tfoot, and caption in HTML.
Links Anchors and Download
Create internal, external, anchor, email, phone, and download links in HTML.
Semantic Tags
HTML5 semantic structure.
Form Validation
HTML5 form validation.
SVG
Scalable Vector Graphics.