Skip to content

[gatsby-image] [gatsby-transformer-sharp] Emulate <img srcSet> behavior (restrict 2x images to 1x width) #9804

@dustinhorton

Description

@dustinhorton

Summary

Relevant information

Prior to using gatsby-image, most of my high-resolution image work in markup was via the img srcSet atrribute. Check out this Codepen for a quick demo. Basically, given the following markup:

<img
  src="//placehold.it/250x250"
  srcSet="//placehold.it/500x500 2x"
>

The result will be:

  • On a 'normal' resolution display, the src image at it's native width & height (250px in my example)
  • On a 'high' resolution display, the srcSet image at half native width & height (so still 250px in my example)

I'm basically looking to achieve that same behavior with gatsby-image: generate a 2x image with gatsby-transformer-sharp, but don't display it at that 2x size—rather restrict it's size so it's never shown at less than full resolution.

I think I'm probably over-thinking or missing something—is this not currently possible with gastby-image as-is? (edit: Maybe I'm misunderstanding the fluid/fixed split…).

If not, I imagine most are achieving this with a container markup/component, but that's not feasible because the app I'm working on—the images are coming from WordPress and a range of sizes.

I've created a repo when I've achieved this functionality, but it's going to take quite a bit of work to update my app to use the solution. Here's the relevant file—note the wrapping div and use of childImageSharp.fluid.presentationWidth: https://github.com/dustinhorton/gatsby-issue2/blob/master/src/components/image2.js

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions