question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use tailwindcss with nextjs image

See original GitHub issue

Bug report

Describe the bug

Hello, I am trying to use tailwindcss in Nextjs project. But I can’t use my tailwind classes with Nextjs image component. Here’s my code.

To Reproduce

<Image
        src={img.img}
        alt="Picture of the author"
        width="200"
        height="200"
        className="h-56 w-56 object-cover"
    ></Image>

Expected behavior

Here I want to use tailwind classes instead of the height and width property of the nextjs image. But I can’t because it throws me an error. Also, unsized property throws another error saying it’s deprecated. Is there any solution? thanks in advance. Here is the error if I don’t use height and width property. enter image description here

When I use layout = ‘fill’ property it shows only one picture. And if I use unsized property, then the following error is shown. enter image description here

The problem here is that I always have to use height and width but I can’t make it to work with tailwindcss classes

Screenshots

System information

  • OS: Windows
  • Browser: chrome, safari
  • Version of Next.js: 10.0.1
  • Version of Node.js: e.g. 14.0.0
  • Deployment: I still haven’t deployed yet

Additional context

Add any other context about the problem here.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
lveillardcommented, Sep 21, 2021

This only solves things when the width and height are fixed. The Image component completely ignores every tailwind class. For example

image

With classes: className=“order-first lg:order-last mb-8 lg:mb-0 max-h-96 max-w-3/4 mx-auto”

Gets transformed into this: image

Setting up random width - height does not fix it, classes: "order-first lg:order-last max-h-96 max-w-3/4 are just being completely ignored.

Having the image under a div with those classes does not work neither.

1reaction
prantaDuttacommented, Nov 22, 2020

that’s because you are using layout=‘fill’. Just delete that and use height and width instead. Here’s my working code.

<Image
    src={img.img}
    alt="Picture of the author"
    width="200"
    height="200"
    className="object-contain"
></Image>
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Tailwind CSS with Next.js Image - Stack Overflow
I want to use Tailwind classes instead of the height and width property of the Next.js Image. But I can't because it throws...
Read more >
How to use Tailwind CSS with Next.js - DEV Community ‍ ‍
Hello guys, hope you are doing well. In this post I am going to discuss how to properly use tailwindcss classes with next.js....
Read more >
How to use next/future/image with Tailwind CSS? #38945
Next.js Image is extremely difficult to work with. Either the old one or the new one. It's our entire team's opinion. However, because...
Read more >
Install Tailwind CSS with Next.js
The quickest way to start using Tailwind CSS in your Next.js project is to use the Next.js + Tailwind CSS Example. This will...
Read more >
Building an Image Gallery with Next.js, Supabase, and ...
To set up your application, we can use create-next-app to clone a Next.js and Tailwind CSS starter application from the official examples ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found