App
Selected: Typed (2sxc 16+) Switch to Dynamic (Razor14 or below)
Best-Possible Image for Every Screen & Device
...using img
and srcset
HTML has a few ways to show an image, and you should know which way is best. Discover:
img
for your standard image- enhancing images with size-detection using
srcset
andsizes
With srcset
on the img
tag you can tell the browser to load different images based on screen size. To really get the hang of this, best check out the MDN docs.
Responsive img
with srcset
and sizes
To Load Different Pictures
What you need to know about srcset
- Important: If the browser already has a large image, it won't load smaller ones. So to see this in action, you have to start with a tiny browser window, loading the 250px - then make it larger to see bigger pictures load.
- The main picture in
src
is used in all old browsers - You can also use
srcset
to specify different resolutions for retina displays. - Important: the width specified in the
max-width
is the view/page width, not the image width
These examples are optimized. The browser will only load new images, if they are larger than the previous one. So to see the effect, you must make the browser very small (narrow), reload the page, and then drag it wider.
Some examples show the currently loaded URL below it, and the difference may be very small as it changes. So look closely 🔍
Base Class with Kit.Image
This tutorial inherits from the Custom.Hybrid.Razor14
or the Custom.Hybrid.RazorTyped
base class.
This allows us to use Kit.Image
to access an IScrub
without having to use GetService<IImageService>
.
This is a "manual" sample, where we listed each size as individual files - just for understanding the concept.
image src should appear here
image src should appear here
<img src='/razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=1230&h=760&quality=75&mode=crop&scale=both' loading='lazy' class='img-fluid' width='1230' height='760' srcset='/razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=2460&h=1520&quality=75&mode=crop&scale=both 2460w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=1230&h=760&quality=75&mode=crop&scale=both 1230w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=922&h=569&quality=75&mode=crop&scale=both 922w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=615&h=380&quality=75&mode=crop&scale=both 615w' sizes='(max-width: 1400px) 100vw, 1230px'>
The following example (and the ones after this) will use a flatter image to make it easier to see all the samples. To achieve this, we must create ResizeSettings
for this. It will be based on the default Content
settings.
image src should appear here
<img src='/razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=1230&h=153&quality=75&mode=crop&scale=both' loading='lazy' class='img-fluid' width='1230' height='153' srcset='/razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=2460&h=307&quality=75&mode=crop&scale=both 2460w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=1230&h=153&quality=75&mode=crop&scale=both 1230w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=922&h=115&quality=75&mode=crop&scale=both 922w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=615&h=76&quality=75&mode=crop&scale=both 615w' sizes='(max-width: 1400px) 100vw, 1230px'>
The default settings contains a list of default resizes - which is why the examples before just worked. You can also specify your own, according to the specs in the srcset docs.
image src should appear here
<img src='/razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=1400&h=175&quality=75&mode=crop&scale=both' srcset='/razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=1600&h=200&quality=75&mode=crop&scale=both 1600w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=1200&h=150&quality=75&mode=crop&scale=both 1200w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=1000&h=125&quality=75&mode=crop&scale=both 1000w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=900&h=112&quality=75&mode=crop&scale=both 900w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=800&h=100&quality=75&mode=crop&scale=both 800w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=700&h=87&quality=75&mode=crop&scale=both 700w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=600&h=75&quality=75&mode=crop&scale=both 600w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=500&h=62&quality=75&mode=crop&scale=both 500w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=400&h=50&quality=75&mode=crop&scale=both 400w'>
image src should appear here
<img src='/razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=1230&h=153&quality=75&mode=crop&scale=both' loading='lazy' alt='SEO Text' class='border border-primary img-fluid' width='1230' height='153' srcset='/razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=2460&h=307&quality=75&mode=crop&scale=both 2460w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=1230&h=153&quality=75&mode=crop&scale=both 1230w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=922&h=115&quality=75&mode=crop&scale=both 922w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=615&h=76&quality=75&mode=crop&scale=both 615w' sizes='(max-width: 1400px) 100vw, 1230px'>
In this example we won't just output the default, but only the img-tag with special ID and styles.
image src should appear here
<img src='/razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=1230&h=153&quality=75&mode=crop&scale=both' id='Some-Id' loading='lazy' class='img-fluid' style='width: 75%' width='1230' height='153' srcset='/razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=2460&h=307&quality=75&mode=crop&scale=both 2460w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=1230&h=153&quality=75&mode=crop&scale=both 1230w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=922&h=115&quality=75&mode=crop&scale=both 922w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=615&h=76&quality=75&mode=crop&scale=both 615w' sizes='(max-width: 1400px) 100vw, 1230px'>
image src should appear here
<img src='/razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=1230&h=153&quality=75&mode=crop&scale=both' srcset='/razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=2460&h=307&quality=75&mode=crop&scale=both 2460w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=1230&h=153&quality=75&mode=crop&scale=both 1230w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=922&h=115&quality=75&mode=crop&scale=both 922w, /razortutorial12/app/Tutorial-Razor/assets/img/assets/pexels-pixabay-533288.jpg?w=615&h=76&quality=75&mode=crop&scale=both 615w' title='Hover to see this'>
Credits:
- Koi fish Photo by agus prianto on Unsplash
- Jellyfish Photo by Karan Karnik on Unsplash