Next.js Tutorial - 2
To create the
pages,components,public, andstylesfolders in your Next.js project, and demonstrate how to use them, follow these steps:1. Create the folder structure
Use the following commands to create the required folders:
mkdir components public styles2. Set up your folders
pagesfolder: This folder is automatically created in Next.js projects and holds route-based pages. Next.js will automatically set up routing for files within this folder. If you don't have it, create it using the command:mkdir pagescomponentsfolder: Store all your reusable components here. For example, a button or a header component.publicfolder: Store static files like images, fonts, etc. These files will be accessible from the root URL.stylesfolder: Add your global CSS and CSS modules for styling components.
3. Create and use components
Let's create a basic
Headercomponent and style it with CSS.Create a
Headercomponent:mkdir components/Header touch components/Header/Header.js touch styles/Header.module.csscomponents/Header/Header.js:import styles from '../../styles/Header.module.css'; function Header() { return ( <header className={styles.header}> <h1 className={styles.title}>Welcome to My Website</h1> </header> ); } export default Header;styles/Header.module.css:.header { background-color: #0070f3; color: white; padding: 20px; text-align: center; } .title { font-size: 2rem; }4. Add global styles
Create a
globals.cssfile to hold your global styles:touch styles/globals.cssstyles/globals.css:body { font-family: 'Arial', sans-serif; margin: 0; padding: 0; background-color: #f0f0f0; } a { color: inherit; text-decoration: none; } * { box-sizing: border-box; }5. Use global styles and components
To use the global styles and the
Headercomponent in your Next.js project, modify the main layout file and the home page.For
pagesrouter (traditional Next.js)pages/_app.js: Import global styles here.import '../styles/globals.css'; function MyApp({ Component, pageProps }) { return <Component {...pageProps} />; } export default MyApp;pages/index.js: Use theHeadercomponent and add some additional content.pages/index.js:import Header from '../components/Header/Header'; export default function Home() { return ( <div> <Header /> <main> <h2>This is the home page</h2> <p>Welcome to your Next.js app!</p> </main> </div> ); }
For
approuter (if using Next.js 13+ with theappdirectory)app/layout.js: Import global styles here.app/layout.js:import '../styles/globals.css'; export default function RootLayout({ children }) { return ( <html lang="en"> <body>{children}</body> </html> ); }app/page.js: Use theHeadercomponent and add content.app/page.js:import Header from '../components/Header/Header'; export default function Home() { return ( <div> <Header /> <main> <h2>This is the home page</h2> <p>Welcome to your Next.js app!</p> </main> </div> ); }
6. Add images to the public folder
To use static assets like images, place them in the
publicfolder. For example, if you placelogo.pngin thepublicfolder, you can access it as/logo.png.Here’s how you can use an image:
<img src="/logo.png" alt="Logo" width={100} height={100} />7. Run the app
After setting everything up, run the Next.js development server:
npm run devVisit
http://localhost:3000in your browser. You should now see theHeadercomponent along with the home page content.Summary
pagesorapp: Defines route-based pages in Next.js.components: Stores reusable components.public: Holds static files (e.g., images).styles: Holds global and component-specific styles.