Is there a way to modify the color of the text within the Link tag of the react-router in a react component?

Struggling to modify the text color in my Navbar component within the Link tag of react-router-dom, but it's stubbornly refusing to change.

Utilizing styled components

Beneath is the complete code for my navbar component

import React from 'react'
import styled from 'styled-components';
import {Link} from 'react-router-dom'

const Navbar = () => {
  return (
    <>
    <Nav>
      <Title>
        PortFolio
      </Title>
      <ul>
        <li><Link to='/'>Home</Link></li>
        <li><Link to='/contact'>Contact</Link></li>
      </ul>
    </Nav>
    <div>

    </div>
    </>
  )
}

export default Navbar


const Nav = styled.div`
  height: 70px;
  width: 100%;
  background-color: black;
  color: white;
  display: flex;
  align-items: center;
  padding: 0 30px;

  ul {
    display: flex;
    list-style: none;
    
    li {
      margin: 0 20px;
      font-size: 20px;
      text-decoration-color: red;
    }
  }

`
const Title = styled.span`
  font-size: 40px;
  font-family: 'Ubuntu', sans-serif; 

`

The color: white; property added to the Nav element should alter the text color, yet no luck so far.

I've attempted adding styles in the ul and li elements without success.

Including the text-decoration property hasn't produced any changes either.

If anyone can advise on how to successfully modify the text color and other properties, I'd greatly appreciate it.

Answer №1

To customize the Link tag, you can utilize a style attribute:

const linkStyle = {
  margin: "1rem",
  textDecoration: "none",
  color: 'blue'
};

function Nav() {
  return (
    <NavUnlisted>
      <Link to="/" style={linkStyle}>
        Home
      </Link>
      <Link to="/about" style={linkStyle}>
        About
      </Link>
    </NavUnlisted>
  );
}

For more details, visit: https://codesandbox.io/s/styling-react-router-linksstarter-template-tctnj?file=/src/Nav.js:253-460

https://dev.to/ridhikgovind/how-to-style-your-react-router-links-using-styled-components-2350

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

Adjust the code to enhance the functionality of web components in Sharepoint

I recently came across some code online that I'm trying to modify in order to add an expanding button next to a web part on my Sharepoint site. However, the problem is that by default, all web parts are already expanded and require a click to collapse ...

Having trouble with the Wordpress JS CSS combination not functioning properly and unable to determine the cause

I recently set up a page on my WordPress site using the Twenty Seventeen theme. At the top of the page, I created a toolbar for users to easily adjust the font size and background color. Check out the image below for reference: See Image for Bar Here&apo ...

Setting up an existing project in reactJS

I'm completely new to reactJS and node, so my inquiry may seem quite basic. After downloading an existing project from gitlab using gitbash on my Windows system, I noticed that the project utilizes ReactJS, node, and webpack. However, when attempting ...

Stop the bootstrap navbar from resizing

I implemented a bootstrap navbar that looks like this... <nav class="navbar navbar-inverse"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="#">WebSiteName</a> </div& ...

Combining functions in React is a powerful way to create

Is there a way to create a single function that combines handleSelectAll and handleSelectNone for toggling options in a list of categories? Instead of using a toggle (on/off) approach, I believe having separate buttons for Select All and Select None is mor ...

Sharing information between React components involves passing data as props. By sending data from

Brand new to React and still figuring things out. Here's the scenario: <div> <DropDown> </DropDown> <Panel> </Panel> </div> After selecting a value in the dropdown and storing it as currentL ...

Margin of Contents in a Table

I have created a table within a box on my webpage. The default setting centers the table inside the box, but I want it to align with the bottom border of the box instead. Each row in the table contains two cells, each cell displaying a picture that I would ...

Database Collection or Record

Considering transitioning from Filemaker to a MERN app has me intrigued. While I grasp the basics of NoSQL databases, it's definitely a different mindset. Although I've replicated my needs in MySQL, the allure of Mongo DB (or Firestore) is undeni ...

How can I implement maskInput in React while disallowing empty spaces?

I am struggling to restrict input to numbers only in Firefox while using the material-ui TextField component. I have tried various workarounds but haven't been successful. The closest I got was by using a mask like this: <Mui.TextField ...

React: What is the best way to dynamically render images by iterating through a list?

Currently, I am attempting to iterate through an array of objects. Each object within the staff array in my JSON contains an imgUrl: { "home": { ... "staff": [ { ... "imgUrl": "../Images/Jon ...

When including the basePath in the next.config.js file, next-pwa fails to function properly for the following 10 instances

Currently, I am utilizing nextJS version "10.0.9" along with next-pwa version "^5.0.6" and they are functioning well together to power my app. However, due to certain requirements, I found the need to introduce a prefix for all my routes. This led me to ad ...

checkbox inspired by the design of the iPhone

I'm looking to create a custom checkbox! <label id="sliderLabel"> <input type="checkbox" /> <span id="slider"> <span id="sliderOn">SELECTED</span> <span id="sliderOff">SELECT</span> ...

Is there a way to design a CSS layout that automatically adjusts text wrapping based on the size of the page and columns?

I am attempting to design a CSS page that mimics the appearance of an aged newspaper. The focus is on ensuring that the page is responsive, with a single column of text on smaller devices. However, the main issue I need help with lies elsewhere. For larg ...

Ensure Website Accessibility by Implementing Minimum Resolution Requirements

Is it possible to create a website that only opens on screens with a resolution of at least 1024 x 768, and displays an error message on unsupported resolutions? I've tried using JavaScript to achieve this, but haven't had any success. Any assis ...

Issue encountered: missing photo data during the transfer of an image from ReactJS to MongoDB

When an image is uploaded to my website, it is sent to the backend for storage in MongoDB database. The base64 of the image is uploaded to the database, but occasionally the photo data retrieved can be "false". For example: (3) [{…}, {…}, {…}] 0 : p ...

Is there a way to determine if the current path in React Router Dom v6 matches a specific pattern?

I have the following paths: export const ACCOUNT_PORTAL_PATHS = [ 'home/*', 'my-care/*', 'chats/*', 'profile/*', 'programs/*', 'completion/*', ] If the cur ...

When CSS Display table is resized at media query breakpoints, it causes other cells to disappear

This is a discussion on creating a responsive image gallery website with images of different sizes and no margins or paddings. The issue at hand is that the .showcase class hides at a specific @media breakpoint when resizing, but there is a desire to displ ...

Loading properties dynamically in ReactJS depending on the environment

When working on a ReactJS based application, I encountered the challenge of making API calls with different URLs depending on the environment. How can I dynamically load these URLs when the application is running in a specific environment? I initially att ...

Hide or show list items in an unordered list using jQuery

I am interested in creating a script that can toggle between "show more" and "show less" functionality for elements in a list. I came across this script: HTML <ul id="myList"> <li>One</li> <li>Two</li> <li> ...

Can a line be created using only CSS without the need for an additional HTML element?

There have been many inquiries regarding drawing lines without adding an additional HTML element. Consider this scenario with an HTML div element: <div class="myDiv"></div> Accompanied by the following CSS: .myDiv{ width: 100px; height: ...