Next.js Discord

Discord Forum

Cannot Use Images or Divs Inside Links?

Answered
Alligator mississippiensis posted this in #help-forum
Open in Discord
Avatar
Alligator mississippiensisOP
pretty self explanatory, I want to be able to use images & divs inside links. I dont know why even Next.js doesnt allow us to do this, I thought of using a relative container with absolute positioned links to solve this but I feel like I shouldn't need to do that, why does next.js not allow us to do that with link elements?
regular <a> tags work just fine with images and divs but if I use that then next.js complains about regular <a> being unoptimized
Answered by Alligator mississippiensis
it was using this random old component that I named as Link 💀
View full answer

12 Replies

Avatar
can you share the error for:
even Next.js doesnt allow us to do this
Avatar
Alligator mississippiensisOP
next.js itself doesnt raise an error, however ts does, as well as the linter
and next.js will just not render the elements inside the Link element, it only renders the text

the code for the one on the right:
export function TestCta({ url, svg, text }: { url: string; svg: string; text: string }) {
  return (
    <Link url={url} text={text}>
      <Image height={40} src={svg} />
    </Link>
  );
}
Image
the one on the left is if I use <Link>
the one on the right is if I use <a>
Image
Avatar
Alligator mississippiensisOP
this bug / weird behaviour happens even for span elements within the link...
Image
here is how its supposed to look
Image
Avatar
thanks for clarifing. Can you share the error for:
...raise an error, however ts does, as well as the linter
Avatar
Alligator mississippiensisOP
wait, I think I know why it happens lol
lol
Image
Avatar
I am quite confused rn
Avatar
Alligator mississippiensisOP
it was using this random old component that I named as Link 💀
Answer
Avatar
Alligator mississippiensisOP
instead of the official next.js's link
Avatar
oh lol