Skip to content

Instantly share code, notes, and snippets.

@ajmalafif
Last active November 27, 2020 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajmalafif/08d2915778d519650d6592db9e0f0186 to your computer and use it in GitHub Desktop.
Save ajmalafif/08d2915778d519650d6592db9e0f0186 to your computer and use it in GitHub Desktop.
[sanity + gatsby] Figuring out jank-free, lqip image loading
@ajmalafif
Copy link
Author

You can get those thumbnails to scale up and keep your content from jumping around by putting the image in a container sized how you want, then setting the CSS on the image to display: block; width: 100%. Unless you are cropping the image, the LQIP should be the same aspect ratio. If you are cropping the image, you can instead set the absolute dimensions on the img element and use object-fit: cover on it.

@ajmalafif
Copy link
Author

ajmalafif commented Nov 27, 2020

Figured it out

<figure
    style={{
        position: 'relative',
        overflow: 'hidden',
        margin: 0,
        width: '50%'
      }}>
      <div
      style={{
        paddingBottom: '56.25%'
      }}></div>
      <SanityImage
        {...props.mainImage}
        sx={{
          '&[data-lqip]': {
            display: 'block',
          position: 'absolute',
          top: 0,
          left: 0,
          width: '100%',
          }
        }}
        style={{ 
          display: 'block',
          position: 'absolute',
          top: 0,
          left: 0,
          width: '100%',
          height: '100%'
        }}
      />
</figure>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment