HTML Basics

HTML Introduction HTML Basic HTML Comments HTML Tags/Elements HTML Attributes HTML Formatting HTML Block and Inline HTML Charsets HTML Classes HTML Colors HTML Div HTML Headings HTML Id HTML Iframes HTML Images HTML File Paths HTML Tables HTML Layout HTML Lists HTML Links <a> HTML Paragraphs HTML Quotations HTML JavaScript HTML Emojis HTML URL Encode HTML Entities HTML Computercode HTML Symbols HTML Styles

HTML Forms

HTML Forms HTML Form Elements HTML Form Attributes HTML Input Attributes HTML Input Regex HTML Input Form Attributes HTML Input Types

HTML SEO

HTML Head HTML Page Title HTML Responsive HTML Semantics HTML Favicon

HTML Graphics

HTML Canvas HTML SVG

HTML Media

HTML Media HTML Audio HTML Video

HTML Reference

a abbr acronym address applet area article aside audio b base basefont bdi bdo big blockquote body br button canvas caption center cite code col colgroup data datalist dd del details dfn dialog dir div dl DOCTYPE dt em embed fieldset figcaption figure font footer form frame frameset h1_-_h6 head header hgroup hr html i iframe img input ins kbd label legend li link main map mark menu meta meter nav noframes noscript object ol optgroup option output p param picture pre progress q rp rt ruby s samp script search section select small source span strike strong style sub summary sup svg table tbody td template textarea tfoot th thead time title tr track tt u ul var video wbr

HTML <picture> Tag

The HTML <picture> tag is used to specify multiple sources for an image element. It allows the browser to choose the most appropriate image source depending on factors like screen resolution or viewport size.

How to Use the <picture> Tag

To use the <picture> tag, you need to specify one or more <source> elements inside the <picture> tag. Each <source> element should include the srcset attribute with the URL of the image source and the media attribute to define the conditions under which that source should be used. Finally, add an <img> element as the last child of the <picture> tag to provide a default image source in case none of the conditions match.

Main Attributes of the <picture> Tag

Attribute Description
srcset Specifies the URL of the image source to be used.
media Specifies the conditions under which the image source should be used.

Example of Using the <picture> Tag

<picture>
  <source srcset="image-large.jpg" media="(min-width: 1024px)">
  <source srcset="image-medium.jpg" media="(min-width: 768px)">
  <img src="image-small.jpg" alt="Fictional Image">
</picture>

In this example, the browser will choose the appropriate image source based on the screen width. If the viewport width is at least 1024px, it will use "image-large.jpg". If the viewport width is at least 768px but less than 1024px, it will use "image-medium.jpg". If the viewport width is less than 768px, it will use "image-small.jpg".