Blog image styles
Before creating image styles we need to fully understand the need for our images. This will allow us to create image styles that can be reused rather than creating one-offs.
For the homepage blog posts we are going to create two types of image styles:
16:9 aspect ratio image styles for the posts in the From our blog section
2:3 aspect ratio image styles for the posts in the Featured content section
In addition, we will create 2x image styles for each to cover high resolution screens.
Creating blog posts image styles
Create the following image styles:
16:9 Medium 460x258
460x258
Scale and crop
16:9 Large 720x405
720x405
Scale and crop
2:3 Small 200x260
200x260
Scale and crop
2:3 Medium 400x520
400x520
Scale and crop
Creating blog posts responsive image styles
Now let's create responsive image styles where we can combine each of the similar image styles into separate bundles.
Blog teaser
16:9 Small 460x258 and 16:9 Small 720x405
Blog featured
2:3 Small 200x260 and 2:3 Medium 400x520
It can be confusing, but Responsive Image Styles are not the same as Image Styles. I think of responsive image styles as bundles that can contain one or multiple image styles.
From Drupal's admin toolbar click Configuration | Media | Responsive Images Styles (Responsive Images core module must be enabled for this option)
Click the Add responsive image style button
Type Blog teaser for label
For Responsive group select Responsive Image
Select one of the smaller images styles we created as Fallback image style and press the Save button
Expand 1x Viewport Sizing [ ]
Under Type check Select multiple image styles and use the sizes attribute
In the Sizes box, type 25vw (this tells Drupal to render the image at about 25% the width or viewport width)
Under Image Styles check all the image shown above for Blog teaser
Click the Save button
Repeat steps 3 throughout 10 with Blog featured.
Media view modes
Since we are using media reference as the field for the blog images, we are going to create two view modes for the media type Image. These view modes are Blog teaser and Blog featured.
From Drupal's admin toolbar click Structure | Media types | Image > Manage display
Expand the CUSTOM DISPLAY SETTINGS fieldset
Click Manage view modes
Scroll down to Media and click Add new Media view mode
Name it Blog teaser and click the Save button. If you scroll down again you will see Blog teaser as one of the view modes for Media
Repeat this step with a new view mode called Blog featured
Repeat steps 1 & 2 above to return to the image configuration page
Expand the CUSTOM DISPLAY SETTINGS fieldset again and you should see the Blog teaser and Blog featured view modes
Select Blog teaser and Blog featured and press the Save button to enable the new view modes
Click Blog teaser from the list of view modes
Ensure Image is the only field outside of the Disabled group
Change the FORMAT from Image to Responsive Image
Click the cogwheel icon on the right of the page, next to Select a responsive image style
Under Responsive Image Style select Blog teaser
Click the Update button then the Save button
Repeat steps 10 through 15 with the Blog featured view mode
By creating a view mode for the Image Media Entity we can define configurations that will only affect the Blog images and not other images on our site. However, there is one more step to do before we can see the changes reflected in the hero image.
Configure blogs image field with image styles
Now that we have image styles, responsive image styles, and have set up view modes for each type of blog images, it's time to do the final step which is configure the image field in the Blog content type to use the newly configured settings.
From Drupal's admin toolbar click Structure | Content types | Blog | Manage display
Click the Teaser view mode
Identify the Image field in the table and click the cogwheel icon located all the way to the right of the image field
Under View mode select Blog teaser
Click the Update button then the Save button
Repeat these steps with the Featured view mode and use the Blog featured view mode for the image.
In some cases you may need to clear your Drupal caches. Visit the homepage and your blog posts images should now look nice and consistent.
Inspect any of the images by right-clicking on the hero image and select Inspect or Inspect element depending on your browser
You will notice that the code for the image has now changed to include all the image styles we created as well as their dimensions and the sizes information. With this we are giving the browser a collection of images to choose from, how big we want them to be rendered. Now the browser can pick the best image possible for each breakpoint.
Last updated