(Next.js) How can I change the color of SVG in next/image?

Viewed 15624
import Image from 'next/image'

...

<Image src="/emotion.svg" alt="emtion" width={50} height={50} />

I want to change the SVG color in next/image.

But in CSS,

img {
  fill="#ffffff"
}

is not working.

How can I solve this?

3 Answers

I recommend you to not to use svg files directly, but use Playground SVG (https://react-svgr.com/playground/), convert the file to JS and then you can change the color and other stuff by props.

I think the best solution here is not to use Image. by default the nextjs Image component is not doing any optimization on svg's. You will then have multiple options/solutions to your problem:

  1. Extend webpack config on your next.config.js file using @svgr/webpack:
// next.config.js

webpack(config) {
  config.module.rules.push({
    test: /\.svg$/,
    use: ["@svgr/webpack"]
  });

  return config;
}
// The file where u want to import the svg
import YourSvg from './public/yourSvg.svg'

// I recommend using the `currentColor` property on your svg, but you can also pass the color as prop
<YourSvg color="red" />

  1. Using babel-plugin-inline-react-svg: https://github.com/vercel/next.js/discussions/20993#discussioncomment-760119 enter image description here
Related