How to import image in Next.js ?
Last Updated :
05 Jan, 2023
Next.js is a full-stack React-based framework. Unlike a traditional react app, which loads and renders the entire application on the client, Next.js allows the first-page load to be rendered by the server, which is great for SEO and performance.
Some of the key features of Next.js are:
- Server-side rendering
- Image optimization
- Static Site generation
- Incremental Site regeneration.
You can learn more about Next.js here. In this article, we will learn to import images in Next.js using the Image component.
Image component in Next.js (next/image): Next.js Image component is an evolved form of <img/> element in HTML. It comes built-in with performance optimization which helps in achieving good Core web vitals. This performance boost is factored in Google's ranking algorithm, hence improving the ranking of our website.
The Image component supports the following built-in optimizations:
- Improved Performance: It serves different image sizes for each device, which reduces image size for smaller devices and thus improves performance.
- Faster Page Loads: Images are not loaded until they enter the viewport, hence enabling the web page to load faster.
- Asset Flexibility: It supports various configurations such as resizing the Image component via props.
- Visual Stability: It automatically prevents Cumulative Layout Shift, which is a layout error that occurs when elements get shifted after being rendered by DOM. It determines the overall stability of our website's layout
Properties of Image Component: The image components support the following props:
- src (required): This property accepts a path string, an external URL, or a locally imported image.
- width (required): It represents the image's rendered or original width in pixels. For locally imported images, this property is optional.
- height (required): It represents the rendered height or the image's original height in pixels. For locally imported images, this property is optional.
- layout: It determines the behavior of the image size when the viewport width changes. It supports the following values:
- intrinsic: It is the default behavior, which scales the image down to the width of the container, up to the image size.
- fixed: It keeps the image fixed to the given width and height.
- responsive: It scales the image to fit the container's dimensions.
- fill: It causes the image to fill the container. It also makes the width and height properties optional because the container will determine them.
- raw: It allows the image to be rendered without any automatic behavior.
Working with Image component: Run the following command to create a new Next.js project.
npx create-next-app@latest gfg
Project Structure:
For the scope of this article, we will only focus on the public and pages directory. The public directory contains all the static files that need to be served when the Next.js app is built for deployment.
In this example, we'll create and add three images with different sources, one will be imported from the public directory, the second image will be served through the static path from the public directory and the other one will be served from an external URL. You can add your image to the public directory (Here we've added gfgLogo.png). And for the external URL, we're using this image, but for the external URL to work, we'll have to add its domain name to the next.config.js file to protect our application from malicious users.
We'll first clean up some boilerplate code in the pages/index.js (Home Page) and import the Image component.
pages/index.js
import Image from "next/image";
const HomePage = () => {
return (
<>
{/* This is a local import, so the
height and width props are optional */}
<div>
<Image src={gfgLogo}
alt="GFG logo imported from public directory" />
</div>
{/* This image is also served from public
directory but using the static path */}
<div>
<Image
src="
https://media.geeksforgeeks.org/wp-content/cdn-uploads/20210420155809/gfg-new-logo.png"
alt="GFG logo served with static path of public directory"
height="100"
width="400"
/>
</div>
{/* This image is being served from an
external URL, for this URL to work we
will need to add the hostname
'media.geeksforgeeks.org' to our
next.config.js file */}
<div>
<Image
src="
https://media.geeksforgeeks.org/wp-content/cdn-uploads/20210420155809/gfg-new-logo.png"
height="100"
width="400"
alt="GFG logo served from external URL"
/>
</div>
</>
);
};
export default HomePage;
/next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
images : {
domains : ['media.geeksforgeeks.org']
}
}
module.exports = nextConfig
Step to run the application: Run your Next app using the following command:
npm run dev
Output:
Similar Reads
How to import SASS in Next.js ? Sass is the short form of Syntactically Awesome Style Sheet. It is an upgrade to Cascading Style Sheets (CSS). Sass is a CSS pre-processor. It is fully compatible with every version of CSS. Importing SASS in Next.js allows you to use SCSS for styling your application. This integration enables advanc
2 min read
How to ignore ESLint in Next.js ? In this article, we are going to see how to ignore ESLint in Next.Js. Follow the below steps to ignore ESLint in the Next.js application:ESLint: ESLint is a JavaScript linter that is designed to help identify and correct problems in JavaScript code. ESLint is based on JSHint, but ESLint has more fea
2 min read
How to add Image Carousel in Next.js ? In this article, we are going to learn how we can add an Image Carousel in NextJS. NextJS is a React-based framework. It has the power to Develop beautiful Web applications for different platforms like Windows, Linux, and mac.ApproachTo create image carousel in next.js we are going to use a react-re
2 min read
How to Import SVGs Into Your Next.js Apps? Next.js is a powerful React framework that simplifies building and deploying web applications. Incorporating SVGs (Scalable Vector Graphics) into your Next.js projects can enhance your user interface with high-quality, scalable images. In this article, we will see how to Import SVGs Into Your Next.j
5 min read
How to Add a Background Image in Next.js? Next.js is a powerful React framework that allows for server-side rendering and the generation of static websites. Adding a background image to your Next.js application can enhance the visual appeal of your web pages. This article will guide you through the process of adding a background image to a
3 min read
ImageResponse Next.js ImageResponse is a powerful feature that allows you to generate images dynamically on the server side using JSX and CSS. To use an ImageResponse function, you need to import it from "next/og". It uses Vercel's open graph library to generate dynamic images. The ImageResponse constructor, by allowing
2 min read
How to use React Icons in Next.js ? Icons make the website beautiful and interactive. Icons are an important part of a website's user interfaces, which are used in expressing objects, actions, and ideas. They are used to communicate the core idea and intent of a product or action and express the object visually. Here, we will learn to
3 min read
How to add Calendar in Next.js ? Adding a calendar to a Next.js application enhances scheduling and event management. We can just install and use the available npm package. In this article, we are going to learn how we can add a calendar loader in NextJS.ApproachTo add our calendar we are going to use the react-calendar package. Th
2 min read
How to add File Dropper in Next.js ? Adding a file dropper feature in Next.js involves setting up a drag-and-drop interface, handling file uploads on the frontend, and processing the files on the server side. In this article, we are going to learn how we can add File Dropper in NextJS.ApproachTo add our File Dropper we are going to use
2 min read
Next.js Dynamic Import Unlike standard import modules, dynamic import modules are flexible in terms of when and how they are loaded. Instead of being forced to upload a module file during reading, a powerful import can be requested during use. With code separating the module into a separate batch file it can be downloaded
3 min read