How to useCSS modules in ReactJS

To change the color of a link in React using CSS modules, create a CSS file with a unique class name, define the desired color property for that class, and import it into your React component.

CSS modules modularize CSS in React by defining styles in a separate file and importing them into the component.

Example: This example use CSS to change the color of links on hover.

Javascript




// Filename - App.js
 
import React from "react";
import "./style.css";
export default function App() {
    return (
        <nav className="navbar">
            <ul className="navMenu">
                <li>
                    <a href="#" className="link">
                        Home
                    </a>
                </li>
                <li>
                    <a href="#" className="link">
                        About
                    </a>
                </li>
                <li>
                    <a href="#" className="link">
                        Contact
                    </a>
                </li>
            </ul>
        </nav>
    );
}


CSS




/* Filename - App.js */
 
.navbar {
    background-color: #333;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
}
 
.navMenu {
    list-style: none;
    display: flex;
}
 
.link {
    color: #fff;
    text-decoration: none;
    padding: 10px;
    font-size: 1.4rem;
}
 
/* Changing the color */
.link:hover {
    color: red;
}


Step to Run the Application: To Run Application: Open the terminal and type the following command.

npm start

Output: This output will be visible on the http://localhost:3000/ on the browser window.

How to Change the Color of a Link in ReactJS ?

In this article, we’ll see how we can change the color of a link in ReactJS. To change a link’s color in React, add a CSS style to the link element using the style attribute in JSX code.

Default link styles in HTML and CSS often include blue color and underlining. However, they may not align with your React app’s design. You might need to change the link color to match your app’s theme, and branding, or differentiate link states.

Syntax:

<a href="#"> Home </a>

Similar Reads

Prerequisites:

NPM & Node JS React JS React Styled-components...

Steps to Create React Application:

Step 1: Create a react application by using this command...

Approach 1: Using CSS modules

To change the color of a link in React using CSS modules, create a CSS file with a unique class name, define the desired color property for that class, and import it into your React component....

Approach 2: Using styled-components

...