CSS
Web Development
HTML
Inline Styling
Coding Tips

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:

html
<a href="/about" style="color: blue; text-decoration: none;">About</a>

This is invalid and every browser ignores it:

html
<!-- WRONG: browsers ignore this entirely -->
<a href="/about" style="a:hover { color: red; }">About</a>

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

html
1<style>
2  .link-primary:hover {
3    color: crimson;
4    text-decoration: underline;
5  }
6</style>
7
8<a href="/about" class="link-primary">About</a>

External stylesheet

css
1/* styles.css */
2.link-primary:hover {
3  color: crimson;
4  text-decoration: underline;
5}
html
<link rel="stylesheet" href="styles.css">
<a href="/about" class="link-primary">About</a>

Hover on a single element

To target exactly one link, use an id selector:

html
1<style>
2  #special-link:hover {
3    color: green;
4    font-weight: bold;
5  }
6</style>
7
8<a href="/special" id="special-link">Special Link</a>

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:

html
1<a href="/about"
2   style="color: black; text-decoration: none; transition: color 0.2s;"
3   onmouseover="this.style.color='red'; this.style.textDecoration='underline'"
4   onmouseout="this.style.color='black'; this.style.textDecoration='none'">
5  Hover over me
6</a>

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:

AspectCSS :hoverJavaScript onmouseover/onmouseout
SyntaxClean, declarativeVerbose, mixed with markup
MaintainabilityEasy to change in one placeScattered across every element
PerformanceHandled by browser's CSS engineTriggers JS execution per event
AccessibilityWorks with keyboard focus via :focusMouse-only unless you also handle onfocus/onblur
Touch devicesBrowser handles hover heuristicsRequires touchstart/touchend handlers
CSP compatibilityAlways worksMay 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)

css
1/* Button.module.css */
2.link {
3  color: black;
4  text-decoration: none;
5}
6
7.link:hover {
8  color: crimson;
9  text-decoration: underline;
10}
jsx
1import styles from './Button.module.css';
2
3function Button() {
4  return <a href="/about" className={styles.link}>About</a>;
5}

Tailwind hover: classes

html
<a href="/about" class="text-black no-underline hover:text-red-600 hover:underline">
  About
</a>

Tailwind's hover: prefix compiles to a real CSS :hover rule, so it behaves like native CSS hover.

styled-components

jsx
1import styled from 'styled-components';
2
3const StyledLink = styled.a`
4  color: black;
5  text-decoration: none;
6
7  &:hover {
8    color: crimson;
9    text-decoration: underline;
10  }
11`;
12
13function Nav() {
14  return <StyledLink href="/about">About</StyledLink>;
15}

React inline style with state

If you need hover with no CSS file at all, React state is the cleanest JavaScript approach:

jsx
1import { useState } from 'react';
2
3function HoverLink({ href, children }) {
4  const [hovered, setHovered] = useState(false);
5
6  return (
7    <a
8      href={href}
9      style={{
10        color: hovered ? 'crimson' : 'black',
11        textDecoration: hovered ? 'underline' : 'none',
12        transition: 'color 0.2s',
13      }}
14      onMouseEnter={() => setHovered(true)}
15      onMouseLeave={() => setHovered(false)}
16    >
17      {children}
18    </a>
19  );
20}

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 ClientSupports <style> blockSupports :hover in <style>
Apple MailYesYes
Gmail (web)Yes (limited)No
Outlook (desktop)PartialNo
Yahoo MailYesYes
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>.

html
1<!-- Email-safe approach: style the default state inline, add hover in <style> as enhancement -->
2<style>
3  .email-link:hover {
4    color: #cc0000 !important;
5  }
6</style>
7
8<a href="https://example.com" class="email-link" style="color: #333333; text-decoration: underline;">
9  Read more
10</a>

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:

css
1.link-primary:hover,
2.link-primary:focus {
3  color: crimson;
4  text-decoration: underline;
5  outline: 2px solid crimson;
6  outline-offset: 2px;
7}

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.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Data 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.

Practice algorithms