fill
属性或者 CSS 选择器来覆盖默认颜色。举个例子,默认情况下 Heroicons 的图标颜色由类名如text-gray-600
控制,但咱们完全可以通过 CSS 代码直接改写这个填充色。这里要注意,不同版本的 Heroicons 在结构上可能有细微差异,2025 年的最新版本更推荐通过 CSS 变量和自定义工具类来实现统一管理,这样既能保证代码整洁,又方便后续维护。<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
svg>
stroke="currentColor"
了吗?这就是控制线条颜色的关键。如果咱们想把这个图标改成蓝色,只需要把currentColor
替换成具体的色值,比如#165DFF
。完整代码就变成了:<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="#165DFF">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
svg>
fill
和stroke
属性,得注意优先级问题。一般来说,fill 会覆盖背景色,stroke 控制边框色,咱们可以根据实际需求调整这两个值。不过这种直接修改的方式不太适合大量图标,因为会增加 HTML 代码量,后续维护也麻烦,更推荐在样式表中统一处理。.hero-icon-primary {
fill: #165DFF;
stroke: #165DFF;
}
.hero-icon-secondary {
fill: #6B7280;
stroke: #6B7280;
}
<svg class="h-6 w-6 hero-icon-primary" ...>...svg>
<svg class="h-6 w-6 hero-icon-secondary" ...>...svg>
fill="none"
的属性,咱们得去掉或者改成具体颜色,不然 CSS 定义的 fill 可能不生效。另外,对于同时有填充和描边的图标,建议同时设置 fill 和 stroke 属性,避免出现颜色不一致的情况。还有啊,CSS 类名可以结合项目的设计系统来命名,比如icon-primary
、icon-success
等,这样团队协作时也能一目了然。// tailwind.config.js
module.exports = {
theme: {
extend: {
colors: {
primary: '#165DFF',
secondary: '#F59E0B',
// 其他颜色定义
},
},
}
}
<svg class="h-6 w-6 text-primary" ...>...svg>
<svg class="h-6 w-6 text-secondary" ...>...svg>
<svg class="h-6 w-6 fill-primary stroke-secondary" ...>...svg>
// tailwind.config.js
module.exports = {
theme: {
// 其他配置
},
plugins: [
function({ addUtilities }) {
addUtilities({
'.icon-red': {
'fill': '#EF4444',
'stroke': '#EF4444'
},
'.icon-green': {
'fill': '#10B981',
'stroke': '#10B981'
}
})
}
]
}
.icon-red
这样的自定义类了。这里要注意,Tailwind v4 更推荐使用 CSS 变量来管理颜色,这样可以实现动态主题切换,比如::root {
--color-icon-primary: #165DFF;
}
.hero-icon {
fill: var(--color-icon-primary);
stroke: var(--color-icon-primary);
}
:root
中的变量值,就能实现图标颜色的动态切换啦,这个技巧在需要暗黑模式的项目中特别有用。
标签或者单独的 CSS 文件中定义变量::root {
--hero-primary: #165DFF;
--hero-secondary: #6B7280;
--hero-danger: #EF4444;
}
.hero-icon {
fill: var(--hero-primary);
stroke: var(--hero-primary);
}
.hero-icon-secondary {
fill: var(--hero-secondary);
stroke: var(--hero-secondary);
}
.hero-icon-danger {
fill: var(--hero-danger);
stroke: var(--hero-danger);
}
<svg class="h-6 w-6 hero-icon" ...>...svg>
<svg class="h-6 w-6 hero-icon-secondary" ...>...svg>
:root
中的变量值:.dark-mode {
--hero-primary: #3B82F6;
--hero-secondary: #9CA3AF;
--hero-danger: #F87171;
}
.dark-mode
类,所有图标颜色就会自动切换,是不是很方便?这里要注意,CSS 变量的命名最好有统一的前缀,比如--hero-
,这样既能避免命名冲突,又方便识别。另外,变量值可以接受任何合法的颜色格式,比如十六进制、RGB、HSL,甚至是 CSS 函数如currentColor
,咱们可以根据项目需求灵活选择。<svg id="dynamic-icon" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
svg>
const icon = document.getElementById('dynamic-icon');
// 鼠标悬停时变红色
icon.addEventListener('mouseenter', () => {
icon.setAttribute('stroke', '#EF4444');
// 如果有fill属性也需要同时修改
if (icon.getAttribute('fill') !== 'none') {
icon.setAttribute('fill', '#EF4444');
}
});
// 鼠标离开时恢复原色
icon.addEventListener('mouseleave', () => {
icon.setAttribute('stroke', 'currentColor');
if (icon.getAttribute('fill') !== 'none') {
icon.setAttribute('fill', 'currentColor');
}
});
.red-icon {
fill: #EF4444;
stroke: #EF4444;
}
icon.addEventListener('mouseenter', () => {
icon.classList.add('red-icon');
});
icon.addEventListener('mouseleave', () => {
icon.classList.remove('red-icon');
});
import { useState } from 'react';
function DynamicIcon() {
const [isHovered, setIsHovered] = useState(false);
const color = isHovered ? 'red' : 'blue';
return (
<svg
className={`h-6 w-6 ${color}-icon`}
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
svg>
);
}
.hero-icon svg path {
fill: currentColor;
stroke: currentColor;
}
addUtilities({
'.text-icon': {
'fill': 'currentColor',
'stroke': 'currentColor'
}
})
<svg style="width:;height:;position:absolute">
<symbol id="icon-search" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
symbol>
svg>
<svg class="h-6 w-6 text-primary">
<use xlink:href="#icon-search" />
svg>
现在做自媒体,安全比什么都重要。头条账号检测功能就是咱们创作者的「安全卫士」,能帮我们提前发现内容里的雷区。今天就把这套「安全秘籍」掰开揉碎了讲给你听。 🔍 账号检测功能全解析 打开头条号后台,点「
🛡️ 数据上传安全机制:从传输到验证的全链路防护很多人第一次用朱雀 AI 时,都会下意识捏把汗 ——“我传的文档、图片,会不会在半路被人扒走?” 其实这块大可不必太紧张。朱雀用的是银行级别的 S
养号到底要多久才能获得推荐?这问题我被问了不下百遍。刚入行的新人总觉得有个标准答案,比如 “21 天必出推荐”“3 个月一定有流量”。但真相是 ——没有固定周期。去年我跟踪过 30 个新注册公众号,最
自媒体伪原创和 SEO 伪原创,听起来都是 “伪原创”,但内里的门道差得远。不少人觉得反正都是改改别人的东西,没什么区别,这想法可太片面了。今天就好好掰扯掰扯,让大家看清楚这俩到底有啥不一样,又有哪些
📌技术突破:从同义词替换到智能重构 2025 年的伪原创工具早已不是简单的同义词替换机器。像智媒 AI 写作平台,它用专利级创意基因库技术,能把改写后的内容原创度做到 92%。这背后是语义网络和知识
视频号如何带动公众号推荐量?双号联动,实现流量互补 现在各平台对于原创作品要求极高,简单的 AI 写作已经不能够通过原创检测,因此写作方法要全面升级,既要兼顾文章的吸引力、有流量属性,又要能够通过朱雀
在 2025 年的新媒体运营领域,一键分发平台凭借其高效性和便捷性,成为众多从业者的首选工具。这些平台能够帮助用户将内容快速、准确地发布到多个新媒体平台,大大节省了时间和精力。 🌟 一键分发平台的核
📌 公众号「看一看」怎么打开?新手必看,了解这个重要的流量入口 公众号「看一看」是微信生态里的重要公域流量入口,能帮文章突破粉丝基数限制,获得更多曝光。但很多新手不知道怎么找到这个功能,也不清楚它的