Next.js Discord

Discord Forum

Making Responsive Images

Answered
ASittingDuck posted this in #help-forum
Open in Discord
I am using the <Image> component from nextjs, but it seems like all the photos are the same size when I look at the rendered <img> element, even though the system says it should be making sizing differences. Am I doing this wrong? How do you configure what sizes should be generated for different breakpoints?

IndexCard component for an example (uses page router on nextjs 13)
import Link from 'next/link'
import Image from 'next/image'
import styles from '@/styles/components/courses/indexCard.module.scss'
import PropTypes from 'prop-types'

const IndexCard = ({ slug, title, date, image, ratings, author }) => {
  return (

    <div className={styles.container}>
      <div className={styles.header}>
        <Link href={`/courses/${slug}`}>
          <Image src={image} alt={title} width={400} height={200}/>
        </Link>
      </div>
      <div className={styles.content}>
        <div className={styles.rating}>
          <p>Rating: {ratings.average} ({ratings.count})</p>
        </div>
        <Link href={`/courses/${slug}`}>
          <h3 className={styles.title}>{title}</h3>
        </Link>
        <p className={styles.date}>{date}</p>
        <div className={styles.meta}>
          <div className={styles.classInfo}>
            <p>Number Enrolled: 0</p>
            <p>Hours Estimated: 2</p>
          </div>
          <div className={styles.author}>
            <p>Author: {author}</p>
          </div>
        </div>
      </div>
      <div className={styles.footer}>
        <Link className={styles.button} href={`/courses/${slug}`}>
          Enroll Course
        </Link>
      </div>
    </div>)
}

IndexCard.propTypes = {
  slug: PropTypes.string,
  title: PropTypes.string,
  date: PropTypes.string,
  image: PropTypes.string,
  ratings: PropTypes.object,
  author: ProptTypes.string
}

export default IndexCard
Answered by Gharial
@ASittingDuck You need to add the sizes prop. Something like this:
          <Image
            src={image}
            alt={title}
            width={400}
            height={200}
            sizes="(max-width: 640px) 100vw, (max-width: 768px) 50vw, 33vw"
          />
View full answer

3 Replies

Bumping to see if others can see this.
Gharial
@ASittingDuck You need to add the sizes prop. Something like this:
          <Image
            src={image}
            alt={title}
            width={400}
            height={200}
            sizes="(max-width: 640px) 100vw, (max-width: 768px) 50vw, 33vw"
          />
Answer