How to set cursor style that indicates any direction scroll using CSS ?


Before moving onto the topic, let us try to understand the importance of cursor as an UI element. We know that the very first thing a user interacts with in a website is mostly the cursor, this is why manipulating the cursor style and effects is the most important for designing, as it greatly contributes to the overall user experience.

The cursor property is used to set the mouse cursor to appear whenever we hover over an element by pointing over it. It is applied to all the elements and is inheritable, meaning the child element will also have the same cursor as that of its parent element.

  • The computed values can be absolute or as specified depending on whether or not we are using urls in the property values. It has a discrete animation style.

  • This is specified by providing one or more comma separated urls for cursor images, but it is mandatory to have a fallback keyword value.

  • While styling, the browsers tries to load the images specified by the urls and if there is some error in loading the images the cursor then fallbacks and uses the keyword values.

Optionally we can also provide two numbers separated by spaces after the urls that defines the cursor hotspot’s x and y coordinates in relation to the top-left corner of the image.

Now let us briefly understand each of these values.

  • url(s) − It is an optional value that contains the source location of the image file that we want as the cursor. We can also provide more than one image as cursor by specifying a comma separated list of urls in case we have more than one cursor to use for fallback.

  • X and Y − These are also optional numeric values pointing to the exact location within the cursor that is being pointed at; the cursor hotspot. The numbers are actually referring to the pixel size and they are clamped within the confines of the cursor image and are relative to the top left corner of the image, which corresponds to "0 0".

  • Keyword values − It is a must have value for this property which specifies the type of cursor we are going to use or the cursor we will be using in case out image fails to load. There are a number of keyword values available for us to use, For example, auto, default, none, help, wait, crosshair, and many more.

Using the all-scroll properties

At this point, we know about the cursor property and its possible values. So, the question that arises is, what keyword value is going to set the cursor to any direction scroll. The answer to the question is, “all-scroll”. The all-scroll property value is used for the elements that we wish to scroll in any direction that we desire.

Example

An example of using the all-scroll keyword value to set the cursor style to any direction scroll is given below.

<!DOCTYPE html>
<html>
<head>
   <style>
      #all-scrollValue {
         color: brown;
         cursor: all-scroll;
      }
   </style>
</head>
<body>
   <h1>Using all scroll property to change cursor</h1>
   <h2 id="all-scrollValue">
      Hover over this text to see the cursor change to any-direction scroll.
   </h2>
</body>
</html>

Using the move property value

As per the bug 275174, the all-cursor property value works as the same as the move property value in Windows. So the windows users can also use the move property value to set any-direction scroll.

Example

The example of using the move property value for any-direction scroll cursor is given below.

<!DOCTYPE html>
<html>
<head>
   <style>
      #all-scrollValue {
         color: brown;
         cursor: all-scroll;
      }
      #movePropertyValue {
         color: crimson;
         cursor: move;
      }
   </style>
</head>
<body>
   <h1>Using all scroll property to change cursor</h1>
   <h2 id="all-scrollValue">
      Hover over this text to see the cursor change to any-direction scroll.
   </h2>
   <h2 id="movePropertyValue">
      Hover over to see the effect of move property value.
   </h2>
</body>
</html>

Conclusion

To conclude, CSS makes it easy to set a custom cursor style that indicates any direction scroll. By using the CSS property of "cursor", you can customize the appearance of your website's cursor and make sure it accurately reflects user behavior, such as scrolling in any direction. With just a few lines of code, you can create an intuitive experience for your users.

Updated on: 27-Feb-2023

348 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements