Build a Next.js Interactive Web-Based Simple Portfolio Website

Written by

in

In today’s digital landscape, a personal portfolio website is more than just an online resume; it’s a dynamic platform to showcase your skills, projects, and personality. For developers, a well-crafted portfolio can be the key to landing that dream job or attracting new clients. This tutorial will guide you through building a simple, yet effective, portfolio website using Next.js, a powerful React framework known for its server-side rendering and static site generation capabilities. We’ll focus on creating a portfolio that is easy to navigate, visually appealing, and optimized for search engines.

Why Choose Next.js?

Next.js offers several advantages for building a portfolio website:

  • Performance: Next.js’s server-side rendering (SSR) and static site generation (SSG) features ensure fast loading times, crucial for user experience and SEO.
  • SEO-Friendly: SSR and SSG make your website easily crawlable by search engines, improving your visibility in search results.
  • Developer Experience: Next.js provides a great developer experience with features like hot module replacement, built-in routing, and easy deployment.
  • Scalability: Next.js is designed to handle websites of all sizes, making it a great choice for future growth.

Prerequisites

Before we begin, make sure you have the following installed:

  • Node.js and npm (or yarn): You’ll need Node.js and npm (Node Package Manager) or yarn installed on your system.
  • A Code Editor: A code editor like Visual Studio Code, Sublime Text, or Atom.
  • Basic Knowledge of HTML, CSS, and JavaScript: Familiarity with these technologies is essential.
  • A GitHub Account (Optional): For version control and deployment.

Project Setup

Let’s start by creating a new Next.js project. Open your terminal and run the following command:

npx create-next-app my-portfolio
cd my-portfolio

This command will create a new Next.js project named “my-portfolio” and navigate you into the project directory. You can replace “my-portfolio” with your preferred project name.

Project Structure

Your project directory should look something like this:


my-portfolio/
├── node_modules/
├── package.json
├── pages/
│   ├── _app.js
│   ├── index.js
│   └── api/
│       └── hello.js
├── public/
│   └── ...
├── .gitignore
├── next.config.js
└── README.md
  • pages/: This directory contains your application’s pages. Each file in this directory represents a route. For example, `index.js` corresponds to the `/` route.
  • public/: This directory is for static assets like images, fonts, and other files.
  • package.json: This file contains project dependencies and scripts.
  • next.config.js: This file is used to configure Next.js.

Building the Homepage (index.js)

Let’s start by modifying the `pages/index.js` file to create our homepage. We’ll create a simple layout with a header, a brief introduction, a section for your projects, and a footer.

Open `pages/index.js` and replace the existing code with the following:

import Head from 'next/head';

export default function Home() {
  return (
    <>
      <Head>
        <title>Your Name - Portfolio</title>
        <meta name="description" content="Your Name's Portfolio - showcasing projects and skills" />
        <link rel="icon" href="/favicon.ico" />
      </Head>

      <main className="container mx-auto p-4">
        <h1 className="text-3xl font-bold mb-4">Your Name</h1>
        <p className="mb-4">Welcome to my portfolio! I'm a [Your Profession] with a passion for [Your Passion].</p>

        <h2 className="text-2xl font-bold mb-2">Projects</h2>
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
          {/* Project Cards will go here */}
        </div>
      </main>

      <footer className="text-center p-4 mt-8 border-t">
        <p>&copy; {new Date().getFullYear()} Your Name. All rights reserved.</p>
      </footer>
    </>
  );
}

In this code:

  • We import the `Head` component from `next/head` to manage the page’s “ section, including the title and meta description for SEO.
  • We use a `main` element with a `container` class for basic layout and padding.
  • We add a heading (`h1`) for your name and a brief introductory paragraph.
  • We create a section for your projects (`h2` and a `div` with a grid layout). We’ll add project cards later.
  • We include a simple footer with a copyright notice.

Important: The code uses Tailwind CSS classes for styling. You’ll need to install Tailwind CSS in your project (see the “Styling with Tailwind CSS” section below) or replace these classes with your preferred CSS framework or custom CSS.

Styling with Tailwind CSS

Tailwind CSS is a utility-first CSS framework that allows you to style your website directly in your HTML. It’s a great choice for rapidly prototyping and building user interfaces.

To install Tailwind CSS, run the following commands in your terminal:

npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

This will install Tailwind CSS, PostCSS, and Autoprefixer, and create a `tailwind.config.js` file in your project root. Next, configure your template paths in `tailwind.config.js`:


/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./app/**/*.{js,ts,jsx,tsx,mdx}",
    "./pages/**/*.{js,ts,jsx,tsx,mdx}",
    "./components/**/*.{js,ts,jsx,tsx,mdx}",

    // Or if using `src` directory:
    "./src/**/*.{js,ts,jsx,tsx,mdx}",
  ],
  theme: {
    extend: {
      // ...
    },
  },
  plugins: [],
}

Then, add the Tailwind directives to your global CSS file. Create a file named `styles/globals.css` (or use the one that Next.js creates by default) and add the following:


@tailwind base;
@tailwind components;
@tailwind utilities;

Finally, import this CSS file in your `pages/_app.js` file:

import '../styles/globals.css'

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}

export default MyApp

Now, you can use Tailwind CSS classes in your HTML. For example, `className=”text-3xl font-bold mb-4″` in the `index.js` file styles the `h1` element.

Adding Project Cards

Let’s create some project cards to showcase your work. We’ll create a simple component for this.

Create a new file named `components/ProjectCard.js` in your project’s root directory and add the following code:

export default function ProjectCard({ title, description, imageUrl, liveLink, githubLink }) {
  return (
    <div className="bg-white rounded-lg shadow-md p-4"
         style={{
           transition: 'transform 0.2s ease-in-out',
         }}
         onMouseEnter={(e) => {
           e.currentTarget.style.transform = 'scale(1.05)';
         }}
         onMouseLeave={(e) => {
           e.currentTarget.style.transform = 'scale(1)';
         }}
    >
      <img src={imageUrl} alt={title} className="mb-2 rounded-md" />
      <h3 className="text-lg font-semibold mb-2">{title}</h3>
      <p className="text-gray-700 mb-2">{description}</p>
      <div className="flex space-x-2">
        {liveLink && (
          <a href={liveLink} target="_blank" rel="noopener noreferrer" className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
          >
            Live Demo
          </a>
        )}
        {githubLink && (
          <a href={githubLink} target="_blank" rel="noopener noreferrer" className="bg-gray-500 hover:bg-gray-700 text-white font-bold py-2 px-4 rounded"
          >
            GitHub
          </a>
        )}
      </div>
    </div>
  );
}

This component takes props for the project’s title, description, image URL, live link, and GitHub link. It displays the project information and links to the live demo and GitHub repository (if provided).

Now, let’s use the `ProjectCard` component in `pages/index.js`. First, import the component:

import ProjectCard from '../components/ProjectCard';

Then, inside the `<div className=”grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4″>` section, add the following code. Replace the example project data with your own project details.


  <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
    <ProjectCard
      title="Project 1 Title"
      description="A brief description of project 1.  Highlight key features and technologies used." 
      imageUrl="/project1.png" // Replace with your image path
      liveLink="#" // Replace with your live demo link
      githubLink="#" // Replace with your GitHub repository link
    /
    >
    <ProjectCard
      title="Project 2 Title"
      description="A brief description of project 2. Highlight key features and technologies used." 
      imageUrl="/project2.png" // Replace with your image path
      liveLink="#" // Replace with your live demo link
      githubLink="#" // Replace with your GitHub repository link
    /
    >
    <ProjectCard
      title="Project 3 Title"
      description="A brief description of project 3. Highlight key features and technologies used." 
      imageUrl="/project3.png" // Replace with your image path
      liveLink="#" // Replace with your live demo link
      githubLink="#" // Replace with your GitHub repository link
    /
    >
  </div>

Make sure to replace the placeholder image paths (`/project1.png`, `/project2.png`, `/project3.png`) with the actual paths to your project images in the `public` directory. Also, update the `liveLink` and `githubLink` with the appropriate URLs for each project.

Adding Images

To display images in your project cards, you’ll need to place your image files in the `public` directory. For example, if you have an image named `project1.png`, you would place it in the `public` directory. Then, in your `ProjectCard` component, you can reference the image using the path `/project1.png`.

Consider optimizing your images for web use. Use a service like TinyPNG or ImageOptim to compress your images without sacrificing quality. This will improve your website’s loading speed.

Adding More Pages (About, Contact, etc.)

To add more pages to your portfolio (e.g., About, Contact), create new files in the `pages` directory. For example, to create an About page, create a file named `pages/about.js`:

import Head from 'next/head';

export default function About() {
  return (
    <>
      <Head>
        <title>About - Your Name</title>
        <meta name="description" content="Learn more about Your Name" />
      </Head>

      <main className="container mx-auto p-4">
        <h1 className="text-3xl font-bold mb-4">About Me</h1>
        <p>Write a detailed description about yourself, your skills, experience, and interests.</p>
      </main>
    </>
  );
}

Similarly, create a `pages/contact.js` file for your contact page. You can include a contact form or your contact information here.

Important: Next.js automatically creates routes based on the file structure in the `pages` directory. The `about.js` file will be accessible at the `/about` route, and the `contact.js` file at the `/contact` route.

Adding Navigation

To navigate between pages, you’ll need to add a navigation menu. Create a `components/Navbar.js` component:

import Link from 'next/link';

export default function Navbar() {
  return (
    <nav className="bg-white shadow-md p-4">
      <div className="container mx-auto flex justify-between items-center">
        <Link href="/">
          <a className="text-xl font-bold">Your Name</a>
        </Link>
        <div className="space-x-4">
          <Link href="/">
            <a>Home</a>
          </Link>
          <Link href="/about">
            <a>About</a>
          </Link>
          <Link href="/contact">
            <a>Contact</a>
          </Link>
        </div>
      </div>
    </nav>
  );
}

This component uses the `Link` component from Next.js to create navigation links. Import and use this component in your `pages/_app.js` file to make it available on all pages:

import '../styles/globals.css'
import Navbar from '../components/Navbar';

function MyApp({ Component, pageProps }) {
  return (
    <>
      <Navbar />
      <Component {...pageProps} />
    </>
  )
}

export default MyApp

Now, the navigation bar will be displayed at the top of each page.

Deployment

Next.js offers several deployment options. One of the easiest ways to deploy your portfolio is using Vercel, which is the platform created by the Next.js team.

Here’s how to deploy your website to Vercel:

  1. Create a Vercel Account: If you don’t already have one, sign up for a free Vercel account at https://vercel.com/.
  2. Connect your GitHub Repository: In your Vercel dashboard, click “Add New Project” and connect your GitHub repository where your portfolio code is stored.
  3. Import the Project: Vercel will automatically detect that it’s a Next.js project. Click “Import” to deploy your project.
  4. Configure Deployment (Optional): You can configure environment variables and other settings in the Vercel dashboard.
  5. Deploy: Vercel will build and deploy your website. Once the deployment is complete, you’ll receive a unique URL for your website.

You can also deploy to other platforms like Netlify or use a custom server. Refer to the Next.js documentation for more deployment options.

SEO Best Practices

To ensure your portfolio website ranks well in search engines, follow these SEO best practices:

  • Use Descriptive Titles and Meta Descriptions: As shown in the code examples, write concise and informative titles and meta descriptions for each page.
  • Optimize Image Alt Attributes: Provide descriptive `alt` attributes for all your images. This helps search engines understand the content of your images.
  • Use Heading Tags (H1-H6): Structure your content using heading tags to indicate the hierarchy of information.
  • Create a Sitemap: Generate a sitemap (e.g., using `next-sitemap`) and submit it to search engines to help them crawl your website.
  • Use Clean URLs: Next.js automatically generates clean URLs based on your file structure.
  • Mobile-Friendly Design: Ensure your website is responsive and looks good on all devices.
  • Fast Loading Speed: Optimize your images, use server-side rendering or static site generation, and minimize the use of third-party scripts to improve loading speed.
  • Content is King: Write high-quality, original content that provides value to your visitors.

Common Mistakes and How to Fix Them

  • Incorrect File Paths: Double-check your file paths when importing components or referencing images. Typos can easily cause errors.
  • Missing Tailwind CSS Configuration: If your Tailwind CSS styles are not being applied, ensure that you have correctly installed and configured Tailwind CSS, and that your global CSS file is importing the Tailwind directives.
  • Incorrect Image Paths: Make sure your image paths in the `ProjectCard` component are correct, relative to the `public` directory.
  • Not Using SEO Best Practices: Take the time to optimize your website for search engines. This will significantly improve your visibility.
  • Ignoring Mobile Responsiveness: Test your website on different devices to ensure it’s responsive and provides a good user experience on all screen sizes. Use CSS media queries or a framework like Tailwind CSS to handle responsiveness.

Summary / Key Takeaways

Building a portfolio website with Next.js is a rewarding project that can significantly boost your online presence. We’ve covered the essentials, from project setup and page creation to styling and deployment. Remember to tailor your portfolio to reflect your unique skills and experience. Regularly update your portfolio with your latest projects and achievements to keep it fresh and engaging. By following the steps outlined in this tutorial and incorporating SEO best practices, you can create a professional and effective portfolio that helps you stand out from the crowd.

FAQ

1. Can I use a different CSS framework instead of Tailwind CSS?

Yes, you can use any CSS framework you prefer, such as Bootstrap, Material UI, or styled-components. The key is to include the framework’s CSS in your project and apply the appropriate classes or styling to your components.

2. How do I handle contact form submissions?

You can use a third-party service like Formspree or Netlify Forms to handle form submissions without needing to set up a backend server. Alternatively, you can create a serverless function in Next.js to handle form submissions and send emails.

3. How do I add a blog to my portfolio?

You can create a blog section by creating a new page (e.g., `pages/blog.js`) and fetching blog posts from a content management system (CMS) like Contentful or Sanity, or from a static file source (e.g., Markdown files). You’ll also need to create individual blog post pages using dynamic routes.

4. How can I improve the performance of my portfolio?

To improve performance, optimize your images, use server-side rendering or static site generation, minimize the use of third-party scripts, and consider code splitting to load only the necessary code for each page.

5. Is it necessary to use a CMS for my portfolio?

No, it’s not strictly necessary, especially if your portfolio content is relatively static. You can hardcode your projects and skills directly into your Next.js components. However, if you plan to frequently update your content or add a blog, using a CMS can make content management much easier and more efficient.

Your portfolio website is a living document, reflecting your evolving skills and accomplishments. Embrace the opportunity to experiment with new technologies, refine your design, and continuously improve your online presence. The more you invest in your portfolio, the greater the returns in terms of career opportunities and professional recognition.

” ,
“aigenerated_tags”: “Next.js, Portfolio, Website, React, Development, Tutorial, SEO, Tailwind CSS, Frontend