This tutorial aims to help you understand how you can style your Next.js application using its built-in CSS support.
By the end of this tutorial, you will be able to:
- Import CSS files into your components
- Apply styles directly to your components
Before you begin, make sure you have:
Next.js allows you to import CSS files from a JavaScript file. This is possible because Next.js extends the concept of import beyond JavaScript.
Create a new .css file in your project. In this file, you can define your CSS rules. For example, you can create a file called styles.css in the styles directory.
You can import the CSS file into your JavaScript or TypeScript file using the import statement. The CSS file can be imported into any file in your Next.js project.
After importing the CSS file, the styles defined in the file are applied globally. That means the styles are applied to every page and component in your Next.js application.
// Import the CSS
import '../styles/styles.css';
// Your Next.js component
function HomePage() {
  return (
    <div className="container">
      <h1 className="title">Hello, World!</h1>
    </div>
  );
}
export default HomePage;
In the styles.css file:
.container {
  padding: 20px;
}
.title {
  color: blue;
}
In this example, the HomePage component imports the styles.css file. The styles defined in the CSS file are then applied to the elements in the component.
In this tutorial, you learned how to style your Next.js application using built-in CSS support. You learned how to create a CSS file, import it into a component, and apply the styles to your elements.
Create a new Next.js application and add a global style sheet.
Solution
npx create-next-app.styles directory._app.js file.Add a new component to your Next.js application and style it using a CSS file.
Solution
components directory.styles directory.Remember to practice regularly to become proficient in using CSS with Next.js. Happy coding!