Skip to content

Commit

Permalink
docs: add better examples to next/image docs. (#51457)
Browse files Browse the repository at this point in the history
  • Loading branch information
leerob committed Jun 19, 2023
1 parent 7d0bdab commit b0a704d
Showing 1 changed file with 105 additions and 2 deletions.
107 changes: 105 additions & 2 deletions docs/02-app/01-building-your-application/05-optimizing/01-images.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,112 @@ Styling the Image component is similar to styling a normal `<img>` element, but
- When using `fill`, the parent element must have `display: block`
- This is the default for `<div>` elements but should be specified otherwise.

For examples, see the [Image Component Demo](https://image-component.nextjs.gallery).
## Examples

### Examples
### Responsive

<Image
alt="Responsive image filling the width and height of its parent container"
srcLight="/docs/light/responsive-image.png"
srcDark="/docs/dark/responsive-image.png"
width="1600"
height="629"
/>

```jsx
import Image from 'next/image'
import mountains from '../public/mountains.jpg'

export default function Responsive() {
return (
<div style={{ display: 'flex', flexDirection: 'column' }}>
<Image
alt="Mountains"
// Importing an image will
// automatically set the width and height
src={mountains}
sizes="100vw"
// Make the image display full width
style={{
width: '100%',
height: 'auto',
}}
/>
</div>
)
}
```

### Fill Container

<Image
alt="Grid of images filling parent container width"
srcLight="/docs/light/fill-container.png"
srcDark="/docs/dark/fill-container.png"
width="1600"
height="529"
/>

```jsx
import Image from 'next/image'
import mountains from '../public/mountains.jpg'

export default function Fill() {
return (
<div
style={{
display: 'grid',
gridGap: '8px',
gridTemplateColumns: 'repeat(auto-fit, minmax(400px, auto))',
}}
>
<div style={{ position: 'relative', height: '400px' }}>
<Image
alt="Mountains"
src={mountains}
fill
sizes="100vw"
style={{
objectFit: 'cover', // cover, contain, none
}}
/>
</div>
{/* And more images in the grid... */}
</div>
)
}
```

### Background Image

<Image
alt="Background image taking full width and height of page"
srcLight="/docs/light/background-image.png"
srcDark="/docs/dark/background-image.png"
width="1600"
height="427"
/>

```jsx
import Image from 'next/image'
import mountains from '../public/mountains.jpg'

export default function Background() {
return (
<Image
alt="Mountains"
src={mountains}
placeholder="blur"
quality={100}
fill
sizes="100vw"
style={{
objectFit: 'cover',
}}
/>
)
}
```

For examples of the Image component used with the various styles, see the [Image Component Demo](https://image-component.nextjs.gallery).

Expand Down

0 comments on commit b0a704d

Please sign in to comment.