HTML onscroll Attribute

HTML onscroll Attribute: The HTML onscroll attribute is defined as it fires when an element’s scrollbar is being scrolled. The onscroll attribute is the part of the event attribute. HTML4 does not support this attribute. It was supported by HTML5.

HTML onscroll Attribute

This HTML onscroll attribute can be applied to all the HTML elements.

Syntax:<div onscroll=”myFunction()”>

Browser Support

The HTML onscroll attribute is supported by the following browsers:

  • Chrome
  • Firefox
  • Internet Explorer
  • Safari
  • Opera

Example: for <div> element

<!DOCTYPE html>
#myDIV {
  border: 1px solid black;
  width: 200px;
  height: 100px;
  overflow: scroll;
<p>Try the scrollbar in div.</p>

<div id="myDIV" onscroll="myFunction()">In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.

function myFunction() {
  document.getElementById("myDIV").style.color = "red";


HTML onscroll attribute