Responsive dynamic background images in WordPress
Tags: development, wordpress, backgroundimage, webdev
Published: 2021-10-08 13:36:24

---
title: Responsive dynamic background images in WordPress
published: true
date: 2020-10-19 12:46:32 UTC
tags: Development,WordPress,backgroundimage,WebDevelopment
canonical_url: https://carlosguzman.dev/responsive-dynamic-background-images-in-wordpress/
---

The most common ways to show images in a website are using the HTML tags `<img>` and `<picture>` or using the CSS property `background-image`. There are [attributes in the HTML tags to show different images](https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images) based on the browser viewport. Or you can use [media queries to show different images](https://cloudfour.com/thinks/responsive-images-101-part-8-css-images/) with background-image. You could find the article _[The CSS background-image property as an anti-pattern](https://nystudio107.com/blog/the-css-background-image-property-as-an-anti-pattern)_ very interesting. If you are using background-image anyway, this article describe how to use it to have a responsive dynamic background images in WordPress.

Nowadays one trend is to use eye-catching images at the top of articles and news in websites to draw the attention of visitors. Downloading images in a website is one of the main factors in the speed loading. Large images increase the quantity of data to download. Most of the traffic of websites is done through mobile devices. Optimize the loading of these images is important.

WordPress has the field [featured image](https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/) in to link a representative image with an article. Some websites use this image in the header of the article.<mark id="annotation-text-c96964d3-be1c-4f5f-91a1-384682d24687"></mark> The next solution also works if the image is not in the featured image field but in a custom field of the article.

## 1. Define the CSS breakpoints

CSS breakpoints define the ranges of device screen width where the content responds in a certain way. In example, the framework [Twitter Bootstrap defines 4 breakpoints and 5 ranges](https://getbootstrap.com/docs/4.5/layout/overview/#responsive-breakpoints): extra small, small, medium, large and extra large.

- Extra small screens: <576px
- Small screens: ≥576px
- Medium screens: ≥768px
- Large screens: ≥992px
- Extra large screens: ≥1200px

## 2. Register image sizes for each screen width range

Each screen width range defined by the CSS breakpoints requires to define the dimensions to show the image in the range. Let’s continue with the example of Twitter Bootstrap. We are using its [containers](https://getbootstrap.com/docs/4.5/layout/overview/#containers) and we are displaying the image using the full width of the container, then we could define the next dimensions(WxH):

- 576×350 for containers in extra small screens
- 540×350 for containers in small screens
- 720×350 for containers in medium screens
- 960×400 for containers in large screens
- 1140×400 for containers in extra large screens

The WordPress function [add\_image\_size](https://developer.wordpress.org/reference/functions/add_image_size/) registers images dimensions. WordPress creates a cropped version of the image for each registered dimension when an image is uploaded.

```
// register image sizes
add_image_size('header-image-xs', 576, 350);
add_image_size('header-image-sm', 540, 350);
add_image_size('header-image-md', 720, 350);
add_image_size('header-image-lg', 960, 400);
add_image_size('header-image-xl', 1140, 400);
```

## 3. Include the image in the theme template

A WordPress theme has different templates for different kind of pages. In this example, we are displaying the images for the articles. Thus we edit the _single-post.php_ template to include the image.

```
// ... content before the background image

<div class="site-content">
  <div class="header-image-container">
    // ... some content over the background image
  </div>
</div>

// ... content after the background image
```

## 4. Include the media queries

When we show the image using background-image, then we need to use CSS media queries to display the images in a responsive way. We can use the WordPress hook [wp\_head](https://developer.wordpress.org/reference/hooks/wp_head/) to include the CSS code in the head of the HTML document. In the function attached to the hook, then we get the featured image of the article and build the media queries. At this point, we have defined the [HTML structure](http://include-image-in-theme) and we know the CSS selector required to point to the element where the background-image is used.

```
// print the background CSS rules
add_action('wp_head', function(){
  // link breakpoints and image sizes
  $images = [
    '0' => 'header-image-xs',
    '576' => 'header-image-sm',
    '768' => 'header-image-md',
    '992' => 'header-image-lg',
    '1200' => 'header-image-xl',
  ];
  
  // check if the post/page has a featured image
  if (has_post_thumbnail()) {
    // get the thumbnail images
    array_walk($images, function(&$item) {
        $item = get_the_post_thumbnail_url(get_the_ID(), $item);
    });
    ?>
    <style>
    <?php foreach($args['images'] as $breakpoint => $image_url): ?>
      <?php if ('0' == $breakpoint): ?>
      .site-content .header-image-container {
        background-image: url("<?= $image_url ?>");
      }
      <?php else; ?>
      @media (min-width: <?= $breakpoint ?>px) {
        .site-content .header-image-container {
          background-image: url("<?= $image_url ?>");
        }
      }
      <?php endif; ?>
    <?php endforeach; ?>
    </style>
    <?php
  }
});
```

And that’s all. You can find [here](https://wpexperiments.carlosguzman.dev/responsive-background-images/) a demo of this approach:

[![Responsive dynamic background images in WordPress](https://carlosguzman.dev/wp-content/uploads/2020/10/responsive-bg-images-demo-1024x1002.jpg)](https://wpexperiments.carlosguzman.dev/responsive-background-images/)<figcaption>Demo of responsive dynamic background images in WordPress</figcaption>

You might found useful the library [wp-responsive-background-image](https://github.com/c24o/wp-responsive-background-image). I created this library which offer an interface with a couple of methods to allow add the background rules easily. It uses the approach mentioned before to insert responsive dynamic background images in WordPress.

The post [Responsive dynamic background images in WordPress](https://carlosguzman.dev/responsive-dynamic-background-images-in-wordpress/) appeared first on [Carlos Guzman](https://carlosguzman.dev).