👀 Live Preview
Hover the box — style changes on mouseover, resets on mouseout:
State: idle

The onmouseover attribute is an inline event handler for the mouseover event. It runs when the pointer enters an element — or moves from a parent onto a child inside it (because mouseover bubbles). Use it to highlight items, show tooltips, or reveal extra info on hover. Pair it with onmouseout to reset styles when the user leaves. For simple color changes, CSS :hover is often enough. For nested menus, consider mouseenter instead to avoid extra events on child moves.
Inline JS.
Pointer enters.
Child moves.
Hover pair.
Preferred way.
Key difference.
onmouseover AttributeThe primary purpose of onmouseover is to run code when the user moves the pointer over an element. Typical uses include applying a highlight, showing a tooltip, previewing an image, or updating a status label. It is the natural partner to onmouseout (pointer leaves).
Because mouseover 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 mouseenter when they only care about entering the whole box.
Use CSS :hover for simple styling. Use onmouseover when JavaScript must run — tooltips, counters, or dynamic content.
Set onmouseover on any element that should respond when the pointer enters:
<button type="button"
onmouseover="highlight(this)"
onmouseout="resetStyle(this)">
Hover me
</button>
<div onmouseover="showTip(event)">…</div>mouseover event fires.div, button, a, etc.this to style the element that received hover.event for relatedTarget (element pointer came from).onmouseout to remove hover effects.el.addEventListener("mouseover", handler).The onmouseover attribute accepts a string of JavaScript code:
onmouseover="highlight(this)" — Style the hovered element.onmouseover="showTip(event)" — Use the event object.box.onmouseover = () => { … } — property assignment.function highlight(el) {
el.style.backgroundColor = "#dbeafe";
el.style.borderColor = "#3b82f6";
}
function resetStyle(el) {
el.style.backgroundColor = "";
el.style.borderColor = "";
}| Property / event | When it fires | Notes |
|---|---|---|
mouseover | Pointer enters element | Bubbles; fires on child moves |
mouseout | Pointer leaves | Partner event |
mouseenter | Enters boundary | Does not bubble from children |
mousemove | While moving over | Continuous, not enter-only |
| Handler attribute | onmouseover | Inline on element |
| Target | Supported? | Notes |
|---|---|---|
<button>, <a> | Yes | Hover feedback |
<div>, <span> | Yes | Cards, tooltips |
<img> | Yes | Captions, previews |
<li> in menus | Yes | Consider mouseenter |
| Simple color change | CSS instead | Use :hover when possible |
onmouseover vs onmouseenter vs onmousemove| Handler | Bubbles? | Typical use |
|---|---|---|
onmouseover | Yes | Pointer entered (incl. to child) |
onmouseenter | No | Clean enter on parent |
onmousemove | Yes | Track every move inside |
onmouseout | Yes | Reset on leave |
Highlight on enter with onmouseout reset, addEventListener status text, and show a tooltip on hover.
Hover the box — style changes on mouseover, resets on mouseout:
State: idle
Change background when the pointer enters:
<div class="hover-div"
onmouseover="changeColor(this)"
onmouseout="resetColor(this)">
Hover over me
</div>
<script>
function changeColor(el) {
el.style.backgroundColor = "lightseagreen";
}
function resetColor(el) {
el.style.backgroundColor = "lightblue";
}
</script>onmouseover applies the effect when the pointer enters. onmouseout restores the default when it leaves.
Attach with addEventListener:
<p id="dynamicElement">
Hover over this text
</p>
<script>
document.getElementById("dynamicElement").addEventListener("mouseover", function () {
document.getElementById("log").textContent = "Mouseover event occurred!";
});
</script>Register once in script. The handler runs each time the pointer enters the element.
Reveal helper text when the user hovers a label:
trigger.addEventListener("mouseover", () => {
tooltip.hidden = false;
status.textContent = "Tooltip visible";
});
trigger.addEventListener("mouseout", () => {
tooltip.hidden = true;
});mouseover shows the tip; mouseout hides it. Production tooltips also need keyboard focus support.
focus and hide on blur for keyboard users.Outside element.
Crosses boundary.
onmouseover runs.
Highlight, tooltip.
The mouseover event and onmouseover handler are supported in all browsers, including legacy Internet Explorer.
onmouseover supportEvery major browser fires mouseover when the pointer enters elements.
Bottom line: Reliable for hover enter logic in any browser environment.
onmouseover with onmouseoutthis to style the hovered element directly:hover for simple visual changesmouseenter when nested children cause flickeraddEventListener("mouseover", …) in productionalert() on every hover:hover and JS colors without a planThe onmouseover attribute runs JavaScript when the pointer enters an element — ideal for highlights, tooltips, and hover feedback.
Pair it with onmouseout, prefer CSS for simple styling, and use mouseenter when nested children cause extra events.
onmouseoverBookmark these before building hover UI.
Hover start.
EventChild moves.
GotchaHover pair.
PatternSimple style.
AltCleaner enter.
Comparemouseover event fires — when the pointer enters an element or moves onto a child inside it.mouseover bubbles and fires when moving to children. mouseenter fires only when entering the element boundary.:hover handles both automatically.addEventListener("mouseover", handler) or mouseenter is preferred for production code.Practice the onmouseover attribute with hover highlight examples in the Try It editor.
5 people found this page helpful