Hero Image styles
Last updated
Last updated
Properly managing images on your website can be the difference between good and bad performance. In Drupal, Image Styles are excellent for providing parameters around how big images should be rendered on a page. Image styles are flexible enough that let you render the same image in different sizes and aspect ratios depending on your needs. In addition, there are many contrib modules that allow you to add extra effects to images through image styles. Things like on-demand cropping, overlays, image scaling and more.
TIP: Enable the Responsive Images core module and any other modules you plan on using for image styles such as crop and focal point.
Before creating an image it is important to have a good understanding on how we want the image to be rendered, not only on desktop, but also on mobile devices. This will determine the number of image styles we need to create as well as the treatment for when using mobile or desktop devices.
For the Hero, we want the image to always render full width on all devices. However, the image should not exceed 1900px on large devices. On tablet devices we want to use an image that is smaller maybe a maximum width of 1200px, and finally on mobile we want the image keep a specific height rather than simply scaling it down.
Although we can accomplish most of the requirements above with Image styles, doing so will require we use the <picture>
element to pick the different images at different breakpoints. This is not always recommended due to issues that may arise from telling the browser which image to use. It's best to let the browser select the best image possible based on factors such as device size, screen density, connection speed, browser user preferences, etc. So this means we will use some CSS tricks to get our mobile image to display at a specific height as required above.
Read more about how to serve responsive images on your Drupal 8 website.
From Drupal's admin toolbar click Configuration | Media | Image Styles
Click the Add image style button
Create the following image styles:
Image Style Name | Effect | Dimensions |
---|---|---|
The image dimensions above reflect the natural size we want images to be rendered at. However, on high resolution screens such as retina screens, those dimensions may display much smaller. It is recommended to create image styles that are 2x the dimensions of the original images. In our example above this means we will need images that are 3800x1400 and 2400x1200. The other remaining two dimensions can be achieved from reusing some of the other image styles. We will make use of all these image styles next when we create Responsive Image Styles.
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 Hero 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, ensure 100vw is available (this tells Drupal to render the image at full width or viewport width)
Under Image Styles check all the image styles we created for Hero
Click the Save button
Click Structure | Content Types | Article | Manage Display
Click the Hero view mode display
For Format select **Responsive Images ** from the dropdown
Click the little cogwheel icon at the far right of the row
For Responsive Image style select Hero
Click the Update button then click the **Save **button
Now that image styles and responsive image styles are in place, we need to configure the Hero image field to use them. Since we used a Media entity reference for the Hero image, we would do this configuration in the Image Entity by using a view mode. If we had used a plain Image field type for the Hero, we would configure the image field directly in the Hero paragraph type. However, using the media features give us more options and a better interface when working with Media assets.
From Drupal's admin toolbar click Structure | Media types | Image
Click the Manage display tab. You should see Default , Media Library, and maybe other view modes. View modes are perfect for ensuring the configuration updates you are making only affect the field of the component you are working with (i.e. Hero).
Expand the CUSTOM DISPLAY SETTINGS fieldset
Click Manage view modes
Scroll down to Media and click Add new Media view mode
Name it Hero and click the Save button. If you scroll down again you will see Hero as one of the view modes for Media
Repeat steps 1 & 2 above to return to the image configuration page
Expand the CUSTOM DISPLAY SETTINGS fieldset again and you should see the Hero view mode
Select Hero and press the Save button to enable the new view mode. Now you should see Hero next to Default, Media Library, and other existing view modes
Click Hero 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 Hero
Click the Update button then the Save button
By creating a view mode for the Image Media Entity we can define configurations that will only affect the Hero image 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.
From Drupal's admin toolbar click Structure | Content types | Article | Manage display
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 Hero
Click the Update button then the Save button
If you visit a page on your site with a hero image, you should notice the updates we just made. In some cases you may need to clear your Drupal caches.
Inspect the hero 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 (100vw). With this we are giving the browser a collection of images to choose from, how big we want them to be rendered at (100vw). Now the browser can pick the best image possible for each breakpoint.
You may have noticed that each of the fields in the Hero are showing their labels (Title, Hero, etc.). Let's remove these labels so we only see the actual content on the page.
From Drupal's admin toolbar, click Structure > Paragraph Types > Hero > Manage Display
In the Label column, click each of the field's dropdown and select Hidden for each field
Click the Save button. If you view the hero again, you may still see the label of "Hero" directly above the Hero image. This label is probably coming from the Homepage content type.
Repeat the 3 steps above using the Homepage content type.
Hero Large
Scale and Crop
1900x700
Hero Medium
Scale and Crop
1200x600
Hero Small
Scale and Crop
900x500
Hero Smallest
Scale and Crop
640x350
Hero Large 2x
Scale and Crop
3800x1400
Heo Medium 2x
Scale and Crop
2400x1400