What sets apart the <img> and <Image> layout attributes in NextJS?

While working on custom checkboxes, I came across a unique situation - the only way the positioning of the checkboxes aligns properly is if the classes '.init' and '.hover' are assigned to images, and the class '.done' is displayed only when assigned to an Image tag.

Check out the JSX code below:


    <div  className={styles.container}>
      <input className={styles.ckb} type="checkbox" name="vt" id="ckb"/>
      <label htmlFor="ckb">
        <img src="VT/vt-.svg" height={88} width={88} className={styles.init}/>
        <img src="VT/vt.svg" height={88} width={88} className={styles.hover} />
        <Image src="VT/vtD.svg" height={88} width={88} className={styles.done} />
       <span className={styles.lab}>: VEGE :</span>
      </label>
    </div>

And here is the corresponding CSS:


.container {
    display: block;
    position: relative;
}

.init {
    position: absolute;
    top: 0; left: 0;
}
.hover {opacity: 0;
    position: absolute;
    top: 0; left: 0;
}

.done {opacity: 0;
    position: absolute;
    top: 0; left: 0;
}

input[type='checkbox'].ckb {
opacity: 1;
cursor: pointer;
position: absolute;
}

input[type='checkbox'].ckb:hover + label .hover {
    opacity: 1;
    display: inline;
    }

input[type='checkbox'].ckb:checked + label .done {
    opacity: 1;
    display: inline;
    }

This raises the question - why does this specific setup work in this way?

Answer №1

When working with Next.js, the distinction between the standard img element and the custom Image component is important for effective image handling on a website. The img element is a basic HTML tag used to display images on a webpage, while the Image component in Next.js offers more advanced features specifically tailored for image rendering.

A significant advantage of the Image component is its ability to define the width, height, and various display options for an image. This includes setting the image source (src), providing alternative text for accessibility purposes (alt), controlling layout behavior, and adjusting quality and loading preferences.

In contrast, the traditional img element lacks these customizable properties and merely displays an image based on the specified source and alternative text attributes. Utilizing the Image component in Next.js allows for more flexibility and control over how images are presented within the layout of a webpage.

For instance:

<Image
  src="/static/image.jpg"
  alt="A description of the image"
  width={400}
  height={300}
  layout="responsive"
  quality={75}
  loading="lazy"
/>

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

Struggling to resolve a glitch in the mobile navigation that requires attention either through JavaScript or CSS coding

I'm having an issue with a mobile navigation menu I created. It's a simple hamburger icon that, when clicked, opens a fullscreen menu. The problem is that I want to prevent scrolling when the overlay is visible. I tried using this code: $(' ...

Adjust the size of the image within a designated container to decrease its proportions when there is an excess of text present

I am working on a project where I need to resize an image based on the available space within its container. When there is text in the description, the image should adjust its size accordingly without pushing the text upwards. I am using React for this pro ...

I'm having trouble with Gutters GX and GY not functioning properly in Bootstrap, HTML, and CSS

I attempted to incorporate gutters into my Bootstrap grid layout, however, they are not showing up as expected. I am following the guidelines provided on the Bootstrap documentation, but for some reason, the gutters are not appearing. <!DOCTYPE html> ...

Elegant Bootstrap 4 Carousel featuring a glimpse of the upcoming slide alongside the primary carousel item

I am in search of a straightforward Bootstrap 4 carousel that showcases a glimpse of the next slide on the right. Despite exploring similar questions, I have not found a suitable solution. The links to those questions are: 1)Bootstrap carousel reveal part ...

Error message: The CustomEvent is not properly defined in the NextJS project, causing a Reference

I encountered an error: ReferenceError: CustomEvent is not defined. I am aware that CustomEvent is only available on the client side (browser), but I am attempting to utilize this function as an event: const mouseEnterHandler = () => { eventDispatc ...

Leveraging Google Cloud Functions with Next.js (Client-Side Rendering)

Having trouble incorporating firebase cloud functions into my Next.js project, encountering an unfamiliar error. firebase-config.js const firebaseConfig = { apiKey: '~~~', authDomain: '~~', projectId: '~~~', storageBu ...

How can I ensure that my data remains properly aligned in a row with 9 columns while utilizing bootstrap?

My approach to creating a row with 9 columns involved using custom CSS to set each column's width to 11.11%, resulting in equal distribution across the row. I then applied the "custom-column" class to populate the row data: .custom-column { widt ...

Next.js is having trouble identifying the module '@types/react'

Every time I attempt to launch my Next.js app using npm run dev, an error notification pops up indicating that the necessary packages for running Next with Typescript are missing: To resolve this issue, kindly install @types/react by executing: np ...

Arranging Boxes side by side

I am struggling to align three boxes horizontally on my website, as they are currently stacking vertically. I have implemented Twitter Boostrap's 'row' class in an attempt to achieve this layout. HTML: .img-box-kai { width: 300px ...

Creating a button within a card: tips and tricks

As I work on designing web sites, I often face challenges. One of my go-to tools is bootstrap, especially when creating card elements. Here is the code I typically use for creating cards: <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/c ...

CSS - Struggling to center an element with absolute positioning in IE

I have created a layout with 3 div elements. The first parent div has a css property of position relative and is taking up the full width of the viewport. The second child div has an absolute position to cover the entire area of the parent. The third child ...

Click X button to toggle and close the dropdown menu

After successfully creating a responsive menu that toggles with a hamburger icon at 1205px resolution, I encountered an issue. Once the hamburger icon is clicked and the dropdown menu opens, there seems to be no way to close it. I am looking to add an X bu ...

Refreshing the page triggers the callback function that retrieves the checkboxes selected in a Kendo treeview component

How can I retain the selected checkboxes after refreshing the page? Is there a way to achieve this using AJAX when submitting data to a database and then reloading the page? AJAX //AJAX call for button $("#primaryTextButton").kendoButton(); va ...

The absence of the head tag in the view source is a known issue related to Next.js

I have created a Next.js app where I implemented my head tag. However, when I check the view source by right-clicking, I don't see my title and meta tags there. How can I achieve that? Even though the head tag is missing in view source, it can be fou ...

Tips for transforming a container div into a content slider

Working with Bootstrap 3, a custom div has been created as shown below: <div class="second-para"> <div class="container"> <div class="second-section"> <div class="c ...

Utilizing CSS for displaying and hiding content based on its unique identifier

Is it possible to show/hide a div using only CSS based on its ID? Showcasing this functionality in jQuery or vanilla JavaScript is straightforward, but can the same effect be achieved with pure CSS? The checkbox hack requires a specific structure to func ...

Expanding User Type to Include Extra User Data Using NextAuth.js

I encountered a dilemma where I needed to include the "profile" property in my section object to cater to different user personas in my application. However, despite retrieving the logged-in user's data from the backend and storing it in the NextAuth. ...

"Unexpected result: getInitialProps is returning a value of

I am currently using getInitialProps on _app to extract cookies and pass the values down to a component in order to adjust an element's width, but I am encountering an issue where it always returns as undefined. Below is the code snippet from the _ap ...

What steps can be taken to fix the issue of "failed to solve: process "/bin/sh -c yarn install --frozen-lockfile" while constructing a Docker file?

I am attempting to dockerize my next.js app and have encountered an error while building the Dockerfile. Below is the Dockerfile I am using: FROM node:18-alpine AS BUILD_IMAGE WORKDIR /app COPY package.json yarn.lock ./ RUN yarn install --frozen-lockfile ...

Having trouble loading Yeoman Webapp SASS

Every time I try to build a web application using 'yo webapp', I encounter a 404 Error. GET http://localhost:9000/styles/main.css 404 (Not Found) I removed compass from the project, but that shouldn't be causing the issue, correct? ...