Next Image 속성

joyoung·2024년 6월 14일

Next 공식 Image 예제 레포지토리

https://github.com/vercel/next.js/tree/fd9c524a72636844cc7091dd5aca713701ebb1f2/examples/image-component/app

기본 사용법

import Image from 'next/image'
 
export default function Page() {
  return (
    <div>
      <Image
        src="/profile.png"
        width={500}
        height={500}
        alt="Picture of the author"
      />
    </div>
  )
}

기본 필수값 속성

PropertyValueTypeNotes
srcsrc="/profile.png"StringRequired
widthwidth={500}Integer (px)Required
heightheight={500}Integer (px)Required
altalt="Picture of the author"StringRequired

전체 속성

PropertyValueTypeNotes
srcsrc="/profile.png"StringRequired
widthwidth={500}Integer (px)Required
heightheight={500}Integer (px)Required
altalt="Picture of the author"StringRequired
loaderloader={imageLoader}FunctionOptional
fillfill={true}BooleanOptional
sizessizes="(max-width: 768px) 100vw, 33vw"StringOptional
qualityquality={80}Integer (1-100)Optional
prioritypriority={true}BooleanOptional
placeholderplaceholder="blur"StringOptional
stylestyle={{objectFit: "contain"}}ObjectOptional
onLoadingCompleteonLoadingComplete={img => done())}FunctionDeprecated
onLoadonLoad={event => done())}FunctionOptional
onErroronError(event => fail()}FunctionOptional
loadingloading="lazy"StringOptional
blurDataURLblurDataURL="data:image/jpeg..."StringOptional
overrideSrcoverrideSrc="/seo.png"StringOptional

반응형으로 이미지를 보여주고 싶을때

종횡비를 모를때 이미지를 꽉 차게 표현하기 위해

부모태그를 감싸주고
style: objectFit 속성
size 태그와
fill 속성을 준다

import Image from 'next/image'
 
export default function Page({ photoUrl }) {
  return (
    <div style={{ position: 'relative', width: '300px', height: '500px' }}>
      <Image
        src={photoUrl}
        alt="Picture of the author"
        sizes="300px"
        fill
        style={{
          objectFit: 'contain',
        }}
      />
    </div>
  )
}
``
profile
꾸준히

0개의 댓글