CSS Cursors
By Codes With Pankaj
CSS Cursors allow you to change the cursor's appearance when it hovers over specific elements on a webpage. This can enhance user experience by providing visual feedback that indicates the element's functionality. In this tutorial, we will explore the various cursor options available in CSS, along with practical examples.
Table of Contents
What is a Cursor?
CSS Cursor Property
Types of Cursors
Basic Cursors
Link Cursors
Text Cursors
Resize Cursors
Custom Cursors
Practical Examples
Example 1: Default and Pointer Cursors
Example 2: Text and Crosshair Cursors
Example 3: Custom Cursor
Conclusion
1. What is a Cursor?
A cursor is a visual indicator on the screen that represents the user's point of interaction, typically controlled by a pointing device like a mouse or trackpad. The appearance of the cursor can change based on the context of the interaction, such as when hovering over a link, text, or an interactive element.
2. CSS Cursor Property
The cursor
property in CSS allows you to control the appearance of the cursor when it hovers over an element. This property can be applied to any HTML element to change the default cursor behavior.
Syntax:
Example:
In this example, when a user hovers over a button, the cursor changes to a pointer, indicating that the button is clickable.
3. Types of Cursors
CSS provides a variety of cursor types that can be used depending on the context of the element.
a. Basic Cursors
Basic cursors are commonly used to indicate different types of interactions. Here are a few examples:
default: The default arrow cursor.
pointer: A hand cursor, typically used for links or buttons.
crosshair: A crosshair cursor, often used for selection.
move: Indicates that the element is movable.
Example:
b. Link Cursors
Link cursors are designed for elements like hyperlinks or buttons:
pointer: A hand cursor indicating that the element is clickable.
Example:
c. Text Cursors
Text cursors are used when hovering over text or input fields:
text: An I-beam cursor indicating that text can be selected or edited.
Example:
d. Resize Cursors
Resize cursors indicate that the element can be resized. These cursors are often used in resizable containers:
n-resize: Indicates that the element can be resized vertically (north).
e-resize: Indicates that the element can be resized horizontally (east).
nw-resize: Indicates diagonal resizing (northwest).
ne-resize: Indicates diagonal resizing (northeast).
Example:
e. Custom Cursors
You can also define custom cursors by specifying the URL of an image file. This allows for unique cursor designs tailored to your website's theme.
Example:
Explanation:
The custom cursor is defined by the image URL. If the image is not available or supported, the browser will fall back to the default cursor (
auto
).
4. Practical Examples
Let's see some practical examples of using CSS cursors.
Example 1: Default and Pointer Cursors
Explanation:
The first box shows the default arrow cursor.
The second box shows the pointer (hand) cursor, typically used for clickable elements.
Example 2: Text and Crosshair Cursors
Explanation:
The first box shows the text cursor (I-beam), indicating editable text.
The second box shows the crosshair cursor, often used for precision tasks like selecting areas.
Example 3: Custom Cursor
Explanation:
The box uses a custom cursor defined by an image URL. If the image fails to load or isn’t supported, the default cursor is used.
5. Conclusion
CSS cursors are a simple yet effective way to enhance the user experience on your website. By customizing the cursor based on the context, you provide visual feedback that can guide users on how to interact with various elements.
Basic Cursors: Default arrow, pointer, crosshair, etc.
Link Cursors: Used for clickable elements like links and buttons.
Text Cursors: Used for editable text areas.
Resize Cursors: Indicate resizable elements.
Custom Cursors: Unique cursors using images.
Using the right cursor for the right context can make your web pages more intuitive and user-friendly.
For more tutorials and insights, visit Codes With Pankaj.
Last updated