Maybe the tech stack doesn’t matter here, but I have a nextjs project with tailwind for styling. I’m trying to insert an image next to an h2 tag, but the image won’t behave how I expect it to. There is no vertical overlap, it’s like the image is taking up a row, and the text is taking up another row. Here’s what I have right now:
And the code that generated it:
<div className="pb-3 justify-center align-center">
<Image
src={logoNextjs}
alt="logo nextjs"
width={100}
height={100}
/>
<div>
<h2 className="text-4xl font-extrabold leading-none tracking-tight text-gray-900 md:text-5xl lg:text-6xl text-center">
Sample
</h2>
</div>
<p className="text-center pb-1 text-lg font-normal text-gray-500 lg:text-xl sm:px-16 xl:px-48 ">
Demo
</p>
</div>
If I add a float-left
class from tailwind to nextjs’s Image
tag, I can get the image and h2 to be somewhat vertically aligned (at least they have some vertical overlap):
But the problem remains that the image is way to the left, when I want it to be right next to the text.
From what I understand, Image
is basically an img
tag with some optimization, so I think plain css solutions will work.
How can I achieve the behavior of the image being fully vertically aligned with the text, and right next to it?
2
Answers
Try adding a flex-box with
justify-content: center
:Result:
Standard positioning approaches include:
and…
Working Example:
Learn Flexbox:
Further Reading: