Dark Mode Toggle

Using Images in Flexbox

The images on this website are simple placeholders used to demonstrate their manipulative capabilities in flexbox. Please excuse the lack of aesthetic effort!

The Rules

Flexbox allows the use of the float property for positioning images inside its child containers. You cannot use it in parent containers!

If you want to add an image to a page in flexbox and make it responsive, follow the same rules you would in conventional CSS:

Rule #1
Do not add the actual width and height measurements to your image code.

<img src="image.png" width="432px" height="500px" alt="alternate text">

Instead use:

<img src="image.png" width="432" height="500" alt="alternate text">

Rule #2

Include the line:
img {max-width:100%}  /*Open Space */
or
img {width:100%} /*Constrained by Container */

The CSS for the image on this page:

div.flex-container picture {
flex: 1;
flex-direction : column;
margin: 0; padding: 0;
}

div.flex-container picture img { 
max-width: 100%; 
height: auto; 
display: block; 
margin: 0 auto}
responsive image

Optimize Your Images

The best way to optimize your images these days is to convert them to one of the newer file formats. The two most popular are avif and WebP.

Swapping Images

with the Picture Element

The bare code without text using the picture element for column 2 on the front page is shown here:

<picture>
  <source media="(max-width: 645px)" srcset="images/pugs2-300.webp">
  <source media="(max-width: 800px)" srcset="images/pugs2.webp">
  <source media="(max-width: 1200px)" srcset="images/pugs2-231.webp">
  <source media="(max-width: 1900px)" srcset="images/pugs2-300.webp">
  <img src="images/pugs2.webp" alt="responsive image">
  </picture>

Old vs New

Some of the older browsers might not support the new file formats, so it's a good idea to include a fallback version of the image that displays on error.

There are a lot of fallback codes posted on the internet. I tried several before finding one that actually worked.

Here's the one we use in this mini site:

<img src="theimage.avif" alt="Alternate Text" onerror="this.src='theimage.png'">

Adding Attributes

The code for column 1 on the front page is shown here:

<div class="flex-container">
<picture>
<source media="(max-width: 645px)" srcset="images/pugs1-300.webp" width="300" height="347" onerror="this.src='images/pugs1-300.jpg'">
<source media="(max-width: 800px)"  srcset="images/pugs1.webp" width="432" height="500" alt="responsive image" onerror="this.src='images/pugs1-432.jpg'">
  <source media="(max-width: 1200px)" srcset="images/pugs1-231.webp"  width="231" height="267" onerror="this.src='images/pugs1-231.jpg'">
  <source media="(max-width: 1900px)" srcset="images/pugs1-300.webp" width="300" height="347" onerror="this.src='images/pugs1-300.jpg'">
  <img src="images/pugs1.webp" width="432" height="500" alt="responsive image" onerror="this.src='images/pugs1-432.jpg'">

<h2>Less is More</h2>
<p>I've tried to construct these templates with.....</p>
</picture>

<picture>.......

Notice in this code that the picture element is used as a column inside the flex-container instead of the usual division tag.

The fallback image code is provide as are width, height and aspect-ratio.

A different size image is provided at various break points. media="(max-width: 645px)"

This is similar to the media query but is provided as inline code.

Image Heavy?

For pages that display a lot of images, add the attribute loading="lazy" so they don't all load at once.

<img src="theimage.avif" alt="Alternate Text" loading="lazy" onerror="this.src='theimage.png'">

Another option to consider is preloading (Head Section of Page):

<link rel="preload" as="image" href="images/pugs1.webp">

Note: If you've set up your Localhost and want to drive yourself NUTS this can all be tested on Lighthouse (Dev Tools).

Add Message On Hover

To display a message when the mouse cursor hovers the image, use the title attribute.

<img class="yourclass" src="theimage.avif" alt="Alternate Text" loading="lazy" title="Your Message"  onerror="this.src='theimage.png'">

NOTE: All attributes will transfer to the fallback image onerror.