코드
1. 이미지 원본 비율 유지하기
<PreviewImage src={el.imgSrc} alt="" width={200} height={200} />
const PreviewImage = styled(Image)`
width: auto;
height: auto;
`;
2. 부모 요소 너비만큼 채우기
<PreviewImage>
<PreviewImage src={el.imgSrc} alt="" width={200} height={200} />
<PreviewImage/>
const PreviewContainer = styled(Box)`
width: 300px;
min-height:300px;
`;
const PreviewImage = styled(Image)`
width: auto;
height: auto;
object-fit:contain; // 이미지 비율을 유지하기
object-fit:cover; // 부모 요소 너비 꽉 채우고 넘치는 부분을 잘라내기
`;
참고자료
https://nextjs.org/docs/pages/api-reference/components/image#onerror
Components: Image | Next.js
Optimize Images in your Next.js Application using the built-in `next/image` Component.
nextjs.org
'프론트엔드 > Next.js' 카테고리의 다른 글
[Next.js] Next.js + Typescript 환경에서 카카오 JS SDK 사용하기 (2) | 2025.05.08 |
---|---|
[Next.js] 다이나믹 임포트를 활용한 하이드레이션 오류 해결방법 | Error: Hydration failed because the server rendered HTML didn't match the client. (0) | 2025.04.30 |
[Next.js] 파일 제출 버튼 스타일링 예시 코드 (1) | 2025.04.17 |
[Next.js] PWA 웹앱 구글 플레이 스토어 배포 방법 A to Z (1) | 2025.04.08 |
[Next.js] Next.js + Supabase 동적 사이트맵 생성 방법 (0) | 2025.04.07 |