HTML onmouseout Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onmouseout attribute is an inline event handler for the mouseout event. It runs when a pointing device moves so the cursor is no longer contained within the element or one of its children.

Pair it with onmouseover for enter/leave pairs. MDN notes that mouseleave is often clearer for “left this whole box,” because mouseout also fires when moving onto a child.

Baseline · Widely available.

Not deprecated and not experimental. Stable across modern browsers since about July 2015.

Child boundaries fire mouseout.

Moving from a parent into a child counts as leaving the parent for mouseout. Prefer mouseleave when that surprise is unwanted.

HTML Syntax

syntax.html
<element onmouseout=" script">

script is JavaScript that runs on leave. Pass event to read event.target and event.relatedTarget. Script alternative: element.addEventListener("mouseout", handler).

Attribute Values

The attribute value is a script. Common patterns:

Value / patternDescriptionTry it
handler()Run code when the pointer leaves the elementTry it
handler(event)Inspect target / relatedTarget (where the pointer went)Try it
With nested childrenSee why leaving a parent into a child still fires mouseoutTry it
addEventListener("mouseout")Preferred modern registration in appsTry it

Live Preview

Move into and out of the pad. Try the nested mode: moving onto the inner box fires mouseout on the outer pad even though you are still “inside” the outer visually.

demo

Leave the pad (or enter the inner box in nested mode) to see mouseout.

Hover area
Outer pad — move out

Status: waiting…

HTML Examples

Short study snippets. Use Try It Yourself for a live editor.

Example 1 — Update Status When Leaving

Show a message when the pointer leaves a simple pad.

HTML
<div
  id="pad"
  onmouseover="setStatus('Inside')"
  onmouseout="setStatus('Left')">
  Hover, then leave
</div>
<p id="status">Waiting…</p>

<script>
  function setStatus(text) {
    document.getElementById("status").textContent = text;
  }
</script>
Try It Yourself

How It Works

  1. Enter the pad — mouseover sets “Inside”.
  2. Leave the pad — mouseout sets “Left”.
  3. No children here — so leave means truly outside the box.
  4. Use for reset UI — hide a tip, clear a highlight, stop a hover preview.

Example 3 — Nested Children Surprise

Moving onto an inner box fires mouseout on the outer — even though you look “still inside.”

HTML
<div id="outer" onmouseout="logOut(event)">
  Outer
  <span id="inner">Inner</span>
</div>
<ul id="log"></ul>

<script>
  function logOut(e) {
    var li = document.createElement("li");
    li.textContent =
      "mouseout on " + (e.target.id || e.target.tagName) +
      " → " + (e.relatedTarget ? (e.relatedTarget.id || e.relatedTarget.tagName) : "null");
    document.getElementById("log").appendChild(li);
  }
</script>
Try It Yourself

How It Works

  1. Enter outer, then move onto Inner. The child covers the parent.
  2. mouseout fires because the pointer left the parent’s own area.
  3. This is why hover menus flicker if they hide on mouseout while moving toward a child.
  4. Fix: use mouseleave for whole-group leave, or ignore events when relatedTarget is still inside the parent.

Edge Cases & Pitfalls

Most bugs come from treating mouseout like “left the whole component.”

children

Entering a child counts as leaving the parent

MDN: mouseout fires when the cursor enters a child that obscures the parent. Prefer mouseleave for whole-box leave.

bubbling

mouseout bubbles

A leave from a nested element can bubble to ancestors. Check event.target if you only want the outer element.

a11y

Hover-only UI excludes keyboard users

Do not hide essential content only on mouse leave. Provide focus/click alternatives for the same information.

touch

Touch has no lasting hover

Phones may not deliver reliable mouseout after a tap. Do not rely on hover leave for critical mobile UX.

pair

Match with mouseover carefully

Enter/leave pairs using mouseover/mouseout both see child boundary noise. Enter/leave pairs using mouseenter/mouseleave usually do not.

Browser Support

onmouseout is an HTML event handler for the mouseout event in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

onmouseout

Widely available since about July 2015. Supported across modern browsers; understand child-boundary behavior before using it for hover menus.

98% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer Legacy support
Supported
onmouseout Widely supported

Bottom line: Safe to use. For “left this whole element,” prefer mouseleave; use mouseout when you intentionally care about child boundaries.

Key Takeaways

  • Role: pointer left the element (or moved onto a child that covers it).
  • vs mouseleave: mouseout reacts to children; mouseleave does not.
  • relatedTarget: the element receiving the pointer after leave.
  • Hover menus: child moves often need mouseleave, not mouseout.

One line: onmouseout means “left this element’s area” — including into its own children.

Frequently Asked Questions

It is an inline event handler that runs JavaScript when the mouseout event fires — the pointing device moves so the cursor is no longer contained within the element or one of its children.
mouseout bubbles and also fires when the pointer moves from a parent onto a child (the child covers the parent). mouseleave does not fire for moves into descendants — it fires when leaving the element as a whole. MDN often recommends mouseleave for “left this box” UI.
onmouseover runs when the pointer enters an element (including via children). onmouseout runs when the pointer leaves that element (including into a child or outside).
Because the child obscures the parent’s visible area, so the pointer is no longer “in” the parent alone. That is expected mouseout behavior.
On mouseout, relatedTarget is the element the pointer is entering (or null). On mouseover, it is the element being left. Useful for knowing where the pointer went.
No. The mouseout event is Baseline widely available. Prefer mouseleave when you only care about leaving the whole element and its children as a unit.

Next: onmouseover

Run script when the pointer enters an element.

onmouseover tutorial →

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.

8 people found this page helpful