Personalize the hover effect and underline style of links in material-ui

I am currently delving into the world of material-ui custom styles and came across this helpful resource. However, I find myself a bit lost in how to properly override the classes using withStyles.

      <Breadcrumbs aria-label="breadcrumb" separator=" " className="menu">
        <Link color="inherit" href="/home">
          Home
        </Link>
      </Breadcrumbs>

In an attempt to solve this issue, I made changes to the global app.css file.

a:hover{
  border-bottom: 1px solid red
}

The result displayed was as follows:

Home
----
----  <- Upon hovering over the link, there are now two underlines present. The lower one is rendered in red.

My goal is to achieve the following:

Home
---- <- only displaying a single red underline upon hover

Answer №1

Instead of using CSS for styling, you can achieve the same effect by utilizing classes in your code:

button: {
    background-color: "#ff0000",
    "&:hover": {
        background-color: "#0000ff",
        border: "1px solid #0000ff"
    }
},

Answer №2

Implement a custom theme for Material UI by following the guidelines provided here.

To specifically address link hover underlining, use the code snippet below:

import { createTheme } from "@mui/material";

export const myTheme = createTheme({
  components: {
    MuiLink: {
      styleOverrides: {
        root: {
          textDecoration: "none",
          ":hover": {
            textDecoration: "underline",
          },
        },
      },
    },
  },
});

Answer №3

When it comes to a Link, which is essentially an <a> tag, the key property you should be focusing on overriding is text-decoration-color, not border-bottom. For more targeted styling, consider assigning a specific className to the Link and defining styles for that particular class.

Component:

  <Breadcrumbs aria-label="breadcrumb" separator=" " className="menu">
    <Link className="custom-link" to="/">
      Home
    </Link>
  </Breadcrumbs>

Style:

.custom-link:hover {
  color: red;
  text-decoration-color: red;
}

For a live demonstration, visit: https://codesandbox.io/s/cool-bush-wpn4m

Answer №4

Another solution suggested in a previous response is to use this code:

a {  
  text-decoration-color: red;  
}

To make this work, there are two options available:

  • Define the styling for anchor tags in index.css and apply it globally across the entire web application as you have already done.
  • Alternatively, you can define the styles within the style object passed when using the withStyles HOC. For added control, consider wrapping the Link tag in a div and applying styles to that div. This way, the changes will be localized, and the Link tag will inherit the properties from its parent div.

Answer №5

For those looking for a solution to customize the default properties of MuiLink without affecting global settings, consider the MUI approach of overriding props in your theme. Here is an example:

const customTheme = responsiveFontSizes(
  createTheme({
    components: {
      MuiLink: {
        defaultProps: {
          underline: "hover",
        },
        styleOverrides: {
          underlineHover: {
            "&:hover": {
              textDecorationColor: "red",
            },
          },
        },
      },
    },
  })
);

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

Are there any real-world examples you can share of utilizing CSS 3D Transforms?

Can you provide any instances of CSS 3D Transforms being utilized in real-world projects besides and ? ...

The changes made in the CSS file are not reflected in the browser

Recently, I encountered a strange issue with my new server where the CSS changes were not reflecting in the browser. Despite trying to refresh and clear the cache, the problem persisted. To investigate further, I used FileZilla to check if the updated CSS ...

When a row is clicked, retrieve the data specific to that row

I have implemented a data-grid using react-table where I pass necessary props to a separate component for rendering the grid. My issue is that when I click on a particular row, I am unable to retrieve the information related to that row using getTrProps. ...

Reacting - page is not refreshing

Describing my current scenario: I have a requirement where I need to be able to navigate to a /details page by clicking on an image. However, when I click on the image, the URL gets refreshed but my component does not load. I attempted various solutions ...

Is there a way to extract the text from the inner div of an element using nightwatch.js?

I'm attempting to retrieve the content of a cell within a table, with the following CSS structure: <div data-testid="cellvalue_row-1_col-0" class="Table-cellContent" xpath="1"><span data-testid="tableCellCon ...

What is the best way to ensure that circles only touch each other by their edges?

Trying to align three circles touching each other has been a challenge for me. Although I have successfully managed to make two touch, the third one remains elusive. How can I ensure that all three circles are in contact with each other, especially when th ...

Best practices for organizing and storing a todo list in ReactJS using local storage

import React, { useState, useEffect } from 'react'; const TodoList = () => { const [tasks, setTasks] = useState(JSON.parse(localStorage.getItem('tasks')) || []); const [newTask, setNewTask] = useState(''); con ...

Tips on preventing users from navigating backwards in a React Redux application

Seeking guidance on how to restrict user access after successful login in react redux. Can anyone assist me? I want to prevent users from navigating back to certain routes once they are logged in I am currently working on securing the routes to block unau ...

What are the benefits of removing event listeners in Reactjs?

In my opinion, the event listeners need to be reliable and consistent. React.useEffect(() => { const height = window.addEventListener("resize", () => { setWindowSize(window.innerHeight); }); return () => window.remov ...

What is the best way to restrict the selection of specific days of the week on an HTML form date input using a combination of JavaScript, React, and HTML?

I need help customizing my Forms Date Input to only allow selection of Thursdays, Fridays, and Saturdays. I've searched for a solution but haven't been successful so far. Is there any JavaScript or HTML code that can help me achieve this? Below ...

Having trouble loading series data after fetching API with React and Redux-Thunk

Issue Greetings everyone, I am currently working on an app using react + redux, and I am facing an issue with the 'series' property that is coming from: const mapStateToProps = (state) => { return { series: state.serieReducer } } ...

Parent whose height is less than that of the child element

I'm working on creating a side menu similar to the one on this website. However, I'm facing an issue with the height of the #parent list element. I want it to match the exact same height as its content (the #child span - check out this jsfiddle). ...

PHP/CSS: Backgrounds for DIVs have been implemented, but they appear to be transparent

Update: Check out the link for some old photos here: I have a PHP file where I am creating divs with images as backgrounds using the code below (within a while-loop): echo ' <div class="pic" style="background:url('.$directory.'/'.$ ...

Effortlessly implement CSS styling in a scoped shadow element with Vue3

I am facing an issue with applying styles to an anchor element in my code. Below is a snippet of the code: <template> <custom-button> #shadow-root (open) <a href="#"></a> <other-custom></other-c ...

Importing CSS properties from MUI v5 - A comprehensive guide

I'm working with several components that receive styles as props, such as: import { CSSProperties } from '@material-ui/styles/withStyles' // using mui v4 import because unsure how to import from v5 paths import { styled } from '@mui/mat ...

I possess an array of objects that includes both images and documents. My goal is to examine the mime_type of each object and select the first element in React to be displayed within an <img> tag

I have an array of objects that contain images and documents. My goal is to display the first image only if the mime_type is 'image/jpeg' or 'image/png'. I am working with React. Despite my attempts, I keep encountering undefined resul ...

Tidying up following the execution of an asynchronous function in useEffect

Recently, I've been facing a challenge while migrating a React project to TypeScript. Specifically, I'm having trouble with typing out a particular function and its corresponding useEffect. My understanding is that the registerListener function s ...

Every time a button is clicked on the interface, a new element or button will appear. This functionality is made possible through

Currently diving into a new React.js project as a beginner. I have a little challenge in the code snippet below - looking to add a button that displays "Hello World" every time it's clicked using setState. Can anyone guide me on enhancing the DisplayM ...

Whenever I attempt to run 'npx create-react-app', it keeps throwing an error saying 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY'

My attempt at creating a React app was by using the following commands: npx create-react-app my-app However, I encountered an error message: UNABLE_TO_GET_ISSUER_CERT_LOCALLY. I also tried using this command: npm init react-app my-app ...

HTML set hover & active states to remain active indefinitely

NOTE: My project utilizes Angular, so Angular may offer a solution to this issue as well I am in the process of creating a page where I can preview and test out various styles that I have designed. In order to achieve this, I need to somehow activate both ...