惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hacker News - Newest:
Hacker News - Newest: "LLM"
Webroot Blog
Webroot Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
N
News and Events Feed by Topic
H
Hacker News: Front Page
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
SecWiki News
SecWiki News
N
News | PayPal Newsroom
T
Tor Project blog
W
WeLiveSecurity
A
Arctic Wolf
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Secure Thoughts
月光博客
月光博客
AWS News Blog
AWS News Blog
D
Docker
C
CERT Recently Published Vulnerability Notes
MyScale Blog
MyScale Blog
Google Online Security Blog
Google Online Security Blog
大猫的无限游戏
大猫的无限游戏
T
The Blog of Author Tim Ferriss
I
InfoQ
人人都是产品经理
人人都是产品经理
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
Hacker News: Ask HN
Hacker News: Ask HN
Blog — PlanetScale
Blog — PlanetScale
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
Recorded Future
Recorded Future
罗磊的独立博客
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
The Exploit Database - CXSecurity.com
D
DataBreaches.Net
S
Security Affairs
WordPress大学
WordPress大学
T
Threatpost
Microsoft Security Blog
Microsoft Security Blog
V
Vulnerabilities – Threatpost
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
B
Blog RSS Feed
Project Zero
Project Zero
P
Proofpoint News Feed

CSS Articles by Temani Afif

