• Hi everyone. I apologize if this is in the wrong spot- I really didn’t know where to put it.

    I know an earlier version of WordPress introduced responsive images in terms of sizes and srcset, so that the browser can make intelligent decisions on what image to load. I am wondering if this still works for background images. I am not able to see a natural image size in the console for background images.

    So does this only work for <img> tags or for any ol’ <div> with a background image, too?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    It’s only for image tags.

    Thread Starter jordanwebdev

    (@jordanwebdev)

    Oh, that’s unfortunate. Are there any workarounds? Sometimes, it’s much easier to style an image if it’s a background. You have nice options like background-size: cover and contain.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    The “Responsive images” term isn’t really about responsive images, it’s about bandwidth. I don’t think that has anything to do with what you want to achieve.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    You’re comparing apples and oranges; HTML5 introduced responsive images on the ‘<img>’ element; whereas you want to use CSS to swap out images. You can surely do this yourself using CSS, The W3C do not have to develop special functionality for this.

    Simple example:

    .foo {
        background: url('small-foo.png');
    }
    
    @media screen and (min-width: 400px) {
        .foo {
            background: url('medium-foo.png');
        }
    }
    
    @media screen and (min-width: 800px) {
        .foo {
            background: url('large-foo.png');
        }
    }

    But WordPress, as a Content Management System, shouldn’t have to incorporate CSS responsive background images – This is the choice of the plugin or theme developer.

    Thread Starter jordanwebdev

    (@jordanwebdev)

    Thanks for that suggestion. Sorry to bother, but I’m having an issue with that method.

    Here’s the div I’m using to hold my background image:

    <div class='img-wrap' style='background: url(<?php echo the_post_thumbnail_url( 'medium' ); ?>) 50% 0% no-repeat; background-size: cover'></div>

    I would expect this to grab the 300×300 image, but it goes to the default full sized one instead. I checked the uploads folder, and there has indeed been a 300×300 version generated. Any clue why it’s not working?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Responsive background image sizes?’ is closed to new replies.