I want the aspect ratio to stay the same, whilst the width is fixed. Please help me automatically modulate the height. I would like to continue to use the Nextjs Image
component if possible.
const Logo = ({ logo }) => {
if (logo) {
return (
<Box
sx={{
width: "200px",
// position: "relative",
// overflow: "hidden",
height: "fit-content",
"& img": {
transition: "transform .5s ease",
"&:hover": {
transform: "scale(1.05)",
},
},
}}
>
<Image
data-cy="employerPublicProfileLogo"
objectFit="contain"
layout="responsive"
width={250}
height={250}calculation
placeholder="blur"
blurDataURL="/hiredinchina-loading-image.jpg"
src={logo}
alt="Logo"
/>
</Box>
);
}
};
2
Answers
To make the Image take all the space, you can use the prop "fill" without giving a hight or a width, then give the parent a position "relative" and the other styles you need.
You could put the Image component inside a div and control the image height width by that div. Here is an example: