Last updated: Apr 6, 2024
Reading time·2 min
Use inline styles to remove the underline of a Link in React.
When the text-decoration property is set to none
, the underline of the link
is removed.
import {BrowserRouter as Router, Link} from 'react-router-dom'; export default function App() { return ( <Router> <div> <Link style={{textDecoration: 'none'}} to="/"> Home </Link> <br /> <a style={{textDecoration: 'none'}} href="google.com" target="_blank"> Google.com </a> </div> </Router> ); }
We used inline styles to set the
text-decoration
property of the Link to none
which removes the Link's underline.
The first set of curly braces in the inline style marks the beginning of an expression and the second set of curly braces is the object containing styles and values.
You can use the same approach to remove the underline of an anchor element
because under the hood the Link
component is really just an <a>
tag.
<a style={{textDecoration: 'none'}} href="google.com" target="_blank"> Google.com </a>
none
on the Link element and not on its children.If you need to change the color of a link, check out the following article.
An alternative solution is to remove the underline from all links by adding a global style in a CSS file.
a { text-decoration: none; }
Now, all we have to do is import the App.css
file for the style to be applied.
import {BrowserRouter as Router, Link} from 'react-router-dom'; // 👇️ Import your CSS file import './App.css'; export default function App() { return ( <Router> <div> <Link to="/">Home</Link> <br /> <a href="google.com" target="_blank"> Google.com </a> </div> </Router> ); }
This removes the underline of all links on the page without having to use the text-decoration property on each link.
a
tag, the link's underline will be removed.It's a best practice to import your
global CSS files in your index.js
file because
then they wouldn't only get loaded when a certain component is mounted.