最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

next.js - NextJS Wrap Link around NextJS Image - causes image to appear smaller - Stack Overflow

programmeradmin2浏览0评论

I want to wrap a Link tag around an image. However when doing so, the image appears much smaller than before (prior to adding the Link tag). How can I get the image back to its original size?

For clarity, remove the Link tag, the image size displays as desired. Add the Link tag, image displays much smaller than desired.

I am using MaterialUI, the NextJS Image tag and next version "14.2.9".

  <Link href={`/services/${link}`}>
          <Box
            sx={{
              display: "block",
              borderRadius: "0 0 2rem 0",
              overflow: "hidden",
              width: "100%",
              height: "100%",
              cursor: "pointer",
              position: "relative",
              "&:hover::before": {
                content: '""',
                position: "absolute",
                top: 0,
                left: 0,
                width: "100%",
                height: "100%",
                backgroundColor: "rgba(0, 0, 0, 0.5)",
                zIndex: 1,
              },
              "&:hover": {
                boxShadow: "0px 4px 20px rgba(0, 0, 0, 0.5)",
              },
            }}
          >
            <Image
              src={imageSrc}
              alt={title}
              priority={false}
              sizes="100vw"
              style={{ width: "100%", height: "100%" }}
              width={400}
              height={400}
            />
          </Box>
      </Link>

I want to wrap a Link tag around an image. However when doing so, the image appears much smaller than before (prior to adding the Link tag). How can I get the image back to its original size?

For clarity, remove the Link tag, the image size displays as desired. Add the Link tag, image displays much smaller than desired.

I am using MaterialUI, the NextJS Image tag and next version "14.2.9".

  <Link href={`/services/${link}`}>
          <Box
            sx={{
              display: "block",
              borderRadius: "0 0 2rem 0",
              overflow: "hidden",
              width: "100%",
              height: "100%",
              cursor: "pointer",
              position: "relative",
              "&:hover::before": {
                content: '""',
                position: "absolute",
                top: 0,
                left: 0,
                width: "100%",
                height: "100%",
                backgroundColor: "rgba(0, 0, 0, 0.5)",
                zIndex: 1,
              },
              "&:hover": {
                boxShadow: "0px 4px 20px rgba(0, 0, 0, 0.5)",
              },
            }}
          >
            <Image
              src={imageSrc}
              alt={title}
              priority={false}
              sizes="100vw"
              style={{ width: "100%", height: "100%" }}
              width={400}
              height={400}
            />
          </Box>
      </Link>
Share Improve this question asked Jan 20 at 5:03 codiancodian 175 bronze badges 1
  • 1 Have you tried removing this style={{ width: "100%", height: "100%" }} from your Image tag, i think it might fix your issue – Raja Jahanzaib Commented Jan 20 at 6:10
Add a comment  | 

1 Answer 1

Reset to default 1

The issue you're encountering is likely caused by the Link tag applying style. So I recommend below style for Link.

<Link href={`/services/${link}`} style={{display: "flex", width: "fit-content"}}>
    <Box
      sx={{
        display: "block",
        borderRadius: "0 0 2rem 0",
        overflow: "hidden",
        width: "100%",
        height: "100%",
        cursor: "pointer",
        position: "relative",
        "&:hover::before": {
          content: '""',
          position: "absolute",
          top: 0,
          left: 0,
          width: "100%",
          height: "100%",
          backgroundColor: "rgba(0, 0, 0, 0.5)",
          zIndex: 1,
        },
        "&:hover": {
          boxShadow: "0px 4px 20px rgba(0, 0, 0, 0.5)",
        },
      }}
    >
      <Image
        src={imageSrc}
        alt={title}
        priority={false}
        sizes="100vw"
        style={{ width: "100%", height: "100%" }}
        width={400}
        height={400}
      />
    </Box>
</Link>
发布评论

评论列表(0)

  1. 暂无评论