HTML onmouseout Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

The onmouseout attribute is an inline event handler for the mouseout event. It runs when the pointer leaves an element — or moves from the element onto a child inside it (because mouseout bubbles). Use it to hide tooltips, reset hover styles, or close menus when the user moves away. Pair it with onmouseover for enter/leave hover effects. For many hover menus, mouseleave is easier because it does not fire when moving between children. Read event.relatedTarget to see where the pointer went.

What You’ll Learn

01

Event handler

Inline JS.

02

mouseout

Pointer leaves.

03

Bubbles

Child moves.

04

relatedTarget

Where to.

05

addEventListener

Preferred way.

06

vs mouseleave

Key difference.

Purpose of onmouseout Attribute

The primary purpose of onmouseout is to react when the user moves the pointer away from an element. Typical uses include resetting a highlight color, hiding a tooltip, closing a dropdown, or stopping a hover animation. It is the natural partner to onmouseover (pointer enters).

Because mouseout bubbles, a handler on a parent can fire when the pointer moves from the parent into a child. That can cause flicker in nested hover UIs — many developers prefer mouseleave instead when they only care about leaving the whole box.

💡
Pair enter with leave

Use onmouseover to apply a hover style and onmouseout to remove it. CSS :hover often replaces both for simple styling.

📝 Syntax

Set onmouseout on any element that should respond when the pointer leaves:

onmouseout.html
<button type="button"
  onmouseover="highlight(this)"
  onmouseout="resetStyle(this)">
  Hover me
</button>

<div onmouseout="hideTooltip(event)"></div>

Syntax Rules

  • Value is JavaScript executed when the mouseout event fires.
  • Works on most elements: div, button, a, etc.
  • Pass event for relatedTarget (element pointer moved to).
  • Pass this to reset the element that lost hover.
  • The event bubbles to parent elements.
  • Modern alternative: el.addEventListener("mouseout", handler).
  • Consider mouseleave when child elements cause unwanted mouseout events.

💎 Values

The onmouseout attribute accepts a string of JavaScript code:

  • onmouseout="resetStyle(this)" — Reset the element that fired the event.
  • onmouseout="handleLeave(event)" — Inspect event.relatedTarget.
  • JavaScript: box.onmouseout = () => { … } — property assignment.
relatedTarget.html
panel.addEventListener("mouseout", (event) => {
  // Element the pointer moved to
  const to = event.relatedTarget;
  if (!panel.contains(to)) {
    tooltip.hidden = true;
  }
});

⚡ Quick Reference

Property / eventWhen it firesNotes
mouseoutPointer leaves elementBubbles; fires on child moves
mouseoverPointer entersPartner event
mouseleaveLeaves element boundaryDoes not bubble from children
event.relatedTargetIn handlerElement pointer moved to
Handler attributeonmouseoutInline on element

Applicable Elements

TargetSupported?Notes
<button>, <a>YesHover feedback
<div>, <span>YesTooltips, cards
<img>YesImage captions
<li> in menusYesConsider mouseleave
Simple styling onlyCSS insteadUse :hover when possible

onmouseout vs onmouseleave vs onmouseover

HandlerBubbles?Typical use
onmouseoverYesPointer entered
onmouseoutYesPointer left (incl. to child)
onmouseenterNoClean enter on parent
onmouseleaveNoClean leave from parent

Examples Gallery

Reset hover styles on leave, addEventListener with status text, and hide a tooltip when the pointer exits.

👀 Live Preview

Hover the box, then move away — style resets on mouseout:

Hover me, then leave

State: idle

Example — onmouseover + onmouseout pair

Apply highlight on enter, reset on leave:

button-onmouseout.html
<button type="button"
  onmouseover="highlight(this)"
  onmouseout="resetStyle(this)">
  Hover me
</button>

<script>
  function highlight(el) {
    el.style.backgroundColor = "#dbeafe";
  }
  function resetStyle(el) {
    el.style.backgroundColor = "";
  }
</script>
Try It Yourself

How It Works

onmouseover applies the effect; onmouseout removes it when the user moves away.

Dynamic Values with JavaScript

Attach with addEventListener:

dynamic-onmouseout.html
<div id="dynamicElement">Hover over me</div>

<script>
  document.getElementById("dynamicElement").addEventListener("mouseout", function () {
    document.getElementById("log").textContent = "Mouse left the box";
  });
</script>
Try It Yourself

How It Works

Register once in script. The handler runs when the pointer exits the element.

Example — Hide tooltip on mouseout

Check relatedTarget so the tooltip stays when moving to the tooltip itself:

tooltip-mouseout.html
wrap.addEventListener("mouseout", (event) => {
  const to = event.relatedTarget;
  if (!wrap.contains(to)) {
    tooltip.hidden = true;
  }
});
Try It Yourself

How It Works

relatedTarget tells you where the pointer went. Use contains() to avoid hiding too early.

♿ Accessibility

  • Do not hide critical info on mouseout only — Keyboard and touch users may never trigger mouseout.
  • Prefer CSS :hover for simple visual hover when no JS logic is needed.
  • Ensure focusable controls — Buttons and links work with keyboard; div hovers do not.
  • Tooltips need keyboard access — Use aria-describedby and focus events, not mouse only.
  • Avoid hover-only menus — Provide click or keyboard paths to open navigation.

🧠 How onmouseout Works

1

Pointer on element

User was hovering.

Hover
2

Pointer leaves

Or moves to child.

Exit
3

mouseout fires

onmouseout runs.

Event
=

Reset UI

Hide, unhighlight.

Browser Support

The mouseout event and onmouseout handler are supported in all browsers, including legacy Internet Explorer.

Mouse Events · Fully supported

Universal onmouseout support

Every major browser fires mouseout when the pointer leaves elements.

100% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer Fully supported
Full support
Opera Fully supported
Full support
onmouseout attribute 100% supported

Bottom line: Reliable for hover leave logic in any browser environment.

💡 Best Practices

✅ Do

  • Pair onmouseover with onmouseout for hover UX
  • Use mouseleave when nested children cause flicker
  • Check event.relatedTarget for tooltip logic
  • Use CSS :hover for simple color changes
  • Prefer addEventListener("mouseout", …) in production

❌ Don’t

  • Rely on mouseout alone without an enter handler
  • Use alert() for leave feedback
  • Hide essential content only reachable via hover
  • Confuse mouseout with mouseleave — bubbling differs
  • Forget keyboard and touch users

Conclusion

The onmouseout attribute runs JavaScript when the pointer leaves an element — ideal for resetting hover styles and hiding tooltips.

Pair it with onmouseover, know when to use mouseleave instead, and prefer CSS for simple hover styling.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onmouseout

Bookmark these before building hover UI.

5
Core concepts
🔄 02

Bubbles

Child moves.

Gotcha
03

+ mouseover

Hover pair.

Pattern
🎯 04

relatedTarget

Where to.

API
05

mouseleave

Alt event.

Compare

❓ Frequently Asked Questions

It runs JavaScript when the mouseout event fires — when the pointer leaves an element or moves onto a child inside it.
mouseout bubbles and fires when moving to children. mouseleave fires only when leaving the element boundary entirely.
Yes for JS hover effects — enter applies style, leave removes it. Simple styling can use CSS :hover alone.
The element the pointer moved to. Useful to avoid hiding tooltips when moving between nested elements.
addEventListener("mouseout", handler) or mouseleave is preferred for production code.
Yes — universal support including Internet Explorer.

Master mouse leave events

Practice the onmouseout attribute with hover reset examples in the Try It editor.

Try onmouseout demo →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful