👀 Live Preview
Hover the box, then move away — style resets on mouseout:
State: idle

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.
Inline JS.
Pointer leaves.
Child moves.
Where to.
Preferred way.
Key difference.
onmouseout AttributeThe 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.
Use onmouseover to apply a hover style and onmouseout to remove it. CSS :hover often replaces both for simple styling.
Set onmouseout on any element that should respond when the pointer leaves:
<button type="button"
onmouseover="highlight(this)"
onmouseout="resetStyle(this)">
Hover me
</button>
<div onmouseout="hideTooltip(event)">…</div>mouseout event fires.div, button, a, etc.event for relatedTarget (element pointer moved to).this to reset the element that lost hover.el.addEventListener("mouseout", handler).mouseleave when child elements cause unwanted mouseout events.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.box.onmouseout = () => { … } — property assignment.panel.addEventListener("mouseout", (event) => {
// Element the pointer moved to
const to = event.relatedTarget;
if (!panel.contains(to)) {
tooltip.hidden = true;
}
});| Property / event | When it fires | Notes |
|---|---|---|
mouseout | Pointer leaves element | Bubbles; fires on child moves |
mouseover | Pointer enters | Partner event |
mouseleave | Leaves element boundary | Does not bubble from children |
event.relatedTarget | In handler | Element pointer moved to |
| Handler attribute | onmouseout | Inline on element |
| Target | Supported? | Notes |
|---|---|---|
<button>, <a> | Yes | Hover feedback |
<div>, <span> | Yes | Tooltips, cards |
<img> | Yes | Image captions |
<li> in menus | Yes | Consider mouseleave |
| Simple styling only | CSS instead | Use :hover when possible |
onmouseout vs onmouseleave vs onmouseover| Handler | Bubbles? | Typical use |
|---|---|---|
onmouseover | Yes | Pointer entered |
onmouseout | Yes | Pointer left (incl. to child) |
onmouseenter | No | Clean enter on parent |
onmouseleave | No | Clean leave from parent |
Reset hover styles on leave, addEventListener with status text, and hide a tooltip when the pointer exits.
Hover the box, then move away — style resets on mouseout:
State: idle
Apply highlight on enter, reset on leave:
<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>onmouseover applies the effect; onmouseout removes it when the user moves away.
Attach with addEventListener:
<div id="dynamicElement">Hover over me</div>
<script>
document.getElementById("dynamicElement").addEventListener("mouseout", function () {
document.getElementById("log").textContent = "Mouse left the box";
});
</script>Register once in script. The handler runs when the pointer exits the element.
Check relatedTarget so the tooltip stays when moving to the tooltip itself:
wrap.addEventListener("mouseout", (event) => {
const to = event.relatedTarget;
if (!wrap.contains(to)) {
tooltip.hidden = true;
}
});relatedTarget tells you where the pointer went. Use contains() to avoid hiding too early.
aria-describedby and focus events, not mouse only.User was hovering.
Or moves to child.
onmouseout runs.
Hide, unhighlight.
The mouseout event and onmouseout handler are supported in all browsers, including legacy Internet Explorer.
onmouseout supportEvery major browser fires mouseout when the pointer leaves elements.
Bottom line: Reliable for hover leave logic in any browser environment.
onmouseover with onmouseout for hover UXmouseleave when nested children cause flickerevent.relatedTarget for tooltip logic:hover for simple color changesaddEventListener("mouseout", …) in productionalert() for leave feedbackThe 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.
onmouseoutBookmark these before building hover UI.
Reset UI.
EventChild moves.
GotchaHover pair.
PatternWhere to.
APIAlt event.
Comparemouseout 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.:hover alone.addEventListener("mouseout", handler) or mouseleave is preferred for production code.Practice the onmouseout attribute with hover reset examples in the Try It editor.
5 people found this page helpful