cursor 属性是 CSS 用户界面模块中的一个属性,它允许您控制当鼠标指针悬停在某个元素上时,其显示的图标样式。这对于提供视觉反馈、增强用户体验和指示元素的可交互性至关重要。
1. cursor 属性的作用 (What is cursor?)
cursor 属性的主要作用是:
-
提供视觉线索: 告知用户当他们与某个元素交互时会发生什么。例如,一个手型光标 (
pointer) 通常表示该元素是可点击的链接或按钮;一个 I 形光标 (text) 表示文本可被选中。 - 增强用户体验: 通过直观的光标变化,让用户更容易理解界面的功能和可操作区域。
-
指示状态: 比如,
wait或progress光标可以告诉用户系统正在忙碌,需要等待。
2. 语法 (Syntax)
cursor 属性的语法如下:
cursor: keyword | url(<uri>, [<uri>]*), keyword;
您可以指定一个关键字(最常见),或者一个或多个自定义图片(通过 url() 函数),然后紧跟着一个作为后备(fallback)的关键字。
3. 可能的值 (Possible Values)
cursor 属性接受大量的预定义关键字值,以及自定义 url() 值。
a) 关键字值 (Keyword Values)
这些是 CSS 提供的标准光标类型,分为几大类:
-
通用光标 (General Cursors):
-
auto: 默认值。浏览器根据上下文决定光标样式。例如,在文本上会显示text,在链接上会显示pointer。 -
default: 默认的光标,通常是一个箭头。 -
none: 不显示任何光标。应谨慎使用,可能导致用户困惑。
-
-
链接与交互光标 (Links & Interactivity Cursors):
-
pointer: 手型光标,通常表示该元素是可点击的链接、按钮或任何交互式元素。 -
help: 问号光标,表示有帮助信息可用。
-
-
文本选择光标 (Text Selection Cursors):
-
text: I 形光标,表示文本可以被选中。 -
vertical-text: 用于垂直文本的 I 形光标。
-
-
忙碌/等待光标 (Busy/Waiting Cursors):
-
wait: 沙漏或旋转圈,表示程序正在忙碌,但用户仍可继续操作其他部分。 -
progress: 旋转圈(或类似图标),表示程序正在忙碌且当前操作可能需要等待完成。
-
-
拖放光标 (Drag & Drop Cursors):
-
grab: 抓取手型(张开的手),表示可以抓取(拖动)元素。 -
grabbing: 抓取中手型(握紧的手),表示元素正在被拖动。 -
move: 四向箭头,表示可以移动整个元素。
-
-
禁止与不允许光标 (Prohibited & Not Allowed Cursors):
-
not-allowed: 带斜线的圆圈(禁止符号),表示当前操作不允许。 -
no-drop: 与not-allowed类似,表示拖动操作在这里不允许放下。
-
-
调整大小光标 (Resizing Cursors):
-
n-resize,s-resize,e-resize,w-resize: 分别对应北、南、东、西方向的调整大小光标。 -
ne-resize,nw-resize,se-resize,sw-resize: 东北、西北、东南、西南方向的调整大小光标。 -
ns-resize,ew-resize: 垂直双向箭头、水平双向箭头。 -
nesw-resize,nwse-resize: 对角线双向箭头。 -
col-resize: 调整列宽的水平双向箭头。 -
row-resize: 调整行高的垂直双向箭头。
-
-
其他光标 (Other Cursors):
-
crosshair: 十字光标,通常用于精确选择或绘图工具。 -
zoom-in: 放大镜带加号,表示可以放大。 -
zoom-out: 放大镜带减号,表示可以缩小。
-
b) url() (自定义光标)
您可以使用一个或多个图像文件作为自定义光标。
-
语法:
url(path/to/image.cur), url(path/to/image.png) x y, default;-
url(): 指定图像文件的路径。 -
x y(可选): 指定光标的“热点”(hotspot)坐标,即鼠标实际点击的位置相对于图像左上角的偏移量。如果省略,热点通常默认为图像的左上角或中心。 - 您可以提供多个
url()作为后备方案,浏览器会尝试加载第一个支持的图像。 -
必须在
url()列表的最后指定一个关键字光标,作为所有自定义图像都无法加载时的最终后备。
-
-
支持的图像格式: 常见的有
.cur(Windows光标文件),.ico,.png,.gif(静态)。.svg格式在现代浏览器中也受支持。 - 文件大小限制: 浏览器通常对自定义光标的大小有严格限制(例如,不超过 32x32px 或 64x64px),并且文件大小也有限制。
-
示例:
cursor: url('my-cursor.cur'), url('my-cursor.png') 16 16, auto;
c) 全局值 (Global Values)
-
initial: 将cursor属性重置为其初始默认值,即auto。 -
inherit: 元素会继承其父元素的cursor属性的计算值。 -
unset: 如果该属性是继承属性,则其值设置为inherit;否则,其值设置为initial。 -
revert: 将属性值回滚到用户代理样式表或作者样式表中的上一个值。
4. 继承性 (Inheritance)
cursor 属性是继承的。这意味着如果你在一个父元素上设置了 cursor,它的所有子元素(除非它们自己显式地设置了不同的 cursor 值)都会继承这个光标样式。这是一个非常有用的特性,因为您通常希望整个可点击区域或某个功能区域内保持一致的光标样式。
5. 浏览器兼容性 (Browser ***patibility)
所有关键字光标在所有现代浏览器中都有非常好的支持。url() 形式的自定义光标也普遍支持,但不同浏览器对图像格式、大小和热点处理可能略有差异。
-
Can I use
cursor: https://caniuse.***/?search=cursor
6. 最佳实践与注意事项
-
保持一致性: 确保光标样式与元素的功能一致。例如,不要在普通文本上使用
pointer,因为它会误导用户认为文本是可点击的。 -
慎用
none:cursor: none;会使鼠标指针完全消失,可能导致用户迷失方向或难以操作。仅在非常特定的、有明确指示的场景下使用(如全屏游戏或自定义的虚拟光标)。 -
自定义光标限制:
- 文件大小和尺寸: 尽量保持自定义光标文件小巧,尺寸适中(例如 16x16, 24x24, 32x32 像素),以避免加载延迟和显示问题。
-
兼容性: 使用
.cur格式通常兼容性最好,但.png也很常用。始终提供一个或多个url()后备,并以一个通用关键字作为最终后备。 - 可访问性: 某些用户可能依赖系统默认光标的可见性或大小。过于独特或难以辨认的自定义光标可能会影响可访问性。
- 性能: 频繁切换或使用过大、复杂的自定义光标可能会对渲染性能产生轻微影响。
-
热点: 对于自定义光标,精确设置
x y坐标(热点)非常重要,它决定了实际的点击位置。例如,如果是一个箭头,热点通常是箭头的尖端;如果是一个十字,热点通常是中心。 -
继承的优势: 利用
cursor的继承性,可以在父容器上设置一个通用的光标样式,然后只在需要特殊处理的子元素上覆盖它。
7. 示例 (Examples)
下面通过一个 HTML 和 CSS 示例来演示 cursor 属性的各种用法,包括常见关键字和自定义光标。
HTML (index.html):
<!DOCTYPE html>
<html lang="zh-***">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS cursor 属性示例</title>
<link rel="stylesheet" href="style.css">
<style>
/* 为了演示,这里直接嵌入一个小的Base64编码SVG作为自定义光标 */
/* 实际应用中建议使用外部文件,并提供多重fallback */
.custom-cursor-element {
cursor: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'><path fill='orange' d='M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-1 15h2v-2h-2v2zm0-4h2V7h-2v6z'/></svg>") 12 12, help;
}
</style>
</head>
<body>
<header>
<h1>CSS `cursor` 属性演示</h1>
<p>将鼠标悬停在下面的区域,观察光标的变化。</p>
</header>
<main>
<section>
<h2>通用光标</h2>
<div class="cursor-box default-cursor">默认光标 (default)</div>
<div class="cursor-box auto-cursor">自动光标 (auto) - 尝试选中文字</div>
<div class="cursor-box none-cursor">无光标 (none) - 悬停时光标会消失!</div>
</section>
<section>
<h2>交互与文本光标</h2>
<a href="#" class="cursor-box pointer-cursor">可点击链接 (pointer)</a>
<div class="cursor-box help-cursor">帮助信息 (help)</div>
<div class="cursor-box text-cursor">可选择文本区域 (text)</div>
</section>
<section>
<h2>状态指示光标</h2>
<div class="cursor-box wait-cursor">请等待 (wait)</div>
<div class="cursor-box progress-cursor">加载中 (progress)</div>
</section>
<section>
<h2>拖放与禁止光标</h2>
<div class="cursor-box grab-cursor">可抓取 (grab)</div>
<div class="cursor-box grabbing-cursor">抓取中 (grabbing)</div>
<div class="cursor-box move-cursor">可移动 (move)</div>
<div class="cursor-box not-allowed-cursor">不允许 (not-allowed)</div>
</section>
<section>
<h2>调整大小光标</h2>
<div class="resize-container">
<div class="resize-handle n-resize-cursor">N</div>
<div class="resize-handle ne-resize-cursor">NE</div>
<div class="resize-handle e-resize-cursor">E</div>
<div class="resize-handle se-resize-cursor">SE</div>
<div class="resize-handle s-resize-cursor">S</div>
<div class="resize-handle sw-resize-cursor">SW</div>
<div class="resize-handle w-resize-cursor">W</div>
<div class="resize-handle nw-resize-cursor">NW</div>
<div class="resize-handle col-resize-cursor">Col</div>
<div class="resize-handle row-resize-cursor">Row</div>
</div>
</section>
<section>
<h2>自定义光标 (Custom Cursor)</h2>
<div class="cursor-box custom-cursor-element">
自定义光标区域 (自定义图标)
<p>鼠标悬停在此处时,您会看到一个橙色的感叹号图标。</p>
</div>
</section>
</main>
<footer>
<p>© 2023 {{IDENTITY}} - All rights reserved.</p>
</footer>
</body>
</html>
CSS (style.css):
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 0;
background-color: #f8f9fa;
color: #333;
line-height: 1.6;
}
header {
background-color: #007bff;
color: white;
padding: 20px;
text-align: center;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
header h1 {
margin-top: 0;
color: white;
}
main {
max-width: 900px;
margin: 20px auto;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
}
section {
margin-bottom: 40px;
padding-bottom: 20px;
border-bottom: 1px solid #eee;
}
section:last-of-type {
border-bottom: none;
margin-bottom: 0;
}
h2 {
color: #0056b3;
margin-top: 0;
padding-bottom: 10px;
border-bottom: 2px solid #007bff;
margin-bottom: 20px;
}
.cursor-box {
background-color: #e9ecef;
border: 1px solid #ced4da;
padding: 15px;
margin-bottom: 10px;
text-align: center;
font-size: 1.1em;
min-height: 40px;
display: flex;
align-items: center;
justify-content: center;
user-select: none; /* 防止文本被选中干扰光标观察 */
}
/* 通用光标 */
.default-cursor { cursor: default; background-color: #e0f2f7; }
.auto-cursor { cursor: auto; background-color: #e6ffed; user-select: text; /* 允许选择文本以看 auto 效果 */ }
.none-cursor { cursor: none; background-color: #ffe0e6; }
/* 交互与文本光标 */
.pointer-cursor { cursor: pointer; background-color: #***e5ff; color: #004085; }
.help-cursor { cursor: help; background-color: #fff3cd; color: #856404; }
.text-cursor { cursor: text; background-color: #d4edda; color: #155724; user-select: text; }
/* 状态指示光标 */
.wait-cursor { cursor: wait; background-color: #e2e6ea; }
.progress-cursor { cursor: progress; background-color: #dae0e5; }
/* 拖放与禁止光标 */
.grab-cursor { cursor: grab; background-color: #d1ecf1; }
.grabbing-cursor { cursor: grabbing; background-color: #bee5eb; }
.move-cursor { cursor: move; background-color: #c7ecec; }
.not-allowed-cursor { cursor: not-allowed; background-color: #f8d7da; color: #721c24; }
/* 调整大小光标容器 */
.resize-container {
display: grid;
grid-template-columns: repeat(5, 1fr); /* 5列布局 */
gap: 5px;
margin-top: 10px;
}
.resize-handle {
background-color: #e0f7fa;
border: 1px solid #00bcd4;
padding: 10px 5px;
text-align: center;
font-size: 0.9em;
user-select: none;
}
.n-resize-cursor { cursor: n-resize; }
.ne-resize-cursor { cursor: ne-resize; }
.e-resize-cursor { cursor: e-resize; }
.se-resize-cursor { cursor: se-resize; }
.s-resize-cursor { cursor: s-resize; }
.sw-resize-cursor { cursor: sw-resize; }
.w-resize-cursor { cursor: w-resize; }
.nw-resize-cursor { cursor: nw-resize; }
.col-resize-cursor { cursor: col-resize; background-color: #e0ffe0; border-color: #4caf50; }
.row-resize-cursor { cursor: row-resize; background-color: #e0e0ff; border-color: #673ab7; }
footer {
text-align: center;
padding: 20px;
margin-top: 30px;
background-color: #e9ecef;
color: #6c757d;
font-size: 0.9em;
border-top: 1px solid #dee2e6;
}
运行此示例并在不同区域悬停鼠标,您将看到各种光标样式的变化。请注意在 none-cursor 区域,光标会完全消失;在 auto-cursor 区域,尝试选中文字,光标会变为 text。自定义光标区域会显示我们定义的感叹号图标。