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

Whenever I try to launch my React app using the `npm start` command in my command

After successfully creating a Simple React App and getting the happy hacking message on cmd, I encountered numerous errors when trying to run "npm start" on cmd. Despite multiple attempts at uninstalling and reinstalling node and npm, the issue persists. H ...

Having trouble accessing the information submitted to the server

This is the code for my express server: const express = require("express"); const app = express(); const mongoose = require("mongoose"); require("dotenv").config(); const cors = require("cors"); const userData = requ ...

Adding click functionality to dynamically generated list items in jQuery and HTML

I'm encountering an issue while trying to assign click events to dynamically added HTML elements in jQuery. Despite extensive research within this community, I find myself more confused than before. Below is the snippet of code causing me trouble: v ...

Is there a way to display two words side by side in React components?

I previously had the following code: projectName: project.get('name') === 'default' ? 'No Project' : project.get('name') In the render() method, it was written like this: <div className='c-card__projects&ap ...

Toggling the form's value to true upon displaying the popup

I have developed an HTML page that handles the creation of new users on my website. Once a user is successfully created, I want to display a pop-up message confirming their creation. Although everything works fine, I had to add the attribute "onsubmit= re ...

Using the -webkit-box-reflect property along with the CSS position:absolute styling

Running on the Chrome Canary build, I have come across this HTML code snippet: <div id="debug" class="debug" >TEST</div> Additionally, here is the CSS code snippet: -webkit-box-reflect: below 0px -webkit-gradient(linear, ...

What are some ways to ensure text stands out against a gradient backdrop?

Is there a way to ensure that text adjusts automatically to a background transition from one color to another using only CSS? I've tried using "mix-blend-mode:difference" from a forum, but it had no effect on the text. .container{ color: white; ...

A React component featuring a nested map function should always include a "unique key" prop for each element

I am trying to figure out how to assign a proper Key Value in this component: {var.map((building, index) => { const handles = building.buildingVertices.map((point) => { return ( <DrawingHandle key={`${i ...

Is there a way to monitor network requests (for debugging purposes) in React Native specifically on a Windows platform?

I've come across several solutions about viewing network requests in React Native on Mac OS, but I need help with debugging on Windows. Can anyone guide me on how to achieve this? ...

Challenge with Adding Padding to Icon and Label

https://i.stack.imgur.com/OSCnh.png Incorporating paddingtop to the Floating Label and positioning the DrawableRight Icon in the center. <android.support.design.widget.TextInputLayout android:id="@+id/input_layout_password" android:layout_wid ...

Enrollment in the course is conditional on two words being a perfect match

I have a concept in mind, but I'm struggling to piece it together. I have bits of different methods that just don't seem to align. That's why I'm reaching out for your expertise. Let's say I have 3 content containers with the clas ...

Having trouble getting CSS hover to work on hidden elements?

I am having trouble getting the rollover effect to work correctly on this page, and I can't seem to figure out what's causing the issue. My CSS is quite basic: open{visibility:hidden;} open:hover{visibility:visible;} If you would like to take ...

Setting the tab width in the Material UI App Bar

Exploring Material UI has been an exciting journey for me as a newcomer! However, I am facing a challenge in adjusting the width of each tab panel/label to ensure all three labels are visible at once. Currently, the last label, Upload, is extending beyond ...

Obtain identical socket event in two separate useEffect hooks

I am facing an issue where I want to access the same event in two different useEffect functions on the same page. Despite my attempts, it doesn't seem to work as expected. Below is what I have tried so far. I'm wondering if it's possible to ...

IE8 is not properly handling nested HTML elements that have the display:none property set

I am currently using jQuery to create a smooth fade-in effect for an <article> element that contains a <section> element. The outer element has CSS properties of display:none, position:fixed, and z-index:5. Meanwhile, the inner element is styl ...

Creating a vertical scrollable container that spans the full height of the PDF document

I am currently working on embedding a PDF file into my website. I'm trying to hide the status bar, including download buttons and page numbers, through some clever techniques so that I can control them externally. My goal is to be able to redirect use ...

Conceal/reveal specific sections of a table cell using jQuery

Here is a table I have: A header Another header First (some-text-initially-hidden) click row Upon clicking, it changes to: A header Another header First (some-text-should-be visible now) click row However, the text "some-text-initially-hi ...

Signal a return type error when the provided element label does not correspond with an existing entity

I am working on a component that accepts three props: children (React elements), index, and label. The goal is for the component to return the child element at a specific index when index is passed, and to return the element with a specific label when la ...

What is the best way to incorporate my Switch component into my Table component as a distinct column, while also maintaining separate states for each component?

My journey into learning React.js has been exciting so far. However, I am facing some confusion when it comes to stateful components. Currently, I am utilizing Bootstrap tables for creating a table and successfully fetching data using GET requests. Additio ...

Creating a redux store with an object using typescript: A step-by-step guide

Having recently started using Redux and Typescript, I'm encountering an error where the store is refusing to accept the reducer when working with objects. let store = createStore(counter); //error on counter Could this be due to an incorrect type set ...