Skip to content

Commit

Permalink
docs(gatsby-image): mention how to avoid stretched images in fluid (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
oorestisime authored and pieh committed Jan 10, 2019
1 parent 1cb7b46 commit 6c27086
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/gatsby-image/README.md
Expand Up @@ -260,6 +260,45 @@ prop. e.g. `<Img fluid={fluid} />`
}
```

### Avoiding stretched images using the fluid type

As mentioned previously, images using the _fluid_ type are stretched to
match the container's width. In the case where the image's width is smaller than the available viewport, the image will stretch to match the container, potentially leading to unwanted problems and worsened image quality.

To counter this edge case one could wrap the _Img_ component in order to set a better, for that case, `maxWidth`:

```jsx
const NonStretchedImage = props => {
let normalizedProps = props
if (props.fluid && props.fluid.presentationWidth) {
normalizedProps = {
...props,
style: {
...(props.style || {}),
maxWidth: props.fluid.presentationWidth,
margin: "0 auto", // Used to center the image
},
}
}

return <Img {...normalizedProps} />
}
```

**Note:** The `GatsbyImageSharpFluid` fragment does not include `presentationWidth`.
You will need to add it in your graphql query as is shown in the following snippet:

```graphql
{
childImageSharp {
fluid(maxWidth: 500, quality: 100) {
...GatsbyImageSharpFluid
presentationWidth
}
}
}
```

## `gatsby-image` props

| Name | Type | Description |
Expand Down

0 comments on commit 6c27086

Please sign in to comment.