Gatsby Image Props

December 15, 2019

Gatsby-image props

After you’ve made a query, you can pass additional options to the gatsby-image component.

NameTypeDescription
fixedobjectData returned from the fixed query
fluidobjectData returned from the fluid query
fadeInboolDefaults to fading in the image on load
durationFadeInnumberfade-in duration is set up to 500ms by default
titlestringPassed to the rendered HTML img element
altstringPassed to the rendered HTML img element. Defaults to an empty string, e.g. alt=""
crossOriginstringPassed to the rendered HTML img element
classNamestring / objectPassed to the wrapper element. Object is needed to support Glamor’s css prop
styleobjectSpread into the default styles of the wrapper element
imgStyleobjectSpread into the default styles of the actual img element
placeholderStyleobjectSpread into the default styles of the placeholder img element
placeholderClassNamestringA class that is passed to the placeholder img element
backgroundColorstring / boolSet a colored background placeholder. If true, uses “lightgray” for the color. You can also pass in any valid color string.
onLoadfuncA callback that is called when the full-size image has loaded.
onStartLoadfuncA callback that is called when the full-size image starts loading, it gets the parameter { wasCached: } provided.
onErrorfuncA callback that is called when the image fails to load.
TagstringWhich HTML tag to use for wrapping elements. Defaults to div.
objectFitstringPassed to the object-fit-images polyfill when importing from gatsby-image/withIEPolyfill. Defaults to cover.
objectPositionstringPassed to the object-fit-images polyfill when importing from gatsby-image/withIEPolyfill. Defaults to 50% 50%.
loadingstringSet the browser’s native lazy loading attribute. One of lazy, eager or auto. Defaults to lazy.
criticalboolOpt-out of lazy-loading behavior. Defaults to false. Deprecated, use loading instead.

Here are some usage examples:

<Img
  fluid={data.file.childImageSharp.fluid}
  alt="Cat taking up an entire chair"
  fadeIn="false"
  className="customImg"
  placeholderStyle={{ `backgroundColor`: `black` }}
  onLoad={() => {
    // do loading stuff
  }}
  onStartLoad={({ wasCached }) => {
    // do stuff on start of loading
    // optionally with the wasCached boolean parameter
  }}
  onError={(error) => {
    // do error stuff
  }}
  Tag="custom-image"
  loading="eager"
/>