max-width: 100% image will not expand beyond its actual size margin: 2% auto centers the image. display: block image starts on new line and displays as a box element
Example Pixel Widths
Device
Viewing Width
Apple iPhone 11
414
Apple iPhone 11 Pro
375
Apple iPhone 12
390
Apple iPhone 12 mini
360
Apple iPhone 14 Plus
428
Apple iPhone 14 Pro
393
Apple iPhone 15 Plus
430
Swapping to Make Images Responsive
Another way to use images on responsive web design pages is to swap them out at various screen widths or break points using Media Queries in your style sheet.
You would create different size widths of the same image. Let's say 3 different sizes.(300px, 200px and 150px)
The following code in your style sheet would define how you want the images set in the page. The line following the block of code would hide images 2 and 3 and allow the first one to show on the page.
Then you would create break points using Media Queries in the style sheet where you want to make a swap to a different size image.
@media all and (max-width: 768px) {
img.image2 {display :block}
img.image1 , img.image3 {display:none}
}
@media all and (max-width: 360px) {
img.image3 {display :block}
img.image1 , img.image2 {display:none}
}
Pretty simple. FOR YOU, YOU SAY!! Seriously. Once you get into building Media Queries in your style sheets, you'll be amazed at some of the crazy things you can do with CSS3.
You can place figcaption above or below the image. Adjust style settings as needed.
The New AVIF Image Format
I wouldn't have known about this if I wasn't a member of Medium.com but there is a new image format that allows you to create or convert images to about a 10th of their original size without any loss in quality or performance.
Think of the difference in loading speed.
It's the .avif format.
Linux Users can find a little program in the repository called XnConvert for converting your bulky screenshots to avif which is fairly easy to use. Before you install it Install gThumb so you can see the images after you convert them.
Windows Users: There are countless places to download a copy for your version of Windows. It is Freeware but you should purchase a license for $15 if you use it for business.
Just to be safe, should your page encounter a browser that doesn't support .avif, You can use a fallback version as shown here.
The Code
Fallback Code that actually works:
<img class="yourclass" src="image.avif" alt="Image 1" title="Text When hovered" onerror="this.src='image.png'">
The fallback will retain class , alt and title settings. Quote marks must be exact.
XnConvert is very easy to use. Drag or copy the image into the Input window. Set the Output window to AVIF and click Convert (Bottom of Window). You can do individual files or entire folders.
Optimizing Images
Optimizing is the process of reducing an image to the least number of bits per pixel and colors without sacrificing the quality of the displayed image. Large unoptimized images are a major cause of irritation for web surfers. Many good graphic creation programs include features for optimization.
Always check your images when HTML pages are uploaded to your web space. Many images will display quite differently over your web hosts server, than they do when viewed on your PC.
*Build Responsive Designed web pages and you won't need to worry about image sizes.