How to use external stylesheets in next.js

Hello, in this post I am going to show you how you can use styles from an external sheet in a Next.js application.

Stylesheets are css files that we use to styles our webpages (i.e it tells browsers how to render a page). External stylesheets are seperate css files that can be accessed by creating a link within the head section of our webpage. 

Importing external stylesheets

In Next.js stylesheets are imported using the import code at the top of our code like so

import styles from "./blog_home.module.css";

The const "styles" is assigned to the imported stylesheet, while "./blog_home.module.css" is the location of the stylesheet with our next.js application.

Using external styles in jfx

After the stylesheet has been imported, we can use it in our jsx section by adding the name const we assigned to the stylesheet followed by the style name all enclosed within string literals and curly brackets like so

<div className={`${styles.example1}`}>
</div>

In the example above styles is the stylesheet name while row_background is the name of the style we wish to apply

Using multiple styles

Lets say for example you wish to apply more than one style on a particular jsx tag, that can be achieved by adding another external style seperated by a space but enclosed within the same string literal and curly brackets as shown below

<div className={`${styles.example1} ${styles.example2}`}>
</div>

Whereby example2 is the second style we wish to add.

Conclusion

External stylesheets are easy to use within our next.js application and its preferred to inline styling to reduce the amount of code on your page and easy resusability of styles. As always thanks for reading if you have any question about syling in next.js  then leave a comment down below and i'll do my best to promptly answer your question.

Author
author-image

Hello, my name is Abubakar Zakari. Am a budding fullstack developer from Nigeria who loves developing softwares and learning new frameworks and langauges.

Comment

Select image


Comments
No comment yet

DEVMAESTERS

Newsletter

Services

Frontend Development |Backend Development |Full Website Development |Bootstrap Website upgrades | Website Debbugging | Website Hosting & deployment

Contact

Interested in hiring me or collaborating with me on a project, click on any of the links below to get my social media handle

Or contact me via Tel: (+234)-806-225-7480 | Email: abubakarzakari1703@gmail.com

Copywright@devmaesters.com
Privacy Policy

By using our website,
you agree that devmaesters can store cookies on your device and disclose information in accordance with our privacy policy.