DEV Community

Cover image for Build a website with Next.js using next/images
George Arthur
George Arthur

Posted on

Build a website with Next.js using next/images

Creating a website with Next.js and using the next/image component involves several steps. Here’s a step-by-step guide to get you started:

1. Setting Up the Project

First, ensure you have Node.js installed on your system. Then, follow these steps:

Step 1: Create a New Next.js Project

npx create-next-app@latest my-nextjs-site
cd my-nextjs-site
Enter fullscreen mode Exit fullscreen mode

Step 2: Start the Development Server

npm run dev
Enter fullscreen mode Exit fullscreen mode

2. Basic Project Structure

Your project will have a structure similar to this:

my-nextjs-site/
├── public/
│   ├── images/
│   │   └── example.jpg
├── pages/
│   ├── _app.js
│   ├── index.js
├── styles/
│   ├── globals.css
├── package.json
├── next.config.js
Enter fullscreen mode Exit fullscreen mode

3. Using next/image Component

The next/image component optimizes images automatically. Here’s how you can use it:

Step 1: Import next/image in Your Page
Open pages/index.js and modify it to include the Image component.

import Image from 'next/image'

export default function Home() {
  return (
    <div>
      <h1>Welcome to My Next.js Site</h1>
      <Image 
        src="/images/example.jpg" 
        alt="Example Image" 
        width={500} 
        height={300} 
      />
    </div>
  )
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Add an Image to the Public Directory
Place your image (e.g., example.jpg) in the public/images directory. Next.js will serve images from this directory.

4. Configuring next.config.js for External Images (Optional)

If you plan to use external images, you need to configure the next.config.js file.

Step 1: Create next.config.js (if not existing)

module.exports = {
  images: {
    domains: ['example.com'], // Replace with your image source domain
  },
}
Enter fullscreen mode Exit fullscreen mode

5. Adding Styles

You can style your components using CSS, Sass, or any other styling method supported by Next.js.

Step 1: Modify styles/globals.css

body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 0;
}

h1 {
  text-align: center;
  margin-top: 50px;
}

div {
  text-align: center;
}
Enter fullscreen mode Exit fullscreen mode

6. Final Directory Structure

Your directory structure should look like this:

my-nextjs-site/
├── public/
│   ├── images/
│   │   └── example.jpg
├── pages/
│   ├── _app.js
│   ├── index.js
├── styles/
│   ├── globals.css
├── package.json
├── next.config.js
Enter fullscreen mode Exit fullscreen mode

7. Running Your Project

Start the development server again:

npm run dev
Enter fullscreen mode Exit fullscreen mode

Open your browser and navigate to http://localhost:3000 to see your Next.js site with the optimized image.

Additional Tips

  • Dynamic Imports: Use dynamic imports for components that are heavy or only needed on certain conditions.
  • API Routes: Next.js supports API routes which can be used to create backend functionality within the same project.
  • Static Site Generation (SSG) and Server-Side Rendering (SSR): Leverage these features for better performance and SEO.

Conclusion

This guide provides a basic setup for a Next.js project using the next/image component. Depending on your project requirements, you can further expand this setup with more pages, components, styles, and functionality.

Top comments (0)