Get Ready For the Powerful CSS border-shape Property! | CSS-Tricks Let’s Play With Gap Decorations! How to Control Infinite CSS Animations (Part 2 of 2) How to Control Infinite CSS Animations (Part 1 of 2) Two Circles, One Arrow, and Anchor Positioning Making a Responsive Pyramidal Grid With Modern CSS | CSS-Tricks How to Create a CSS-only Elastic Text Effect Making Complex CSS Shapes Using shape() | CSS-Tricks Responsive Hexagon Grid Using Modern CSS | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 2) | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 1) | CSS-Tricks Perfectly Pointed Tooltips: To The Corners Perfectly Pointed Tooltips: All Four Sides Perfectly Pointed Tooltips: A Foundation Sequential linear() Animation With N Elements | CSS-Tricks Infinite Marquee Animation using Modern CSS Better CSS Shapes Using shape() — Part 4: Close and Move | CSS-Tricks Drawing CSS Shapes using corner-shape Better CSS Shapes Using shape() — Part 3: Curves | CSS-Tricks Better CSS Shapes Using shape() — Part 2: More on Arcs | CSS-Tricks Better CSS Shapes Using shape() — Part 1: Lines and Arcs | CSS-Tricks Creating Blob Shapes using clip-path: shape() Creating Flower Shapes using clip-path: shape() Custom progress element using the attr() function A CSS-Only Star Rating Component and More! (Part 2) | CSS-Tricks A CSS-Only Star Rating Component and More! (Part 1) | CSS-Tricks How to Create Wavy Boxes Using CSS Full-Bleed Layout with Modern CSS Fancy Menu Navigation Using Anchor Positioning | CSS-Tricks How to Create a Zig-Zag Box Using CSS How to Create Zig-Zag CSS Loaders Using One Element Custom Progress Element Using Anchor Positioning & Scroll-Driven Animations How to Create Filling CSS Loaders Using One Element How to Create Curved-Edge and Rounded-Edge Shapes Using CSS CSS Tricks That Use Only One Gradient | CSS-Tricks How to create Shapes with Inner Curves using CSS Mask Custom Range Slider Using Anchor Positioning & Scroll-Driven Animations How to Get the Width/Height of Any Element in Only CSS How Keyboard Navigation Works in a CSS Game How To Create Cut-Out Shapes using The clip-path property The Modern Guide For Making CSS Shapes — Smashing Magazine css-shape.com: The Ultimate Collection of CSS-only Shapes ⚡️ Sliding 3D Image Frames In CSS — Smashing Magazine CSS Tricks To Master The clip-path Property Creating Wavy Circles with Fancy Animations in CSS Modern CSS Tooltips And Speech Bubbles (Part 2) — Smashing Magazine Do you need a Tooltip or a Speech Bubble? I have created 100 using CSS 😲 Modern CSS Tooltips And Speech Bubbles (Part 1) — Smashing Magazine Creating Flower Shapes using CSS Mask & Trigonometric Functions CSS Shapes: The Triangle The Complex But Awesome CSS border-image Property — Smashing Magazine CSS Tricks to add 3D Effects to your Text CSS Responsive Multi-Line Ribbon Shapes (Part 2) — Smashing Magazine CSS Responsive Multi-Line Ribbon Shapes (Part 1) — Smashing Magazine I have made 100+ CSS-only Ribbon Shapes | The Perfect Collection 🎀 How to create a CSS-only infinite scroll animation Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 2) — Smashing Magazine Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 1) — Smashing Magazine How to Create CSS Ribbon Shapes with a Single Element CSS Shapes: The Ribbon Revealing Images With CSS Mask Animations — Smashing Magazine css-loaders.com: The Biggest Collection of Loading Animations (more than 500 🤯) How To Define An Array Of Colors With CSS — Smashing Magazine CSS effects on images II Shines, Perspective, And Rotations: Fancy CSS 3D Effects For Images — Smashing Magazine What's your Lucky CSS Pattern? Check It Now! How to Add a CSS Reveal Animation to Your Images Modern Layouts using CSS Grid I created 100+ unique CSS patterns | The best collection 🤩 How to Create a Custom Range Slider Using CSS CSS Shapes: The Heart CSS Shapes: Polygon & Starburst A Text Reveal Animation using CSS CSS Tip: learn CSS the easy way! How to build a CSS-only accordion How to create Breadcrumb Navigation with CSS Different Ways to Get CSS Gradient Shadows | CSS-Tricks A CSS-only responsive Stepper component A Fancy Hover Effect For Your Avatar | CSS-Tricks How to make a zoom effect using CSS CSS Infinite 3D Sliders | CSS-Tricks CSS Infinite Slider Flipping Through Polaroid Images | CSS-Tricks How to create a responsive sidebar menu using CSS CSS Infinite and Circular Rotating Image Slider | CSS-Tricks Making Static Noise From a Weird CSS Gradient Bug | CSS-Tricks CSS Grid and Custom Shapes, Part 3 | CSS-Tricks Fancy Image Decorations: Outlines and Complex Animations | CSS-Tricks Fancy Image Decorations: Masks and Advanced Hover Effects | CSS-Tricks Fancy Image Decorations: Single Element Magic | CSS-Tricks How to create an infinite image slider using CSS How to Create Wavy Shapes & Patterns in CSS | CSS-Tricks How I Made a Pure CSS Puzzle Game | CSS-Tricks How to create a Tooltip/Speech Bubble using CSS CSS Grid and Custom Shapes, Part 2 | CSS-Tricks CSS Grid and Custom Shapes, Part 1 | CSS-Tricks Zooming Images in a Grid Layout | CSS-Tricks How to create a CSS-only loader with one element Exploring CSS Grid’s Implicit Grid and Auto-Placement Powers | CSS-Tricks How to create background pattern using CSS & conic-gradient Single Element Loaders: The Bars | CSS-Tricks
Creating a Custom Cursor using CSS
2023-01-24 · via CSS Articles by Temani Afif

Creating a Custom Cursor using CSS

Written on by Temani AfifWeb Developer

Estimated read time 5 minutes

In this article, we will cover different ways of creating a custom cursor using only CSS. We will study two different methods. The classic one that you probably know and an advanced one lesser know. The main focus will be on the advanced method that allows us to customize a cursor using CSS without any external resources.

The Classic Method

I think it’s not a surprise if I tell you we are going to use the cursor CSS property. From the MDN page, we can read:

The cursor CSS property sets the mouse cursor, if any, to show when the mouse pointer is over an element.

and

The cursor property is specified as zero or more <url> values, separated by commas, followed by a single mandatory keyword value.

In order to update the cursor, you either consider one of the predefined keywords or use an external image.

html {
  cursor: pointer;
  cursor: help;
  cursor: progress;

  cursor: url("pointer.png"), auto
}

That’s it! All you have to do is to use your favorite image editor to create your own cursor or check the MDN page for all the predefined keywords.

This is the classic method and it’s clear that we don’t have any CSS control over the cursor. It’s either an external resource or an icon that depends on your browser, OS, etc. This said it remains the easiest way to customize your cursor.

40%

💰 40% OFF YOUR FIRST MONTH WITH VERPEX MANAGED CLOUD SERVERS

with the discount code

SERVERS-SALE

The Advanced Method

In this method, we are still going to use the cursor property but we will introduce a bit of SVG that will help us insert our CSS. It’s a kind of CSS-in-SVG-in-CSS. I know it’s a bit confusing but you will see that the logic is pretty easy.

Since we can consider external resources, it means that we can either use an image file or an SVG file.

html {
  cursor: url("pointer.png"), auto
  cursor: url("pointer.svg"), auto
}

You can create your cursor using SVG, store it in a file and then link it within the cursor property. Below is a basic example:

I defined the SVG like below:

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
          "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg viewBox="0 0 30 30" width="30" height="30" xmlns="http://www.w3.org/2000/svg">
  <rect x="0" y="0" width="30" height="30" fill="red" />
</svg>

You can consider any shape you want. All you need is some SVG skills or a tool that can export your creation into SVG.

Now we are going to remove the use of the external resource and embed the SVG directly into the CSS using the following code:

html {
  cursor: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 30 30"><rect x="0" y="0" width="30" height="30" fill="red" /></svg>'), auto;
}

Now the SVG code is written directly into the CSS and we can easily adjust it there without the need for any external resources. All you have to do is to start the URL with data:image/svg+xml;utf8, then copy/paste your SVG code.

But we are still using SVG and not CSS to style the cursor?

True but we are not done yet! This method relies on SVG and it’s a powerful one if you have good knowledge of SVG. I highly recommend this method because SVG is more suitable to draw shapes than CSS but the CSS method we will see next is also a good one especially if you are more comfortable using CSS than SVG (like me!)

Now we are going to use something called <foreignObject> which is as SVG tag that allows us to integrate non-SVG elements within an SVG:

The <foreignObject> SVG element includes elements from a different XML namespace. In the context of a browser, it is most likely (X)HTML. ref

We can include HTML inside an SVG which means we can use CSS to style the HTML. Our previous cursor can be written like below:

html {
  cursor: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 30 30"><foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="width:100%;height:100%;background:red;"></div></foreignObject></svg>') , auto;
}

We used the cursor property (CSS) to embed an SVG code and inside the SVG code we embedded some HTML/CSS. We have our CSS-in-SVG-in-CSS. SVG here is only used as a container for our HTML/CSS code.

In the above example, I used a single div and inline styles to make the code easier but we can have more HTML elements and also separate the CSS. You have to follow some formatting rules to make sure the code works fine:

  • You need to minify your code and avoid any line break

  • Pay attention to the use of single quotes ' and double quotes ". If you are using a single quote with url() you need to use double quotes inside the SVG and vice versa

  • You need to encode the # character. Replace all the occurrences with %23. If you want to use the color #fff it should be %23fff.

  • If you want to separate the CSS, you can embed <style> ... </style> inside <foreignObject> but you need to make it: %3Cstyle%3E ... %3C/style%3E.

I know, it’s a lot of rules to remember in addition to the complex structure but we get easily used to it with some practice. Most of the time, you will copy and paste a previous code and adjust it, that’s what I do all the time. No need to remember everything by heart. Get back here, grab the code of one cursor and adjust it to create your own cursor.

Here is a demo with different examples of cursors that use the above code:

From the basic shape to the complex one, we can have anything we want. We can even insert some text as shown in the last example. The only limit is your imagination!

I let you explore those examples and you will notice that all I am doing is writing some common CSS while respecting the same structure and the rules I listed above.

90%

💰 90% OFF YOUR FIRST MONTH WITH ALL VERPEX CLOUD HOSTING PLANS

with the discount code

AWESOME

Conclusion

Customizing a cursor has no secret for you now! From using an existing image to embedding CSS inside SVG, you have plenty of ways to create your own design. There is no best method, it all depends on your need. The last one may look a bit overkill but it’s a good exercise to practice with some SVG while still using CSS.

Frequently Asked Questions

Temani Afif

Temani Afif is an expert web developer, a content creator, and a CSS addict. He is the mastermind behind CSS Loaders, CSS Generators, CSS Tip and many other CSS-related websites.

View more posts by Temani Afif