Search code examples
javascripthtmlcsscursorstyling

Custom Cursor using CSS styling - html/css - javascript


I'm trying to make a custom cursor without uploading an image.I want the cursor to be a little blue circle. Currently, I'm trying to set the cursor's css as an id. Then I would associate the built in CSS cursor styling with the new id:

.mainbody{
....
cursor:#id?

};

Is this possible? Am I even going about this correctly? All help appreciated.


Solution

  • If you want to have a custom CSS cursor, you need to add a div in your markup, style it, hide the default cursor and move it to the mouse coordinates :

    $(document).ready(function() {
      $(document).on('mousemove', function(e) {
        $('#cursor').css({
          left: e.pageX,
          top: e.pageY
        });
      })
    });
    html {
      cursor: none;
    }
    #cursor {
      height: 20px;
      width: 20px;
      border-radius: 20px;
      background-color: blue;
      position: absolute;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="cursor"></div>