Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
- In the last video, we just looked
at how we can use the image element and the srcset attribute
to list a set of image files and have the browser decide
which file to download and display
considering the pixel density of the screen.
What if instead we want the browser
to consider the width of the viewport
and to choose a file based on the size of the screen
as well as the density?
Let's look at a demo.
It's very similar to the one I just showed in the last video
with four images listed inside of a srcset attribute.
But this time instead of saying one x, two x,
three x, four x,
we'll tell the browser how wide each file is,
480w, 960w.
That means 480 pixels wide, 960 pixels wide.
Now the browser will dynamically choose
which image to display based on a combination
of device density and viewport width.
This is not going to pick the right size image
for the layout that you have.
It's choosing the image based on an assumption
that you need the image to be 100% of the viewport width.
So why?
Well, this system is designed to be fast
and for the browser to make a decision
about which image to download
as early as possible in the page loading process.
It chooses the image file before the CSS has been parsed
and before layout has happened.
So the browser has no idea
what you're going to do with that image.
It doesn't know what the size of the box is going to be
where the image is going.
If we know the image is going to be a lot smaller
than the width of the whole page,
we probably want to add more information here
for the browser to use when it's making its choice
about which file to grab.
Here, I've used CSS to make this image half the width it was
in the last demo,
but the browser is still loading the bigger files.
We don't need so much data,
so let's tell the browser when to use the file.
We'll use the sizes attribute for this,
listing which size image to use at which media query.
You can see now that the second photo looks just as good,
but it's using the smaller files.
It's using the files that are the right size
for the layout that we have.
HTML has a lot of power when it comes to providing options
to the browser for downloading different size files
of the same photo.
Srcset gives you two options
for communicating to the browser
that there are a set of images available to use.
You can specify which images are
for which resolution of screen,
or you can indicate which image has how many pixels
and let the browser choose an image based
on the combination of screen density and viewport width.
The sizes attribute gives you a way to specify
for the browser how much of the viewport's width
that image will take up at which break point.
In every case, you're giving the browser information
in the HTML so it can decide intelligently
which image file to download,
especially considering the network conditions
and the user's preferences.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.