287

I have the following:

enter image description here

How do I get rid of the blue underline? The code is below:

<Link to="first"><MenuItem style={{paddingLeft: 13, textDecoration: 'none'}}> Team 1 </MenuItem></Link>

The MenuItem component is from http://www.material-ui.com/#/components/menu

dbc
  • 104,963
  • 20
  • 228
  • 340
Jo Ko
  • 7,225
  • 15
  • 62
  • 120

35 Answers35

371

I see you're using inline styles. textDecoration: 'none' is used in child, where in fact it should be used inside <Link> as such:

<Link to="first" style={{ textDecoration: 'none' }}>
  <MenuItem style={{ paddingLeft: 13 }}>Team 1</MenuItem>
</Link>

<Link> will essentially return a standard <a> tag, which is why we apply textDecoration rule there.

starball
  • 20,030
  • 7
  • 43
  • 238
Grgur
  • 7,092
  • 2
  • 19
  • 34
  • 9
    there a way to set global with textdecoration none ? in the app.css ? – stackdave Oct 08 '17 at 15:08
  • 5
    It works :). Be aware that if you copy paste the style to your .css (because, of course, you don't like inline styles) then is `text-decoration: none;` – David Torres Feb 17 '18 at 18:16
178

I think the best way to use react-router-dom Link in a MenuItem (and other MaterialUI component such as buttons) is to pass the Link in the "component" prop

<Menu>
   <MenuItem component={Link} to={'/first'}>Team 1</MenuItem>
   <MenuItem component={Link} to={'/second'}>Team 2</MenuItem>
</Menu>

you need to pass the route path in the 'to' prop of the "MenuItem" (which will be passed down to the Link component). In this way you don't need to add any style as it will use the MenuItem style

Daniele Urania
  • 2,658
  • 1
  • 15
  • 11
99

If you are using styled-components, you could do something like this:

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


const StyledLink = styled(Link)`
    text-decoration: none;

    &:focus, &:hover, &:visited, &:link, &:active {
        text-decoration: none;
    }
`;

export default (props) => <StyledLink {...props} />;
JoeTidee
  • 24,754
  • 25
  • 104
  • 149
89

There's also another way to properly remove the styling of the link. You have to give it style of textDecoration='inherit' and color='inherit' you can either add those as styling to the link tag like:

<Link style={{ color: 'inherit', textDecoration: 'inherit'}}>

or to make it more general just create a css class like:

.text-link {
    color: inherit;
    text-decoration: inherit;
}

And then just <Link className='text-link'>

Panos
  • 1,764
  • 21
  • 23
17

You can add style={{ textDecoration: 'none' }} in your Link component to remove the underline. You can also add more css in the style block e.g. style={{ textDecoration: 'none', color: 'white' }}.

<h1>
  <Link style={{ textDecoration: 'none', color: 'white' }} to="/getting-started">
    Get Started
  </Link>
</h1> 
Thomas Ebert
  • 447
  • 6
  • 15
Shubham Verma
  • 8,783
  • 6
  • 58
  • 79
15
a:-webkit-any-link {
  text-decoration: none;
  color: inherit;
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Enravel
  • 151
  • 1
  • 2
12

If someone is looking for material-ui's Link component. Just add the property underline and set it to none

<Link underline="none">...</Link>

mdanishs
  • 1,996
  • 8
  • 24
  • 50
10

There is the nuclear approach which is in your App.css (or counterpart)

a{
  text-decoration: none;
}

which prevents underline for all <a> tags which is the root cause of this problem

Dave Pile
  • 5,559
  • 3
  • 34
  • 49
10

Material UI v5+

You should be able to globally customize MUI component styles, such as:

import { createTheme } from '@mui/material'

const theme = createTheme({
  components: {
    MuiLink: {
      styleOverrides: {
        root: {
          textDecoration: 'none',
        },
      },
    },
  },
})

const App = ({ currentAccount, neighborhoodsWithPropertyCount }) => (
  <ThemeProvider theme={theme}>
    <Router>
      <Routes>
        <Route path="/" element={<Home />} />
      </Routes>
    </Router>
  </ThemeProvider>
)

export default App

However, more often than not, you should actually be using the Link component from react-router-dom, in which case links would have no text decoration by default.

Flavio Wuensche
  • 9,460
  • 1
  • 57
  • 54
7

//CSS

.navigation_bar > ul > li {
      list-style: none;
      display: inline;
      margin: 2%;
    }

    .link {
      text-decoration: none;
    }

//JSX

 <div className="navigation_bar">
            <ul key="nav">
              <li>
                <Link className="link" to="/">
                  Home
                </Link>
              </li>
            </ul>
          </div>
Kushal Atreya
  • 183
  • 2
  • 7
5

Its pretty simple. Just add this style={{ textDecoration: 'none' }} inside of your <Link> tag

<Link to="first" style={{ textDecoration: 'none' }}>
   <MenuItem style={{ paddingLeft: 13 }}>
         Team 1
   </MenuItem>
CS Alam
  • 177
  • 1
  • 6
4

The underline comes by default from the react-router-dom package. You can do the following to fix the issue.

<Link to="/route-path" style={{ textDecoration: 'none' }}> 
    // Rest of the code
</Link>
Suresh Mangs
  • 705
  • 8
  • 19
  • This will work, and to make life easier you can also make a re-useable component like and use that so you don't have to repeatedly apply this styling across your whole app. – Matt S. Nov 02 '21 at 18:16
3

Working for me, just add className="nav-link" and activeStyle{{textDecoration:'underline'}}

<NavLink className="nav-link" to="/" exact activeStyle= 
  {{textDecoration:'underline'}}>My Record</NavLink>
vuvo
  • 51
  • 6
3

Look here -> https://material-ui.com/guides/composition/#button.

This is the official material-ui guide. Maybe it'll be useful to you as it was for me.

However, in some cases, underline persists and you may want to use text-decoration: "none" for that. For a more cleaner approach, you can import and use makeStyles from material-ui/core.

import { makeStyles } from '@material-ui/core';

const useStyles = makeStyles(() => ({
    menu-btn: {
        textDecoration: 'none',
    },
}));

const classes = useStyles();

And then set className attribute to {classes.menu-btn} in your JSX code.

2

jsx:

<Link className="link">
  test
</Link>

css:

.link{
    text-decoration: none;
}
Serhii Zadorozhnyi
  • 576
  • 2
  • 8
  • 25
2

CSS Solution also for React

  1. Add className/class in the Link(React Router) tag only.(Most Important Part!! Add ClassName in Link tag not any other.)
  2. Add text-decoration: none in the css file.

Nav.js

  <Link to="/" className="link" >
    Get Started
  </Link>

Nav.css

.link{
text-decoration: none;
}
ZebraCoder
  • 1,480
  • 1
  • 5
  • 12
1

To expand on @Grgur's answer, if you look in your inspector, you'll find that using Link components gives them the preset color value color: -webkit-link. You'll need to override this along with the textDecoration if you don't want it to look like a default hyperlink.

enter image description here

AlleyOOP
  • 1,536
  • 4
  • 20
  • 43
1
style={{ backgroundImage: "none" }}

Only this worked for me

tudorprodan
  • 950
  • 8
  • 18
1

I have resolve a problem maybe like your. I tried to inspect element in firefox. I will show you some results:

  1. It is only the element I have inspect. The "Link" component will be convert to "a" tag, and "to" props will be convert to the "href" property:

  1. And when I tick in :hov and option :hover and here is result:

As you see a:hover have text-decoration: underline. I only add to my css file:

a:hover {
 text-decoration: none;
}

and problem is resolved. But I also set text-decoration: none in some another classes (like you :D), that may be make some effects (I guess).

Neil Nguyen
  • 349
  • 2
  • 7
1
<Link
   to='/maxpain'
   replace
   style={{
           textDecoration: 'none'
          }}
   >
     <LinkText>Click here!</LinkText>
</Link>

Simple as that!

Mohamed Jakkariya
  • 1,257
  • 1
  • 13
  • 16
1

the <Link /> tag basically is <a> tag on render time, so you can just write

a { text-decoration: none; }

and it worked for me :) Good luck

hamidreza nikoonia
  • 2,007
  • 20
  • 28
Quoc Ly
  • 11
  • 1
  • This has been stated several times by others. You could improve your answer by citing documentation and providing formatted code samples. – Besworks Apr 05 '22 at 19:31
0
<Link to="/page">
    <Box sx={{ display: 'inline-block' }}>
        <PLink variant="primary">Page</PLink>
    </Box>
</Link>

In some cases when using another component inside the Gatsby <Link> component, adding a div with display: 'inline-block' around the inner component, prevents underlining (of 'Page' in the example).

servrox
  • 239
  • 3
  • 9
0

Well you can simply use this piece of code in your scss file; This will remove that unwanted color change,

a:-webkit-any-link {
  &:hover {
    color: white;
  }
}

Joel Jaimon
  • 202
  • 3
  • 10
0

I had a problem where the Link element was changing my h4 to 'underline', setting text-decoration: 'none' did not work, my only solution was to use a button instead.

<Link to="/settings">
    <button>Settings</button>
</Link>
tomstan11
  • 1,047
  • 7
  • 9
0

standard a-link and react-link are the same.

so if you are styling a-link, it will automatically style react-link.

a{ what ever styling you want }

fruitloaf
  • 1,628
  • 15
  • 10
0

Just

<Link
   to={`$path`}
   style={{ borderBottom: "none" }}> 
    .... 
</Link>
0

I find this question and no one of the answers really resolve the problem at all in a general case (e.g. if the elements isn't a MenuItem). I suggest:

import {useHistory} from "react-router-dom";
const MyComp = () => {
  const history = useHistory();
  return <div>
    <AnyComponent onclick={()=>history.push('/path/u/want')}
  </div>
}
0

I just added two lines and worked for me :)

{
 text-decoration: none; 
 color: black;
}
Shakeel Haider
  • 107
  • 1
  • 5
0
 a {
  text-decoration: none !important;
  color: black !important; 
  font-size: 20px;
}

used !important in App.css

tastytim
  • 141
  • 1
  • 3
0

Add css style in

a:link {
  text-decoration: none;
  color: #cc850a;
}

a:visited {
  text-decoration: none;
  color: #cc850a;
}

a:hover {
  text-decoration: none;
  color: #47a1ad;
}

a:active {
  text-decoration: none;
}
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
0

It's 2023 and has pseudo-class is widely supported. Since MenuItems are rendered as <li> we can define a css rule like this:

a:has(> li) {
  text-decoration: unset;
  color: unset;
}

This only modifies <a> elements that have a <li> child element.

0

I found two main ways:

with MUI Link API

 import { Link } from '@mui/material';

 <Link
    href={`https://www.example.com/?symbol=${myValue}`}
    underline="none"
    color="inherit"
    target="_blank"
    rel="noopener noreferrer"
         >
    {myValue}
  </Link>

or with MUI sx props

  <Link
    href={`https://www.example.com/?symbol=${myValue}`}
    target="_blank"
    rel="noopener noreferrer"
    sx={{
      color: '#42a5f5'
      textDecoration: 'none',
      display: 'inline',
      fontWeight: 'light'
      mx: 0.5,
      fontSize: '0.80rem',
    }}
  >
    {value.toUpperCase()}
  </Link>
Java bee
  • 2,522
  • 1
  • 12
  • 25
-2

What worked for me is this:

<Link to="/" style={{boxShadow: "none"}}>
Alex Mireles
  • 121
  • 1
  • 7
-2

Just add this to your css file or styles to remove any link causing underline!

a:-webkit-any-link{text-decoration: none;}

ZedanSaheer
  • 34
  • 1
  • 3
  • 1
    Please note that this rule is only intended for WebKit browsers. Here's the rule's [documentation page](https://developer.mozilla.org/en-US/docs/Web/CSS/:any-link). – soufiane yakoubi Aug 25 '21 at 15:04
-2
<Link 
   _hover={{
      textDecoration: "none"
   }}
   >
   Logo
</Link>