Tips for adjusting the height of the NextJS Image tag based on the width of the screen

I'm leveraging next/image to display my image in the following way:

<Image 
 src={imageLink} 
 width="1920" 
 height="512" 
 alt="Hero Image" 
/>

Everything works well for screen widths larger than 750px.

Is there a way to adjust the height to "612" when the user accesses the site on a mobile or tablet (screen width below 750px)?

Answer №1

Embed an Image in a div and apply the specified properties to the Image:

<div className="custom-div">
   <Image src={imageUrl} layout="fill" objectFit="cover" />
</div>

Ensure that the div has a position: relative. This allows flexibility in adjusting the height/width using media queries.

Answer №2

2023 has arrived, and NextJS 13 brought about significant changes to the Image-API. Embracing the new app directory structure and naming conventions of NextJS 13, here is my approach to achieving the desired functionality:

// page.jsx

import styles from './page.module.css';
import logo from '../public/logo.png';

export default function Home() {
  return (
    <div className={styles.logoContainer}>
      <Image src={logo} alt='Argo Logo' fill />
    </div>
  );
}
/* page.module.css */

.logoContainer {
  position: relative;
  overflow: hidden;
  width: 158.57px;
  height: 30.93px;
}

@media (max-width: 500px) and (orientation: portrait) {
  .logoContainer {
    width: 79.285px;
    height: 15.465px;
  }
}

Answer №3

Embed your image within a div element and easily adjust the height and width:

<div className='relative h-96 md:h-3/4'>
     <Image className='h-full sm:h-64' 
         src="/static/images/desktop/image-header.jpg" alt="sun" 
         layout='fill' objectFit='cover' width={400} height={350} />
</div>

Answer №4

If you're working with the latest version of Next.js, you now have the ability to implement responsive sizing for images. Here's an example:

<Image
    fill
    src="/example.png"
    sizes="(max-width: 768px) 100vw,  1920px"
  />

Keep in mind that the sizes attribute will adjust the width of the image. To modify the height of the image, consider wrapping it within a div and using CSS to control the height of the parent container.

For more information, refer to the documentation: https://nextjs.org/docs/pages/api-reference/components/image#sizes

Answer №5

To customize the styling of your website, you can create a custom CSS class in your stylesheet file.

Within this class, define the properties you need such as height, padding, margins, etc.

@media (min-width: 576px) {
  .custom_class {
    max-width: 540px;
  }
}

@media (min-width: 768px) {
  .custom_class {
    max-width: 720px;
  }
}

@media (min-width: 992px) {
  .custom_class {
    max-width: 960px;
  }
}

@media (min-width: 1200px) {
  .custom_class {
    max-width: 1140px;
  }
}

Answer №6

To create a responsive design, use the Grid tag as a parent element when incorporating an Image tag. See the example below:

 <Grid width={30}>
       <Image
         onClick={() => setIsDrawerOpen(true)}
         layout="responsive"
         src={menuIcon}
         component="img"
         alt="WhatsApp logo">
         </Image>
 </Grid>

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

Activate the trigger event when the popover is activated remotely

I am facing a unique situation on my website where I have multiple popovers (pop1), (pop2), (pop3) that are triggered in sequence when the "Next" button is clicked. These popovers appear one after the other without direct access to them individually. If y ...

Ways to condense a text by using dots in the middle portion of it

How can I dynamically shorten the text within a container that varies in width? The goal is to replace the strings between the first and last words with dots so that it fits within the container. For example, Sydney - ... - Quito. It should only replace wh ...

Having issues with floats in Bootstrap?

I'm facing an issue trying to float two elements within a Bootstrap navigation bar, but for some reason it's not working. .cls1 { float: left !important; } .cls2 { float: right !important; } <link href="https://maxcdn.bootstra ...

Ways to connect a date to a partnership using prisma

In my project, I am working on establishing a many-to-many relationship between users and ranks in a database where some ranks can be assigned temporarily. The challenge I am facing is figuring out how to link the expiration timestamp of a temporary rank a ...

What is the easiest method to design an email subscription form that remains fixed on the top right corner of the screen?

Looking for advice on setting up a sleek email signup bar that remains at the top of the browser while users scroll and navigate through different pages. I am working with WordPress and have jquery already loaded, but have not yet worked with either. Up ...

Menu Drop on top of Flash player

A challenge I am currently facing is with my live event site and the list of events displayed in a mouseover menu. The issue arises when the flash streaming player remains in front of the mouseover menu, causing interference across all versions of Interne ...

"Exploring the versatility of Material UI's dropdown feature with multiple columns in React JS

Is there a way to implement a Material UI dropdown box like the one in this image using only Material UI React JS (JavaScript)? What is the best approach to utilize the backend for displaying data from both the first and second columns? Requirements: Use ...

Encountering MIME error in Vite + React app upon page reload

After deploying my React app with Vite on Netlify and configuring a backend server on Heroku, everything seemed to be running smoothly without any issues. However, I encountered a problem with two components that only return a blank white screen when reloa ...

Function wrapper intended for axios

How can I create a wrapper function for axios? I am looking to create a function that can return axios, allowing for easy substitution with another fetch API in the future. This is what I have attempted so far: import axios from 'axios' expor ...

Positioned in the middle is a collection of left-aligned divs

While many questions address similar issues, the focus is always on having all items centered (accomplished by using display:inline-block). However, my requirement is to have the last line of items floated neatly left and only center the container itself ( ...

The file '.manage.py' cannot be accessed due to error code [Errno 2], as the file or directory does not exist

Currently, I'm following a tutorial on a YouTube video. Here is the link: https://www.youtube.com/watch?v=JD-age0BPVo&t=793s While using VSC on my Mac, I encountered the following error: Sourav@Souravs-MBP Settle_Guide % python .\manage.py m ...

Tips for resolving an issue with an overflowing Uber React-Vis multicolor bar chart

I am trying to create a multi-colored bar chart using Uber's react-vis library. However, I am encountering an issue where the leftmost bar of the chart is overflowing below the YAxis instead of remaining contained to the right of it. You can view the ...

What are the steps to make a div with no content inside?

Presently, I am dealing with 2 overlapping transparent div's each containing unique buttons and functionalities in the following manner: .item1{ height:10rem; width:10rem; position:absolute; border:0.5rem solid red; background-color:trans ...

How can you change the direction of a React MUI <Menu /> component to right-to-left?

I am currently working with ReactJS and the Material UI framework. My application fully supports both right-to-left (rtl) and left-to-right (ltr) languages, and it functions perfectly in both cases. However, I have noticed that for the {component1} and { ...

Arranging the list items in a perfectly straight vertical line

Concern: I'm struggling with alignment issues on my website. I have country flags displayed using display:inline;. The issue arises when the number next to the flag exceeds single digits (e.g., 10 or more) as it affects the alignment due to the consi ...

Set up variables during instantiation or within the class declaration

Is there a preferred way to initialize class variables in ReactJS with TypeScript - in the constructor or when declaring the variable? Both methods seem to work equally well and result in the same transpiled javascript. export class MyClass extends Reac ...

Tips on placing a white background behind fa-3x (Font-awesome) icons

I am facing challenges while trying to customize the background behind my font-awesome icons. My goal is to create a white background that does not extend beyond the actual icon itself. Currently, the result looks like the image below: https://i.sstatic. ...

ways to utilize inline styling in react using javascript

When repurposing an array of React components, I am looking to modify the inline styles generated with them. How can I access and log the inline styles of a React component? UPDATE: see the code snippet below: const pieces = this.props.pieces.map((decl, ...

Tips for changing the color of text in the navbar with Bootstrap 5

I am currently learning the basics of Bootstrap for front-end web development. However, I am facing difficulty in changing the color of navbar text items to blue. I have tried several online solutions but none seem to work with the following code: <n ...

Stacking elements in perfect alignment

Seeking assistance with React/MUI as a newcomer. I am utilizing a Stack of TextFields, which are resizing effectively based on screen width. <Stack direction="row" sx={{ margin: "16px" }} > ...