在 React 中改变 SVG 颜色

本文介绍了如何在React应用中使用svgr加载器处理SVG格式,并通过添加fill或stroke属性来改变SVG组件的颜色。通过设置`fill=currentColor`和`stroke=currentColor`,可以确保SVG元素颜色与父元素一致,实现动态颜色效果。同时,检查SVG文件中是否存在不必要的fill属性,以确保颜色设置生效。
  1. 利用 svgr 加载器处理 SVG 格式
  2. 导入
import CloudSVG from "/public/img/application/panel/cloud.svg";
  1. 使用这个 SVG 组件,直接添加 fill 属性,改变 SVG 的颜色
<CodeSVG fill="#404a59" />
  1. 如果没有其作用的话,检查 SVG 文件:
    1. 注意将 fill 改为 current
      1 - 1 fill to current
    2. path上如果有 fill 属性的话,可以将其删除
      1 - 2 delete fill
  2. 如果是 stroke 的话,用 currentColor
<svg width="20" height="17" viewBox="0 0 20 17" fill="none" stroke="currentColor" xmlns="http://www.w3.org/2000/svg">
    <path clip-rule="evenodd" d="M2.636 1.545h14.727c.904 0 1.637.733 1.637 1.637v10.636c0 .904-.733 1.636-1.636 1.636H2.636A1.635 1.635 0 0 1 1 13.818V3.182c0-.904.733-1.637 1.636-1.637z" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
    <path d="m1.12 2.575 7.693 5.503a2 2 0 0 0 2.325.002l7.738-5.519" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
</svg>

<Email stroke="red" />
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值