CodeGym /Courses /Frontend SELF EN /Custom Cursors

Custom Cursors

Frontend SELF EN
Level 19 , Lesson 4
Available

10.1 The cursor Property

CSS lets you change the look of the cursor when hovering over elements using the cursor property. This gives you the chance to set different cursor types, making your web pages more interactive and visually appealing.

The cursor property defines what the mouse cursor looks like when it's over an element. Values can be either predefined or custom images.

Syntax:

    
      element {
         cursor: value;
      }
    
  

Values:

The cursor property supports many values, which can be divided into predefined and custom.

Predefined values:

  • default: the default cursor
  • pointer: a hand-like cursor, usually used for links
  • text: a text cursor (I-shaped), usually for text fields
  • move: a moving cursor
  • wait: an hourglass or waiting indicator cursor
  • help: a question mark cursor
  • crosshair: a crosshair cursor
  • not-allowed: a forbidden sign cursor (circle with a slash)
  • grab and grabbing: dragging cursors

Example of using predefined values:

CSS
    
      .default-cursor {
        cursor: default;
        padding: 10px;
        border: 1px solid black;
      }

      .pointer-cursor {
        cursor: pointer;
        padding: 10px;
        border: 1px solid black;
      }

      .text-cursor {
        cursor: text;
        padding: 10px;
        border: 1px solid black;
      }
    
  
HTML
    
      <div class="default-cursor">Default cursor</div>
      <div class="pointer-cursor">Pointer cursor</div>
      <div class="text-cursor">Text cursor</div>
    
  

10.2 Custom Values (Custom Cursors)

The cursor property also supports using custom images as cursors. You do this with the url value.

Syntax:

    
      element {
        cursor: url("custom-cursor.png"), fallback;
      }
    
  

Where:

  • url('custom-cursor.png'): specifies the path to the cursor image
  • fallback: a predefined value used if the custom image can't be loaded

Example of using custom cursors:

CSS
    
      .custom-cursor {
        cursor: url('https://via.placeholder.com/32'), auto;
        padding: 10px;
        border: 1px solid black;
      }
    
  
HTML
    
      <body>
        <div class="custom-cursor">Custom cursor</div>
      </body>
    
  

Specifications and Limitations of Custom Cursors:

  1. Image formats: supports .cur, .png, .jpg, .gif formats.
  2. Image sizes: it's recommended to use 32x32 pixels or smaller for better performance and compatibility.
  3. Click point coordinates: You can set the click point (hotspot) coordinates in the cursor image. The default is the top-left corner (0,0).

10.3 Details of Using Custom Cursors

Image Formats

Custom cursors can be in various formats like PNG, GIF, JPG, and SVG. It's recommended to use formats with transparency support (like PNG) to ensure it looks right on any background.

Image Sizes

Cursor images shouldn't be too large. Recommended sizes are 32x32 pixels or smaller. Larger images might not display correctly in some browsers.

Specifying Focus Coordinates

You can specify the focus (hotspot) coordinates of the cursor using additional values after the image URL.

Syntax

    
      element {
        cursor: url("custom-cursor.png") x y, fallback;
      }
    
  

Where:

  • x and y: focus coordinates relative to the top-left corner of the image.

Example of using focus coordinates:

CSS
    
      .custom-cursor-focus {
        cursor: url('https://via.placeholder.com/32') 16 16, auto;
        padding: 10px;
        border: 1px solid black;
      }
    
  
HTML
    
      <body>
        <div class="custom-cursor-focus">Custom cursor with focus</div>
      </body>
    
  

Explanation of the Code:

  • .custom-cursor-focus: sets a custom cursor with focus coordinates (16, 16), making the center of the image the hot spot

Practical Use:

  1. Improving UI/UX: using custom cursors can greatly enhance the user interface, making it more intuitive and aesthetically pleasing.
  2. Interactive Elements: custom cursors can be applied to interactive elements like buttons, links, icons, etc.
  3. Games and Applications: in games and web apps, custom cursors help create a unique style and atmosphere.
1
Task
Frontend SELF EN, level 19, lesson 4
Locked
Predefined Cursors
Predefined Cursors
1
Task
Frontend SELF EN, level 19, lesson 4
Locked
Custom Cursor
Custom Cursor
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION