This will set your background image to 100% width and allow the height to overflow. Now you can use media queries to swap out that image instead of relying on JavaScript.

    EDIT: I just realized (3 months later) that you probably don’t want the image to overflow; you seem to want the container element to resize based on it’s background-image (to preserve it’s aspect ratio), which is not possible with CSS as far as I know.

    Hopefully soon you’ll be able to use the new srcset attribute on the img element. If you want to use img elements now, the currently accepted answer is probably best.

    However, you can create a responsive background-image element with a constant aspect ratio using purely CSS. To do this, you set the height to 0 and set the padding-bottom to a percentage of the element’s own width, like so:

    This will set your background image to 100% width and allow the height to overflow. Now you can use media queries to swap out that image instead of relying on JavaScript.

    EDIT: I just realized (3 months later) that you probably don’t want the image to overflow; you seem to want the container element to resize based on it’s background-image (to preserve it’s aspect ratio), which is not possible with CSS as far as I know.

    Hopefully soon you’ll be able to use the new srcset attribute on the img element. If you want to use img elements now, the currently accepted answer is probably best.

    However, you can create a responsive background-image element with a constant aspect ratio using purely CSS. To do this, you set the height to 0 and set the padding-bottom to a percentage of the element’s own width, like so:

    However, you can create a responsive background-image element with a constant aspect ratio using purely CSS. To do this, you set the height to 0 and set the padding-bottom to a percentage of the element’s own width, like so:
    However, you can create a responsive background-image element with a constant aspect ratio using purely CSS. To do this, you set the height to 0 and set the padding-bottom to a percentage of the element’s own width, like so:
    However, you can create a responsive background-image element with a constant aspect ratio using purely CSS. To do this, you set the height to 0 and set the padding-bottom to a percentage of the element’s own width, like so:
    However, you can create a responsive background-image element with a constant aspect ratio using purely CSS. To do this, you set the height to 0 and set the padding-bottom to a percentage of the element’s own width, like so:
    However, you can create a responsive background-image element with a constant aspect ratio using purely CSS. To do this, you set the height to 0 and set the padding-bottom to a percentage of the element’s own width, like so:
    However, you can create a responsive background-image element with a constant aspect ratio using purely CSS. To do this, you set the height to 0 and set the padding-bottom to a percentage of the element’s own width, like so: