html · level 3 · unit 2 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
<picture> swaps in a different picture for each screen.
srcset offers sizes of one picture. Sometimes you want a different picture: a wide shop banner on a laptop, but a tight of the logo on a phone, where the banner would be tiny. Choosing pictures like this is called .
Wrap an <img> in <picture>. Before it, add a <source> for each special case, with a media condition in media and the file in srcset. The browser uses the first <source> that matches.
The <img> goes last and is required. It is the fallback, and it carries the alt, width and height for whichever picture is chosen.
<h1>Pip's Plants</h1>
<picture>
<source media="(min-width: 800px)" srcset="tree.svg">
<img src="logo.svg" alt="The Pip's Plants shop" width="400" height="400">
</picture> Press show my page to see it here.
<source> is an empty element, like <img>, so it never has a closing tag.
guess first
Which picture shows on a phone?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<picture>
<source media="(min-width: 900px)" srcset="cake.svg">
<img src="logo.svg" alt="Bake Street Bakery" width="300" height="300">
</picture>
your turn
Your turn. Art-direct the hero.
On screens at least 1000 pixels wide, show the wide tree.svg. Fill the media and srcset blanks on line 3. Phones keep the logo.
Press show my page to see it here.
- the source is for wide screensnot checked yet
- wide screens get the tree picturenot checked yet
- phones still get the logonot checked yet
fix it
The picture has vanished. Can you fix it?
Press show my page to see it here.
- the picture ends with an
<img>not checked yet - wide screens still get the treenot checked yet
- A
<picture>shows nothing on its own. Which element does the showing? - Every
<picture>must end with an<img>. It is the fallback, and it holds thealttext. - Change line 4 to
<img src="logo.svg" alt="The Pip's Plants shop" width="400" height="400">.
nailed it
the right picture for every screen. :D
<picture>chooses between different pictures.- Each
<source>has amediacondition and asrcset. - The
<img>comes last and carries thealttext.
new words in your collection
- art direction
- crop
This lesson isn’t saved. A free account remembers every lesson you finish.