Last updated: Apr 7, 2024
Reading time·5 min
To style an element on hover using an external CSS file:
:hover
pseudo-class in a CSS file..css
file into your React component.import './App.css'; const App = () => { return ( <div> <div className="box"> <p>bobbyhadz.com</p> </div> </div> ); }; export default App;
And here is the related App.css
file.
.box { width: 120px; height: 60px; background-color: yellow; color: black; padding: 12px; } .box:hover { background-color: black; color: white; }
Make sure to import the App.css
file as shown in the code sample.
We used the :hover pseudo-class to add styling to an element when the user hovers over the element.
<div className="box"> <p>bobbyhadz.com</p> </div>
We added the class to a div
, so every time the user hovers over the div
, the
styles get applied.
If you need to show an element or text on hover, click on the link and follow the instructions.
To add inline CSS styles on hover in React:
onMouseEnter
and onMouseLeave
props on the element.import {useState} from 'react'; const App = () => { const [isHovering, setIsHovering] = useState(false); const handleMouseEnter = () => { setIsHovering(true); }; const handleMouseLeave = () => { setIsHovering(false); }; return ( <div> <div> <div style={{ width: '140px', height: '60px', padding: '20px', backgroundColor: isHovering ? 'green' : 'blue', color: isHovering ? 'white' : 'red', }} onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave} > bobbyhadz.com </div> </div> </div> ); }; export default App;
We used the useState hook to keep
track of the isHovering
state variable.
const [isHovering, setIsHovering] = useState(false);
The hook returns an array containing a value and a function that is used to set update the value.
We set the onMouseEnter
and onMouseLeave
props on a div
element.
The mouseenter event is triggered when the user's mouse is moved within the element.
Conversely, the mouseleave event is triggered when the user's mouse is moved out of the element.
Every time the user hovers over the div
, the handleMouseEnter
function is
invoked.
const handleMouseEnter = () => { setIsHovering(true); };
And every time they move their cursor out of the element, the handleMouseLeave
function is invoked.
const handleMouseLeave = () => { setIsHovering(false); };
We can then use the ternary operator to set inline styles on the element conditionally.
<div style={{ backgroundColor: isHovering ? 'green' : 'blue', color: isHovering ? 'white' : 'red', }} onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave} > bobbyhadz.com </div>
The ternary operator is very similar to an if/else
statement.
You can imagine that the value before the colon is the if
block and the value
after the colon is the else
block.
<div style={{ backgroundColor: isHovering ? 'green' : 'blue', color: isHovering ? 'white' : 'red', }} onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave} > bobbyhadz.com </div>
In other words, if the isHovering
variable stores a true
value, we set the
backgroundColor
property to green
, otherwise we set it to blue
.
If you only need to set a style when the user is hovering over the element, use
an empty string for the falsy
case.
<div style={{ backgroundColor: isHovering ? 'green' : '', color: isHovering ? 'white' : '', }} onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave} > bobbyhadz.com </div>
If the isHovering
variable is equal to true
, the backgroundColor
property
gets set to green
, otherwise, it remains the default of an empty string.
When the user hovers over the element:
handleMouseEnter
function is invoked.isHovering
state variable is set to true
.Conversely, when the user moves their cursor out of the element:
handleMouseLeave
function is invoked.isHovering
state variable is set to false
.I've also written a detailed guide on how to change the style on an element on click.
You can also conditionally style an element on hover using classes.
import './App.css'; import {useState} from 'react'; const App = () => { const [isHovering, setIsHovering] = useState(false); const handleMouseOver = () => { setIsHovering(true); }; const handleMouseOut = () => { setIsHovering(false); }; return ( <div> <div> <div className={isHovering ? 'bg-salmon' : ''} onMouseOver={handleMouseOver} onMouseOut={handleMouseOut} > Hover me </div> </div> </div> ); }; export default App;
And here is the App.css
file for the example.
.bg-salmon { background-color: salmon; color: white; }
We set the onMouseOver
prop on the div
element, so every time the user
hovers over the element, the handleMouseOver
function is invoked.
<div className={isHovering ? 'bg-salmon' : ''} onMouseOver={handleMouseOver} onMouseOut={handleMouseOut} > Hover me </div>
The mouseover
event is triggered when the user moves their cursor onto the
element or one of its child elements.
In our handleMouseOver
function, we simply set the isHovering
state variable
to true
.
const handleMouseOver = () => { setIsHovering(true); };
Conversely, in our handleMouseOut
function, we set the state
variable to
false
.
const handleMouseOut = () => { setIsHovering(false); };
The mouseout
event is triggered when the user's cursor is no longer contained
within the element or one of its children.
We used the ternary operator to conditionally add the class to the element.
<div className={isHovering ? 'bg-salmon' : ''} onMouseOver={handleMouseOver} onMouseOut={handleMouseOut} > Hover me </div>
The ternary operator is very similar to an if/else
statement.
If the expression to the left of the question mark is truthy, the operator returns the value to the left of the colon, otherwise, the value to the right of the colon is returned.
In other words, if the isHovering
variable stores a true
value, we add the
bg-salmon
class to the element, otherwise return an empty string.
You can imagine that the value before the colon is the if
block and the value
after the colon is the else
block.
If you need to add or remove a class on click, check out the following tutorial.
I also have an article on how to use a ref to change an element's style.
You can learn more about the related topics by checking out the following tutorials: