Fill parent container and reduce image resolution with next/image
You can configure the deviceSizes
property in next.config.js
to match the breakpoints used in your app, as those are used to generate the next/image
's srcset
s, and serve the correctly sized image.
If you know the expected device widths of your users, you can specify
a list of device width breakpoints using thedeviceSizes
property innext.config.js
. These widths are used when thenext/image
component
useslayout="responsive"
orlayout="fill"
to ensure the correct image
is served for user's device.
The above should be paired with the sizes
attribute on the next/image
if the rendered image takes up less than the full viewport width.
If you are using
layout="fill"
,layout="responsive"
, orlayout="raw"
it's important to assignsizes
for any image that takes up less than
the full viewport width.For example, when the parent element will constrain the image to
always be less than half the viewport width, usesizes="50vw"
. Withoutsizes
, the image will be sent at twice the necessary resolution,
decreasing performance.
As an example, let's assume your app uses the following device breakpoints [320, 640, 1024, 1280, 1920]
, and you're rendering a 240x240
image.
Update the deviceSizes
property to match the breakpoints:
// next.config.js
module.exports = {
images: {
deviceSizes: [320, 640, 1024, 1280, 1920],
// Other existing `images` configs
},
// Other existing configs
}
Then set the sizes
property on the next/image
to suggest the ideal image size.
<div style={{ position: 'relative', width: 240, height: 240 }}>
<Image
src="<image-url>"
layout="fill"
sizes="50vw"
/>
</div>
You should adjust the above values to better fit your use case.
couldn't reduce the size of image in next/image component
In your example, in the sandbox, you are usinglayout="fill"
This will overwrite your size properties to fill the entire parent.
Remove that property and see that your width and height options are accepted.
https://codesandbox.io/s/cave-forked-egtko?file=/components/Header.js
How to make NextJS Image component 100% height and width for its parent div?
Ok, I see that you want the image to take the width and height of its parent element, you can just use layout="fill"
instead of responsive
, which causes the image to expand to fill its parent element.
Something like this:
<div style={{width: '100%', height: '100%', position: 'relative'}}>
<Image
alt='Mountains'
src='/mountains.jpg'
layout='fill'
objectFit='contain'
/>
</div>
Next.JS Image `layout='fill'` is broken
The wrapping div should have position: relative
:
export default function Home() {
return (
<div>
<div style={{width: '100px', height: '100px', position: 'relative'}}>
<Image src={"/i.png"} layout='fill'/>
</div>
</div>
)
}
This is a consequence of how position: absolute
works. It's containing block will be the nearest ancestor element that has any position value but static
(the initial value).
How do I auto-resize an image to fit a 'div' container?
Do not apply an explicit width or height to the image tag. Instead, give it:
max-width:100%;
max-height:100%;
Also, height: auto;
if you want to specify a width only.
Example: http://jsfiddle.net/xwrvxser/1/
img {
max-width: 100%;
max-height: 100%;
}
.portrait {
height: 80px;
width: 30px;
}
.landscape {
height: 30px;
width: 80px;
}
.square {
height: 75px;
width: 75px;
}
Portrait Div
<div class="portrait">
<img src="http://i.stack.imgur.com/xkF9Q.jpg">
</div>
Landscape Div
<div class="landscape">
<img src="http://i.stack.imgur.com/xkF9Q.jpg">
</div>
Square Div
<div class="square">
<img src="http://i.stack.imgur.com/xkF9Q.jpg">
</div>
Related Topics
How to Give Cross Browser Transparency to Element's Background Only
What Actually Happens to CSS in High Contrast Mode
Repeat Dots in Less/Sass for Content Property
Lesscss - Ie Gradient Filter with Variables and Lighten
Background Image Width Not 100% on Ipad
Vertical Align Multiple Image with Different Height
Next-Sibling Combinator Should Not Work with Complex Selectors
Horizontally Centering Elements to Their Container While Avoiding Overlap
Viewport-Unit Font-Size and Zooming Bug: Which Browsers Are Affected
Google Chrome Bug - Overflow:Auto | Scroll Doesn't Work
How to Combine Compass with Bless
Overflow with Absolute/Relative Positioning Layout
Position:Fixed in Windows Phone 7
How to Make Sidebar with Same Height as The Content Div
CSS Selector Compare 2 Attributes
How to Select My CSS Module Class Using Document.Queryselector