Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next.js adding a css class to Link

How does one add a css class to a Link component in Next.js?

<Link
    className="app-subnav__link"
    href="/globalSettings"
>
    Settings overview
</Link>

It doesn't like the above! ES Link is throwing an error:

enter image description here

like image 654
J86 Avatar asked Jul 16 '21 09:07

J86


People also ask

How do I style a link in Nextjs?

In Next. js, <Link> components generally go with <a> tags. To style a link, we don't directly add our CSS class names or inline styles to <Link>. Instead, we do that with the <a> tag.

How do I link a component in Nextjs?

When linking between pages on websites, you use the <a> HTML tag. In Next. js, you can use the Link Component next/link to link between pages in your application. <Link> allows you to do client-side navigation and accepts props that give you better control over the navigation behavior.

How do I use external CSS in next JavaScript?

You can add the css file in head of nextjs. and in the render method, inside the return add a head tag similar to ordinary HTML, the head tag should be inside a div. also the css should be in static folder. Add a key to the link tag to reuse the css in multiple pages.

Does Next JS support CSS modules?

Adding Component-Level CSSNext. js supports CSS Modules using the [name]. module.

How do I add a CSS file to a next JS application?

This is possible because Next.js extends the concept of import beyond JavaScript. To add a stylesheet to your application, import the CSS file within pages/_app.js. For example, consider the following stylesheet named styles.css:

How to add styling to an active link in nextjs?

There are two different ways to add styling. The class is given to the element when it is active by writing: <NavLink to="/about" activeClassName="active"> About </NavLink> The styles is applied to the element when it is active by writing: 2. Create An Active Link In NextJS

How do I import a CSS file in nextnext JS?

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. To add a stylesheet to your application, import the CSS file within pages/_app.js.

How to add a link to the head of a page?

In your page components import Head and add a <link /> to your CSS. And that's it, this way Next.js should render the link tag in the head of the page and the browser will download the CSS and apply it. Sorry, something went wrong. Wow, thank you for the quick reply :).


Video Answer


1 Answers

Try this:

<Link href="/globalSettings"> 
    <a className="app-subnav__link">Settings Overview</a>
</Link>

Reference: https://nextjs.org/docs/api-reference/next/link

like image 120
Alireza Amini Avatar answered Sep 28 '22 07:09

Alireza Amini