HTML onmouseover Attribute

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

Introduction

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.

What You’ll Learn

01

Event handler

Inline JS.

02

mouseover

Pointer enters.

03

Bubbles

Child moves.

04

+ onmouseout

Hover pair.

05

addEventListener

Preferred way.

06

vs mouseenter

Key difference.

Purpose of onmouseover Attribute

The 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.

💡
CSS vs JavaScript hover

Use CSS :hover for simple styling. Use onmouseover when JavaScript must run — tooltips, counters, or dynamic content.

📝 Syntax

Set onmouseover on any element that should respond when the pointer enters:

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

<div onmouseover="showTip(event)"></div>

Syntax Rules

  • Value is JavaScript executed when the mouseover event fires.
  • Works on most elements: div, button, a, etc.
  • Pass this to style the element that received hover.
  • Pass event for relatedTarget (element pointer came from).
  • The event bubbles to parent elements.
  • Pair with onmouseout to remove hover effects.
  • Modern alternative: el.addEventListener("mouseover", handler).

💎 Values

The onmouseover attribute accepts a string of JavaScript code:

  • onmouseover="highlight(this)" — Style the hovered element.
  • onmouseover="showTip(event)" — Use the event object.
  • JavaScript: box.onmouseover = () => { … } — property assignment.
highlight-fn.html
function highlight(el) {
  el.style.backgroundColor = "#dbeafe";
  el.style.borderColor = "#3b82f6";
}

function resetStyle(el) {
  el.style.backgroundColor = "";
  el.style.borderColor = "";
}

⚡ Quick Reference

Property / eventWhen it firesNotes
mouseoverPointer enters elementBubbles; fires on child moves
mouseoutPointer leavesPartner event
mouseenterEnters boundaryDoes not bubble from children
mousemoveWhile moving overContinuous, not enter-only
Handler attributeonmouseoverInline on element

Applicable Elements

TargetSupported?Notes
<button>, <a>YesHover feedback
<div>, <span>YesCards, tooltips
<img>YesCaptions, previews
<li> in menusYesConsider mouseenter
Simple color changeCSS insteadUse :hover when possible

onmouseover vs onmouseenter vs onmousemove

HandlerBubbles?Typical use
onmouseoverYesPointer entered (incl. to child)
onmouseenterNoClean enter on parent
onmousemoveYesTrack every move inside
onmouseoutYesReset on leave

Examples Gallery

Highlight on enter with onmouseout reset, addEventListener status text, and show a tooltip on hover.

👀 Live Preview

Hover the box — style changes on mouseover, resets on mouseout:

Hover me

State: idle

Example — Inline onmouseover handler

Change background when the pointer enters:

div-onmouseover.html
<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>
Try It Yourself

How It Works

onmouseover applies the effect when the pointer enters. onmouseout restores the default when it leaves.

Dynamic Values with JavaScript

Attach with addEventListener:

dynamic-onmouseover.html
<p id="dynamicElement">
  Hover over this text
</p>

<script>
  document.getElementById("dynamicElement").addEventListener("mouseover", function () {
    document.getElementById("log").textContent = "Mouseover event occurred!";
  });
</script>
Try It Yourself

How It Works

Register once in script. The handler runs each time the pointer enters the element.

Example — Show tooltip on mouseover

Reveal helper text when the user hovers a label:

tooltip-mouseover.html
trigger.addEventListener("mouseover", () => {
  tooltip.hidden = false;
  status.textContent = "Tooltip visible";
});

trigger.addEventListener("mouseout", () => {
  tooltip.hidden = true;
});
Try It Yourself

How It Works

mouseover shows the tip; mouseout hides it. Production tooltips also need keyboard focus support.

♿ Accessibility

  • Do not hide critical info behind hover only — Keyboard and touch users may never trigger mouseover.
  • Prefer CSS :hover for decorative styling that does not convey essential information.
  • Tooltips need focus — Also show on focus and hide on blur for keyboard users.
  • Avoid hover-only menus — Provide click or keyboard paths to open navigation.
  • Ensure sufficient contrast on hover states for readability.

🧠 How onmouseover Works

1

Pointer approaches

Outside element.

Before
2

Pointer enters

Crosses boundary.

Enter
3

mouseover fires

onmouseover runs.

Event
=

Hover effect

Highlight, tooltip.

Browser Support

The mouseover event and onmouseover handler are supported in all browsers, including legacy Internet Explorer.

Mouse Events · Fully supported

Universal onmouseover support

Every major browser fires mouseover when the pointer enters 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
onmouseover attribute 100% supported

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

💡 Best Practices

✅ Do

  • Pair onmouseover with onmouseout
  • Pass this to style the hovered element directly
  • Use CSS :hover for simple visual changes
  • Use mouseenter when nested children cause flicker
  • Prefer addEventListener("mouseover", …) in production

❌ Don’t

  • Use alert() on every hover
  • Mix conflicting CSS :hover and JS colors without a plan
  • Put essential content only behind mouseover
  • Confuse mouseover with mousemove — enter vs continuous move
  • Forget keyboard and touch users

Conclusion

The 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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onmouseover

Bookmark these before building hover UI.

5
Core concepts
🔄 02

Bubbles

Child moves.

Gotcha
03

+ mouseout

Hover pair.

Pattern
🎨 04

CSS :hover

Simple style.

Alt
05

mouseenter

Cleaner enter.

Compare

❓ Frequently Asked Questions

It runs JavaScript when the mouseover 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.
CSS for simple styling. JavaScript when you need logic like tooltips or dynamic text.
Yes for JS hover effects — enter applies changes, leave removes them. Pure CSS :hover handles both automatically.
addEventListener("mouseover", handler) or mouseenter is preferred for production code.
Yes — universal support including Internet Explorer.

Master mouse enter events

Practice the onmouseover attribute with hover highlight examples in the Try It editor.

Try onmouseover 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