r/nextjs•
comstrader

How do I adjust the size/color of an svg image in nextjs?

Need help

I want to adjust the color and size of an svg image "headset.svg".

Before I was using next/image:

<Image
  src={'/headset.svg'}
  alt='logo'
  width={30}
  height={30}
  className='object-contain'
/>

This works well for the size but I want to change the color (in css) as well.

I've added the svgr tool to make a component of the svg image.

next.config.js:

module.exports = {
    webpack(config) {
      config.module.rules.push({
        test: /\.svg$/i,
        issuer: /\.[jt]sx?$/,
        use: ['@svgr/webpack'],
      })
      return config
    },
  }

Code:

import Logo from '../public/headset.svg';
...
<Logo className='w-[50px] h-[50px] text-black' />

But the css (className='w-[50px] h-[50px] text-black') is not being applied to the svg image.

Posted Jul 21, 2023, 7:06 PM UTC

14
keithj0nes•

SVGs use fill and stroke for color. So you’d wanna do something like fill-red-100 or stroke-blue-400

1
comstraderOP•

So "<Logo className='fill-red-100' />" ? Doesn't seem to work either

1
SqualoRx•

If you want to use text-black as the svg color, your svg must have the fills/strokes set to currentColor for example:

<svg width="100" height="100"> <circle cx="50" cy="50" r="40" stroke="currentColor" stroke-width="4" fill="currentColor" /></svg>

currentColor is a css property that use the current text color as a value.

However, if you don't want to change your svg directly, you can create a class like .headset-icon and and access the svg to change its color:

.headset-icon path { @apply black; }

1
comstraderOP•

So <svg width="100" height="100"> just makes the element that size, but not the actual svg.

1