How can I write 'a:hover' in inline CSS?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
You cannot write a:hover in inline CSS. The style attribute only accepts CSS property declarations. It does not support selectors, pseudo-classes, or pseudo-elements. To get hover behavior, use a <style> block, an external stylesheet, or a JavaScript event handler as a fallback in environments where stylesheets are unavailable.
Why Inline Styles Cannot Express :hover
The HTML style attribute accepts a sequence of CSS declarations (property-value pairs). It does not accept rules, which require a selector. :hover is a pseudo-class selector, so it belongs in a stylesheet context, not inside an attribute.
This is valid inline CSS:
This is invalid and will be ignored by every browser:
The browser does not throw a visible error. It silently discards the invalid syntax, which makes debugging confusing if you do not know the rule.
The Correct Solution: Use a Style Block or Stylesheet
The standard way to add :hover behavior is through CSS rules in a <style> element or an external stylesheet.
Inline style block (scoped to the page)
External stylesheet
Targeting a single element
If you want hover on exactly one link, use an id selector:
JavaScript Fallback for Inline-Only Environments
Some environments restrict you to inline attributes only. Email HTML templates, certain CMS widgets, and embedded HTML editors sometimes prohibit <style> blocks. In these cases, JavaScript event handlers provide a workaround.
This approach works but has significant downsides:
| 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 |
Modern CSS Alternatives to Inline Hover
If you are working in a framework where inline styles are common (React, Vue), consider these approaches instead of raw onmouseover handlers.
CSS Modules (React)
Tailwind CSS utility classes
Tailwind's hover: prefix compiles to a real CSS :hover rule under the hood, so it has all the benefits of native CSS hover.
CSS-in-JS (styled-components)
React inline style with state
If you genuinely need hover behavior without any CSS file, React state is the cleanest JavaScript approach:
Email HTML: The Special Case
Email HTML is the most common reason developers ask this question. Email clients like Gmail, Outlook, and Yahoo Mail strip <style> blocks or heavily limit which CSS properties are supported. Inline styles are the only reliable way to control appearance.
Unfortunately, :hover support in email 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 |
Given this fragmented support, the practical advice for email HTML is: do not rely on hover effects. Design the email so it works without any hover interaction. If you add hover effects, treat them as progressive enhancement that some clients will show and others will not.
Clients that strip the <style> block will still render the link with the inline default styling.
Accessibility and :focus
When you add :hover styles, always pair them with :focus for keyboard accessibility. Users navigating with Tab should see the same visual feedback as mouse users.
On touch devices, :hover behavior 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 depend solely on hover.
Common Pitfalls
Putting selector syntax inside the style attribute. Writing style="a:hover { color: red }" is silently ignored. The browser does not warn you, so the bug can go unnoticed for a long time.
Using onmouseover without onmouseout. If you set a style on hover but never reset it, the element stays in its hovered state permanently after the first interaction.
Relying on hover for essential UI behavior. Touch devices may never trigger hover events. If a dropdown menu or tooltip only appears on hover, it is inaccessible to a significant portion of users.
Forgetting :focus alongside :hover. Keyboard users and screen reader users navigate with Tab, not a mouse. Without :focus styles, they get no visual feedback when they reach your interactive element.
Using JavaScript hover in emails. Email clients strip all JavaScript. The onmouseover and onmouseout attributes will do nothing in Gmail, Outlook, or any other email client.
Summary
- Inline CSS (
styleattribute) does not support pseudo-classes like:hover. It only accepts property declarations. - The correct solution is a
<style>block or external stylesheet with a class-based:hoverrule. - JavaScript
onmouseover/onmouseouthandlers are a fallback for environments where stylesheets are unavailable, but they are harder to maintain and less accessible. - In modern frameworks, use CSS Modules, Tailwind's
hover:prefix, or CSS-in-JS libraries instead of raw inline hover logic. - Email HTML has limited and inconsistent
:hoversupport. Treat hover effects in emails as progressive enhancement, not a requirement. - Always pair
:hoverwith:focusfor keyboard accessibility.
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 check if an array includes a value in JavaScript?
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.