What is the best way to modify the text color within a Navbar, especially when the Navbar is displayed within a separate component?

First question on StackOverflow.

I have a Next App and the Navbar is being imported in the _app.js file.

import Navbar from "../Components/Navbar";

function MyApp({ Component, pageProps }) {
  return (
    <>
      <Navbar />
      <Component {...pageProps} />
    </>
  );
}

The color of Navbar links is grey by default, which works fine on most pages with a light-colored background. However, on some pages, the color is not visible.

I am looking for a way to change the color of the Navbar links only when we are on a specific page. For example, when visiting the page http://localhost:3000/model3, the Navbar links should be white.

I am using the module approach for the Navbar and SASS as the CSS compiler with a Navbar.module.scss

Model3.js

import React from "react";
import section from "../styles/section.module.css";
import S from "../styles/models.module.css";
import E from "../styles/model3.module.css";
import Link from "next/link";
import Head from "next/head";
import { Fade } from "react-reveal";

function model3() {
  return (
    <div>
      <Head SameSite="None">
        <title>Model 3 | Tesla</title>
        <link
          rel="icon"
          href="https://www.tesla.com/themes/custom/tesla_frontend/assets/favicons/favicon.ico"
        />
      </Head>

      <div className={S.Wrapper}>
        <div className={S.Maincontainer}>
          <div className={S.Container}>
            <div className={E.TopSection}>
              <Fade bottom>
                <div className={section.carname}>
                  <div className={section.title}>
                    <h2 style={{ color: "white" }}>Model 3</h2>
                  </div>
                </div>
              </Fade>
              <Fade bottom>
                <div className={E.Details}>
                  <div className={S.DetailsContainer}>
                    <div className={S.DetailsHeading}>
                      <h3>3.1 s</h3>
                    </div>
                    <div className={S.DetailsData}>0-60 mph*</div>
                  </div>
                  <div className={S.DetailsContainer}>
                    <div className={S.DetailsHeading}>
                      <h3>358 mi</h3>
                    </div>
                    <div className={S.DetailsData}>Range (EPA est.)</div>
                  </div>
                  <div className={S.DetailsContainer}>
                    <div className={S.DetailsHeading}>
                      <h3>AWD</h3>
                    </div>
                    <div className={S.DetailsData}>Dual Motor</div>
                  </div>
                  <div className={S.DetailsContainer}>
                    <Link href="">
                      <span>Order Now</span>
                    </Link>
                  </div>
                </div>
              </Fade>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}
export default model3;

If there is anything else I should include, please let me know. Thank you.

Please note that all Navlinks in the Navbar are currently grey. To improve visibility, I need to change them to white specifically on this page.

Here is how the page looks like.

Answer №1

There are numerous approaches you can take:

  • Utilize your route hooks - React Router offers useLocation(), where you can check the location and apply a class to change behavior according to the path.

  • Pass props to your navbar component.

  • Add a class to the body depending on the route, and then style your navbar using CSS .dark .navbar

The most suitable method will vary based on the layout of your project.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to vertically center my footer text?

footer { text-align: center; color: #FFF; position: fixed; bottom: 0px; width: 100%; height: 90px; background-color: #464646; } <footer> &copy; Name </footer> I am looking to center my name within the footer, however, it cu ...

What is the best way to create a scrollable Material UI modal and dialog?

Having a problem with my mui modal where the top content is getting cut off and I can't scroll up. I've tried adding the {overflow:"scroll"} property to the modal but it's not working. Here's the code snippet I'm currentl ...

"Conceal the DIV only when neither of the switches are turned on

Is there a way to hide a paragraph only when two collapsible switch buttons are turned off in my Bootstrap frontend form? I have two checkboxes with corresponding switches, and the paragraph should only be hidden when both switches are off, but I'm no ...

What exactly does the .proxy() method do in jQuery?

Can you explain the purpose of the jQuery.proxy function in jQuery and describe the scenarios where it is most beneficial? I came across this link, but I'm struggling to grasp its concept fully. ...

What is the best way to eliminate the appearance of the blue square that shows up when the button is clicked on smaller screens?

When testing my project on different screen sizes in Chrome dev tools or on my phone, I noticed a blue square briefly appearing around the button when it is clicked. I attempted to remove this by setting outline: none or outline: 0 on various pseudo-classe ...

Designing a calendar with a grid template

I am attempting to design a calendar that resembles an actual calendar, but I am facing an issue where all created divs (representing days) are being saved in the first cell. I am not sure how to resolve this. Any help would be greatly appreciated. css . ...

Steps for Adding a class or Id to an Ext.Msg.alert box

Is there a way to customize the style of a specific Ext alert box without affecting all alert boxes? Can someone please explain how to assign a class or ID to an Ext.Msg.alert box? Ext.Msg.alert('Status', 'Changes saved successfully.' ...

When the program is executed, immediately use .trigger('click')

There is a spelling game that features a grid filled with hidden words. The objective of the game is to spell out these words by clicking on the letters of the alphabet, aided by hints such as images and sounds. Players are given the word they need to spe ...

learn how to customize the appearance of text for a group in vis.js by utilizing the groupTemplate feature

I am currently working on an application that utilizes vis.js to create timeline events in a React environment. My goal is to enhance each event by adding an icon to it. To accomplish this, I am utilizing the groupTemplate feature and intend to render a co ...

Guide to retrieving the primary key auto-increment value for the ID field in an SQL record using PHP Scripting

I am currently working on a multi-page web survey form and I have a PHP script called process.php that handles my responses on www.buythosecars.com. This script stores the responses in a MySQL table and then redirects the user to www.buythosecars.com/surve ...

Placing a pair of labels onto a bootstrap form while ensuring column alignment

I'm trying to format a form according to my UI/UX specifications, where there is a parent label and two sub-labels. Can this be achieved using Bootstrap? https://i.stack.imgur.com/J8wJZ.png However, I'm struggling to align the columns and rows ...

A step-by-step guide on integrating SASS into a React project using Vite

I've recently set up a project using React with Vite and I'd like to incorporate SASS as well. Currently, I have it installed and typically I open a Git Bash terminal and run sass -w sass:css. However, I'm wondering if there's a more e ...

Is There a Glitch in IE9? Unexplained Expansion of DIV Element

Here is a small code sample that I have: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Test</title> <style type="text/css"> table.Sample { width: 600px; table-layout: fixed; } table.Sample ...

Manipulate the way in which AngularJS transforms dates into JSON strings

I am working with an object that contains a JavaScript date, structured like this: var obj = { startTime: new Date() .... } When AngularJS converts the object to JSON (for instance, for transmission via $http), it transforms the date into a string as ...

Is it possible to retrieve the values of #select1 and #select2 before initiating the AJAX request?

I am presented with the following snippet of HTML code: <select id="choice1"> <option value="0">Option 0</option> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3 ...

Whiskey Angostura and Straight Row Set to 8, not 12

Recently, I incorporated Bourbon, Bitters, and Neat into my rails application. However, I am encountering an issue where the number of grid-columns is stuck at 8 instead of the desired 12. To address this problem, I have taken the following steps: Ensur ...

Creating trails by following the cursor's movement in KineticJS

Currently, I am working on a drawing application using KineticJS. While I have successfully used it to draw shapes and straight lines by following the method outlined in KineticJS - Drawing Lines with Mouse, I now need to draw a line along the mouse's ...

How to implement dynamic color for classes in a v-for loop using Vue.js?

Struggling with a seemingly simple issue that may be easy for VueJS pros. In my data, I have JSON objects with an attribute that can take values of 10, 20, 30, or 40. Depending on this value, I want to change the color of a v-btn. I've attempted mul ...

Are there any specific steps I should take to ensure that my server can support jQuery.getJSON when using a bookmarklet?

Currently, I am in the process of creating a bookmarklet that will require some user details to be input. After researching my options for cross domain communication, I have found that my best choices are either using jQuery.getJSON or adding a form and i ...

Encountering an issue while trying to initiate npm start command for a ReactJS application on an

While attempting to deploy my node and react app on AWS ec2, I encountered an issue. The node app is working fine, but the react app is giving an error when running npm run build. I also tried using npm start, but unfortunately, it resulted in the same er ...