Image component from next/image and Tailwind CSS
Unanswered
Red-billed Tropicbird posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
Hi,
I want to use the Image component from Next/Image, and style it with Tailwind CS. But it does not seem to be working properly because it expects me to either specify the
Using the
I want to use the Image component from Next/Image, and style it with Tailwind CS. But it does not seem to be working properly because it expects me to either specify the
width
and height
if I use the source directly in the image component, or it will figure out the size automatically if I import it in the file on top as seen below: import Image from 'next/image'
import adamwathan from '../public/adamwathan.jpeg'
export default function Home() {
return (
<div className='bg-gray-700 text-white min-h-screen flex items-center justify-center'>
<Image src={adamwathan} alt='test' className='rounded-full w-10 h-10' />
</div>
)
}
Using the
w-10 h-10
on className with Tailwind CSS yields no change.66 Replies
![Avatar](https://cdn.discordapp.com/avatars/770682951930871828/d6c8de82dff289b9fe61e0a09fea8cb1.webp?size=256)
Morgan
First, what physical size is the image?
Second, what happens if you remove the flex class from its parent?
Second, what happens if you remove the flex class from its parent?
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
460x460
![Avatar](https://cdn.discordapp.com/avatars/770682951930871828/d6c8de82dff289b9fe61e0a09fea8cb1.webp?size=256)
Morgan
You might need to set a max-width on the image
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
Removng flex just moves the image to the left top corner
![Avatar](https://cdn.discordapp.com/avatars/770682951930871828/d6c8de82dff289b9fe61e0a09fea8cb1.webp?size=256)
Morgan
Ok. Is there one already? Eg max-width: 100%?
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
instead of being center
Nope, I only wrote what is in the code
Should I try max-width?
![Avatar](https://cdn.discordapp.com/avatars/770682951930871828/d6c8de82dff289b9fe61e0a09fea8cb1.webp?size=256)
Morgan
Always use max-width: 100% height: auto as defaults on images otherwise they overflow.
![Avatar](https://cdn.discordapp.com/avatars/478299681806614551/fcdbf2cc6e0e44318ab1b95142f83e6d.webp?size=256)
!isColonel
![Image](https://cdn.discordapp.com/attachments/1154517562231623790/1154521038672109669/41230262-2343-4A06-BD27-962A1B83A5C2.png)
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
e.g. as part of tailwind className?
![Avatar](https://cdn.discordapp.com/avatars/478299681806614551/fcdbf2cc6e0e44318ab1b95142f83e6d.webp?size=256)
!isColonel
Required Props
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
Yea they are required, but if that is the case then I will just regular old
img
html tagsince I want to style with tailwind
export default function Home() {
return (
<div className='bg-gray-700 text-white min-h-screen flex items-center justify-center'>
<img src='/adamwathan.jpeg' alt='test' className='rounded-full w-10 h-10' />
</div>
)
}
This works flawlessly
img
![Avatar](https://cdn.discordapp.com/avatars/478299681806614551/fcdbf2cc6e0e44318ab1b95142f83e6d.webp?size=256)
!isColonel
Can still style img tag but u loose the quality and priority props which help make image more better
![Avatar](https://cdn.discordapp.com/avatars/770682951930871828/d6c8de82dff289b9fe61e0a09fea8cb1.webp?size=256)
Morgan
It’s the actual img element you need to apply max-width on
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
So you're saying I should just use the
Image
with the required props and forget about using the tailwind w
and h
styles?:)(but use everything else from Tailwind)
import Image from 'next/image'
import adamwathan from '../public/adamwathan.jpeg'
export default function Home() {
return (
<div className='bg-gray-700 text-white min-h-screen flex items-center justify-center'>
<Image src={adamwathan} alt='test' width={40} height={40} className='rounded-full' />
</div>
)
}
This works
I guess thats just how people do it?
![Avatar](https://cdn.discordapp.com/avatars/478299681806614551/fcdbf2cc6e0e44318ab1b95142f83e6d.webp?size=256)
!isColonel
Use what u want. If image allows u to do what u want than use that but Image from next js requires those props. U can use Image and use the style={{}} and make it differently I think but like I said if priority and quality props dont matter just leavit
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
seems weird to have styles both as its props and inside classname
I prefer to learn the 'correct' nextjs way, which is the Image component
but I was just a bit confused as to why it had width and height as required props
seems like you answered
its probably due to making the quality and priority better
![Avatar](https://cdn.discordapp.com/avatars/478299681806614551/fcdbf2cc6e0e44318ab1b95142f83e6d.webp?size=256)
!isColonel
Well its just the way nextjs is.
![Avatar](https://cdn.discordapp.com/avatars/770682951930871828/d6c8de82dff289b9fe61e0a09fea8cb1.webp?size=256)
Morgan
I thought width and height aren’t required when importing the image?
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
so I will use Image component with with and height required props, and tailwind CSS className for everything else in styles
It isn't
but if one wants to specify something
thats the way to do it, it seems
![Avatar](https://cdn.discordapp.com/avatars/770682951930871828/d6c8de82dff289b9fe61e0a09fea8cb1.webp?size=256)
Morgan
So this doesn’t make sense.
![Avatar](https://cdn.discordapp.com/avatars/478299681806614551/fcdbf2cc6e0e44318ab1b95142f83e6d.webp?size=256)
!isColonel
You using ts or js?
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
TS
![Avatar](https://cdn.discordapp.com/avatars/478299681806614551/fcdbf2cc6e0e44318ab1b95142f83e6d.webp?size=256)
!isColonel
I think ive gotten away with just using width prop on js but ts is way strict.
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
import Image from 'next/image'
import adamwathan from '../public/adamwathan.jpeg'
export default function Home() {
return (
<div className='bg-gray-700 text-white min-h-screen flex items-center justify-center'>
<Image src={adamwathan} alt='test' className='rounded-full' />
</div>
)
}
I can do it without width and height when I import it
but I specified
width={40} height={40}
to make it smaller.The default is huge (e.g. original image size)
![Avatar](https://cdn.discordapp.com/avatars/770682951930871828/d6c8de82dff289b9fe61e0a09fea8cb1.webp?size=256)
Morgan
Well that makes sense as the image is massive
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
I wanted to resize it with tailwind css className, and I couldn't
w-10 and h-10 does nothing
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Cairn Terrier
Wrap it in a div and make it fill that maybe?
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
seems like more work than just abandoning
w
and h
tailwind css styles in className, and just use width={40} height={40}
props as part of the Image component itself![Avatar](https://cdn.discordapp.com/avatars/770682951930871828/d6c8de82dff289b9fe61e0a09fea8cb1.webp?size=256)
Morgan
Not worth the hassle. But if you’re interested to know more, check what element the class is applied to.
![Avatar](https://cdn.discordapp.com/avatars/478299681806614551/fcdbf2cc6e0e44318ab1b95142f83e6d.webp?size=256)
!isColonel
I did this for one of mine
<Image
priority
quality={100}
width={300}
height={300}
className="p-1 rounded-full ring-2 ring-sky-300 w-1/2 md:w-full md:h-full "
style={{
width: "205px",
height: "205px",
objectFit: "cover",
}}
src={Me}
alt="Developer profile image"
/>
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
does your image resizing work in the
className
![Avatar](https://cdn.discordapp.com/avatars/478299681806614551/fcdbf2cc6e0e44318ab1b95142f83e6d.webp?size=256)
!isColonel
Made it smaller with style prop idk 🤷â€â™‚ï¸. Anyway ya
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
even after setting width and height props?
![Avatar](https://cdn.discordapp.com/avatars/478299681806614551/fcdbf2cc6e0e44318ab1b95142f83e6d.webp?size=256)
!isColonel
I dont think so. I can remove them cuz i style it with uh style prop. You trying to make it bigger and smaller depending on screen size?
![Avatar](https://cdn.discordapp.com/avatars/770682951930871828/d6c8de82dff289b9fe61e0a09fea8cb1.webp?size=256)
Morgan
You’ve also got object fit
And w-full
But then overriding w-full
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
Im just trying to make it smaller in general
![Avatar](https://cdn.discordapp.com/avatars/478299681806614551/fcdbf2cc6e0e44318ab1b95142f83e6d.webp?size=256)
!isColonel
Tried a smaller number? In width and height prop?
Ya imma remove
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
Yes that work
but I wanted to do in classname using tailwind css
![Avatar](https://cdn.discordapp.com/avatars/770682951930871828/d6c8de82dff289b9fe61e0a09fea8cb1.webp?size=256)
Morgan
Lol
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Red-billed TropicbirdOP
but I got it now
I have to use the required props
![Avatar](https://cdn.discordapp.com/avatars/478299681806614551/fcdbf2cc6e0e44318ab1b95142f83e6d.webp?size=256)
!isColonel
In my experience, the more u let next js do for you, cleaner the code will be