308

I quite like the inline CSS pattern in React and decided to use it.

However, you can't use the :hover and similar selectors. So what's the best way to implement highlight-on-hover while using inline CSS styles?

One suggestion from #reactjs is to have a Clickable component and use it like this:

<Clickable>
    <Link />
</Clickable>

The Clickable has a hovered state and passes it as props to the Link. However, the Clickable (the way I implemented it) wraps the Link in a div so that it can set onMouseEnter and onMouseLeave to it. This makes things a bit complicated though (e.g. span wrapped in a div behaves differently than span).

Is there a simpler way?

4
  • 3
    You're absolutely right - the only way to simulate :hover etc selectors with inline styles is to use onMouseEnter and onMouseLeave. Regarding the exact implementation of that - it's entirely up to you. To look at your specific example, why not make the <Clickable/> wrapper a span? Commented Feb 6, 2015 at 14:20
  • 5
    i would suggest using external style sheets along with ExtractText Webpack plugin , this will help you on longer run if you ever wish to ServerRender otherwise you can try Radium github.com/FormidableLabs/radium Commented Aug 17, 2016 at 14:05
  • Currently Styled Component is best solution to simulate all possibilities of css/scss in react. Commented Sep 25, 2017 at 9:51
  • Well, the C in CSS stands for cascading. When we go inline, we lose cascading features such as :hover Commented Feb 11, 2024 at 4:45

32 Answers 32

1
2
0

I'm not 100% sure if this is the answer, but its the trick i use to simulate the CSS :hover effect with colours and images inline.

`This works best with an image`

class TestHover extends React.PureComponent {
render() {
const landingImage = {     
"backgroundImage": "url(https://i.dailymail.co.uk/i/pix/2015/09/01/18/2BE1E88B00000578-3218613-image-m-5_1441127035222.jpg)",
"BackgroundColor": "Red", `this can be any color`
"minHeight": "100%",
"backgroundAttachment": "fixed",
"backgroundPosition": "center",
"backgroundRepeat": "no-repeat",
"backgroundSize": "cover", 
"opacity": "0.8", `the hove trick is here in the opcaity slightly see through gives the effect when the background color changes`
    }

  return (
    <aside className="menu">
        <div className="menu-item">
          <div style={landingImage}>SOME TEXT</div>
        </div>
    </aside>
      ); 
  }
}
ReactDOM.render(
    <TestHover />,
  document.getElementById("root")
);

CSS:

.menu {
top: 2.70em;
bottom: 0px;
width: 100%;
position: absolute;
}

.menu-item {
cursor: pointer;
height: 100%;
font-size: 2em;
line-height: 1.3em;
color: #000;
font-family: "Poppins";
font-style: italic;
font-weight: 800;
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
}

Before hover

.menu-item:nth-child(1) {
color: white;
background-color: #001b37;
} 

On hover

.menu-item:nth-child(1):hover {
color: green;
background-color: white;
}

Example: https://codepen.io/roryfn/pen/dxyYqj?editors=0011

Sign up to request clarification or add additional context in comments.

Comments

0

Many useful comments above, but a way which doesn't use any additional technology, honours the sprit of doing it all in html and has the virtue of simplicity would be the following

const FancyLink = () =>
<div className="FancyLink">
    <style>
        {`.FancyLink a:hover {
            color: red;
        }`}
    </style>
    <a href="http://google.com">a fancy link</a>
</div>

const App = () =>
    <div className="App">
        <a href="http://google.com">a default link</a>
        <FancyLink />
    </div>

export default App;

This does have a caveat: it's not valid html - styles ought to appear in a metadata context - but it does work on all browsers. If you need html that validates, you can wrap the script tag in eg Helmet.

Comments

1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.