Adjust the size of a Next.js Image element within a container div

In an attempt to create a section with text on one side and a responsive image on the other, I encountered an issue. When using a basic

<img src="/myimage.extension" alt="describe my image" />
tag, everything worked perfectly. However, upon switching to the Next.JS Image component for optimization purposes, the responsiveness was lost.

To illustrate, here is the code for a hero section with a responsive image that works well:

<>
<div className={"h-screen py-0 py-20 text-center bg-blue-100 text-dark-blue"}>
  <div
    className={"flex flex-col items-center justify-center h-full md:flex-row app-container md:justify-between"}>
    <div>
      <h1 className={"text-red-400"}>With the Image component</h1>
      <p>It doesn't resize on the size of the viewport</p>
    </div>
    <div className={"relative"}>
      <svg
        className={"absolute right-0 z-50 hidden w-1/2 sm:block"}
        css={css`top: 25px;left: 25px;`}
        xmlns="http://www.w3.org/2000/svg" viewBox="0 0 674 674"
      >
        <circle id="Ellipse_8" cx="337" cy="337" r="337" fill="#e23b58"/>
      </svg>
      <img
        src={"/image.jpg"}
        alt={"Some image description"}
      />
    </div>
  </div>
</div>
</>

However, the moment the img tag is replaced by the Image component, the responsiveness breaks down – the image remains static in size.

<>
  <div className={"h-screen py-0 py-20 text-center bg-green-100 text-dark-blue"}>
    <div
      className={"flex flex-col items-center justify-center h-full md:flex-row app-container md:justify-between"}>
      <div>
        <h1 className={"text-red-400"}>With the Image component</h1>
        <p>It doesn't resize on the size of the viewport</p>
      </div>
      <div className={"relative"}>
        <svg
          className={"absolute right-0 z-50 hidden w-1/2 sm:block"}
          css={css`top: 25px;left: 25px;`}
          xmlns="http://www.w3.org/2000/svg" viewBox="0 0 674 674"
        >
          <circle id="Ellipse_8" cx="337" cy="337" r="337" fill="#e23b58"/>
        </svg>
        <Image
          src={"/image.jpg"}
          alt={"Some image description"}
          width={1103}
          height={628}
        />
      </div>
    </div>
  </div>
</>

You can view the issue in action on this sandbox. I am seeking a solution that allows for precise control over the positioning of the Image component.

Answer №1

The issue does not lie within the Image component itself. It actually stems from the styling applied to the surrounding div element.

If you eliminate the items-center class from lines 18-22, you will notice that the image starts resizing as expected:

<div className={"flex flex-col items-center justify-center ...}>

To resolve this, simply update the code to:

<div className={"flex flex-col justify-center ...}>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a Search Bar Component with React Native

I am attempting to integrate a searchbar from react-native-searchbar into my application in order to filter a list. However, I am encountering an error that states: "deputies is not defined." As a beginner in React Native, I am unsure about how to proc ...

Tips for resolving the issue of "Text stays in original position while image changes in the background when hovering over the text."

I'm currently working on a website and I have a question regarding how to make text stay in position while hovering over it (inside a span), and at the same time, display an image in the background that allows the text to overlay it. HTML: <ht ...

NextJS api route not resolving due to file path issue

I've encountered an issue while trying to resolve a file path in NextJS. It seems that API routes function differently when deployed on Vercel. To correctly point to the file, I attempted the following: const svg = fs.readFileSync( path.join(proces ...

Integrate the dForm plugin with a convenient time picker widget

Currently, I am attempting to integrate a time picker widget into a jQuery plugin called dForm. The specific timepicker widget can be found at the following link: https://github.com/jonthornton/jquery-timepicker Despite reviewing the dForm documentation, ...

Having trouble accessing my data from Firebase version 9

This is a snippet of my database I've been struggling to fetch data from a collection of users based on the associated user's UID (USER.UID). However, the code continues to return a "Document Not Found" error. import { db } from '../Fireb ...

Eliminate the dark backdrop from html5 videos that only shows up for a brief moment

Is there a way to remove the brief black background that appears when loading an HTML5 video? I have tried using CSS without success. The HTML: <div id="start-screen"> <video id="video-element"> <source src="video-mp4.mp4" type="vide ...

Struggling to properly test the material-ui datagrid component using react-testing-library

I'm facing an issue while testing the content of my material-ui datagrid. Only the first 3 out of 8 columns are displayed in the test, but everything appears fine on a web browser. I suspect that the problem might be related to the width and height ...

Error: Unable to find element with specified ID in React when utilizing axios

I am currently using axios to retrieve data from a JSON placeholder and display it by clicking a 'GET' button in a React application. I am encountering the common TypeError: document.getElementById(...) is null error, indicating that I have mispl ...

Encountering an issue with the Node.js 'crypto' module compatibility when trying to add the mongoClient in the Auth Js file for Next.js due to limitations in the edge

I recently integrated auth js into my next js project and decided to split the configuration into two files, auth.js and auth.config.js. However, when I attempted to add mongoClient to my auth.config.js for the signIn functionality, I encountered an edge ...

What is the best way to expand the parent div to accommodate the children divs?

Is there a way to make the parent div stretch to fit its children div? I attempted adding an element with clear: both; after the content, but it did not have the desired effect. Here is the HTML structure: <div id="wrapper"> <div class="lef ...

Does the submit button function even without filling in the required fields?

After creating a form with 20 fields, I encountered an issue. Out of those fields, there are 10 mandatory ones. When attempting to submit the form, only the mandatory input fields display a red border without showing an error message. Despite this, the sub ...

Is it possible to utilize context within a custom hook?

Here is a custom hook that attempts to use context inside it, but results in an error stating "Invalid hook call. Hooks can only be called inside of the body of a function component" export const useFetch = async () => { const { city, setFetchError } = ...

Test failed in React Jest because the mock function was not called as expected

I've recently uploaded a more simplified example for reference. I am struggling to get mock functions executed when they are nested within a helper function. Can't seem to figure out what the issue is. class TodoSearch extends Component { ha ...

Prevent child element from being rotated along with parent element

I am facing an issue with two div elements: a parent element that is rotated and a child element that I want to remain unaffected by the rotation of its parent. To tackle this problem, I tried rotating the child element in the opposite direction of the pa ...

What is the best way to make the buttons on my website shift downwards?

I'm currently working on making my website mobile-friendly. I've managed to stack the buttons on top of each other when the width reaches 700, but now I'm struggling to make them move down the screen. Some resources suggest using absolute po ...

Menu bar placed atop a carousel

I am currently in the process of developing a website and I have encountered an issue with the navigation bar. I have a slider that I would like to appear below the navbar but the navbar is pushing it down instead. Ideally, I want the navbar to be transpar ...

Align list item span to the center

I have encountered an issue with creating a match list using li and ul tags. Despite applying the center span to a fixed width and the team spans to the remaining space, the widths still appear different. How can I ensure that the team spans have the same ...

Rendering a React component conditionally within the same line

I'm trying to conditionally render a Home component based on a certain condition. I attempted to utilize the Inline If-Else with Conditional Operator recommended by React, as explained in this source. The code snippet I have looks like this: import ...

Utilizing MDX Component Properties in NextJS

Currently, I am in the process of developing a layout system for my upcoming app inspired by Adam Wathan's approach and his tailwind website. While I have successfully implemented it for js files, I am facing issues trying to apply the same syntax to ...

Guide to sending users back to the previous page following a successful sign-in process using NextAuth

I am currently working on a project using Next.js and incorporating NextAuth. One specific feature I am working on is a product page that includes an Add to Cart button. In the case where a user is not logged in and tries to click on Add to Cart, my goal ...