Can you use :hover in inline CSS? (a:hover in a style attribute)
Codemia Knowledge Hub ·
You cannot write a:hover in inline CSS. The style attribute only accepts CSS property declarations, not selectors, pseudo-classes or pseudo-elements. For hover behavior, use a <style> block, an external stylesheet, or, where stylesheets are unavailable, a JavaScript event handler.
Why the style attribute cannot express :hover
The style attribute takes a sequence of property-value pairs. It does not accept rules, which need a selector, and :hover is a pseudo-class selector.
This is valid inline CSS:
This is invalid and every browser ignores it:
The browser shows no error. It silently discards the invalid syntax, so the bug can go unnoticed for a long time.
Put the :hover rule in a style block or stylesheet
Style block in the page
External stylesheet
Hover on a single element
To target exactly one link, use an id selector:
JavaScript fallback when only inline attributes are allowed
Email templates, some CMS widgets and embedded HTML editors may prohibit <style> blocks. There, event handlers are a workaround:
Always pair onmouseover with onmouseout. Without the reset, the element stays in its hovered state after the first interaction. The approach also has real costs:
| Aspect | CSS :hover | JavaScript onmouseover/onmouseout |
| Syntax | Clean, declarative | Verbose, mixed with markup |
| Maintainability | Easy to change in one place | Scattered across every element |
| Performance | Handled by browser's CSS engine | Triggers JS execution per event |
| Accessibility | Works with keyboard focus via :focus | Mouse-only unless you also handle onfocus/onblur |
| Touch devices | Browser handles hover heuristics | Requires touchstart/touchend handlers |
| CSP compatibility | Always works | May be blocked by strict Content Security Policy |
Hover in React, Vue and other frameworks
Where inline styles are common, these are better than raw onmouseover handlers.
CSS Modules (React)
Tailwind hover: classes
Tailwind's hover: prefix compiles to a real CSS :hover rule, so it behaves like native CSS hover.
styled-components
React inline style with state
If you need hover with no CSS file at all, React state is the cleanest JavaScript approach:
Hover in HTML email
Email is the most common reason people ask this. Gmail, Outlook and Yahoo Mail strip <style> blocks or heavily limit CSS, so inline styles are the only reliable way to control appearance. Email clients also strip all JavaScript, so onmouseover and onmouseout do nothing there.
:hover support across clients is inconsistent:
| Email Client | Supports <style> block | Supports :hover in <style> |
| Apple Mail | Yes | Yes |
| Gmail (web) | Yes (limited) | No |
| Outlook (desktop) | Partial | No |
| Yahoo Mail | Yes | Yes |
| Outlook.com (web) | Yes (limited) | No |
Design the email so it works without hover. If you add hover effects, treat them as progressive enhancement: style the default state inline and add the hover rule in <style>.
Clients that strip the <style> block still render the link with its inline default styling.
Pair :hover with :focus
Keyboard and screen reader users navigate with Tab, not a mouse. Give them the same visual feedback:
On touch devices, :hover is unpredictable. Some mobile browsers trigger hover on the first tap and the click on the second; others ignore hover entirely. Never make essential functionality, such as a dropdown menu or tooltip, depend only on hover.
Related reading
- How can I write an iPhone app entirely in JavaScript without making it just a web app?
- How can send PDF attachment in Node aws-sdk sendRawEmail function?
- How can you check for a
- How can you encode/decode a string to Base64 in JavaScript?
- How do I add two numbers in JavaScript without using or - operators?
- How do I call an asynchronous node.js function from within a GraphQL resolver requiring a return statement?
- How do I check for null values in JavaScript?
- How do I clone a range of array elements to a new array?
System design interview problems on this topic
- Design Google Doc: solution and walkthrough
- Design Facebook Messenger: solution and walkthrough
- Design Typeahead Suggestion: solution and walkthrough
- Design an Online Presence Indicator Service: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.