Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Icon size cannot be change with TailwindCSS v4 class #346

Closed
misbahansori opened this issue Jan 28, 2025 · 6 comments
Closed

Icon size cannot be change with TailwindCSS v4 class #346

misbahansori opened this issue Jan 28, 2025 · 6 comments

Comments

@misbahansori
Copy link

When using the Icon component from @nuxt/icon, the icon size cannot be changed using TailwindCSS v4 classes.

Example :

<Icon name="mdi:home" class="size-10" />
<Icon name="mdi:home" class="w-10 h-10" />

These icons should be 40x40px, but instead, they are using the default size of the icon.

Image Image Image

Expected behavior :
The icon should be 40x40px.

Package version :

  • "@nuxt/icon": "1.10.3",
  • "@tailwindcss/vite": "^4.0.0",
  • "tailwindcss": "^4.0.0",

Minimal Reproduction:
https://github.com/misbahansori/nuxt-icons-issue

@cyberalien
Copy link

That works as intended. That icon is rendered as background image, which is impossible to do without setting width/height, so default width/height are set to 1em.

To change that, you can do one of the following:

  • Change rendering mode to svg by adding mode="svg"
  • Increase specificity of your width/height properties by adding ! before class name: class="!size-10"
  • Resize icon using font-size: class="text-[40px]"

@misbahansori
Copy link
Author

This used to work in TailwindCSS v3. I think there is something to do with the @layer directive in Tailwind V4.

Image https://tailwindcss.com/docs/upgrade-guide

Tailwind V4 uses native CSS layers and styles that are not inside the @layer directive will override the styles defined in the @layer directive based on the MDN specification.

Image

I'm not sure if this issue should be fixed on the Tailwind CSS or Nuxt Icon side

@untlsn
Copy link

untlsn commented Jan 29, 2025

Because tailwind now use layers we don't need to fight it, we can embrace it
Try adding cssLayer to config so icons are in the same layer:

defineNuxtConfig({
  icon: {
    cssLayer: 'icon', // <- this. Try to give unique name to prevent breaking tailwind layer order
  },
})

Then size-10 and w-10 h-10 will work
But still I suggest to use font-size for changing size of icon

Thanks to @riicodespretty for showing problem with naming collision of layers

@antfu
Copy link
Member

antfu commented Jan 29, 2025

Yeah I believe that @untlsn's comment above should solve that issue (Thanks!)

And yes I would also suggest to use font-size, like text-xl.

@antfu antfu closed this as completed Jan 29, 2025
@frank-laemmer
Copy link

Well written ticket and the proposed solution to send icon to the correct css layer appears to be working.

I want to use size-2/3 for an icon - roughly translated into CSS: width: 66%; height: 66%. If I understand correctly, the size property of the icon will not compute 66%. So the CSS overwrite is required for me.

@riicodespretty
Copy link

In case someone looks it up, I do not recommend @untlsn's solution as it breaks tailwind layer orders, for example making the preflights override utility classes as explained here: tailwindlabs/tailwindcss#15864 (comment).

Do not use tailwind layer names, instead here's a safer solution:

export default defineNuxtConfig({
  // ...
  icon: {
    cssLayer: 'icon'
  },
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants