Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
b0a704d8
Unverified
Commit
b0a704d8
authored
Jun 19, 2023
by
Lee Robinson
Committed by
GitHub
Jun 19, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
docs: add better examples to `next/image` docs. (#51457)
parent
7d0bdab8
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
105 additions
and
2 deletions
+105
-2
01-images.mdx
.../01-building-your-application/05-optimizing/01-images.mdx
+105
-2
No files found.
docs/02-app/01-building-your-application/05-optimizing/01-images.mdx
View file @
b0a704d8
...
...
@@ -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).
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment