Tips on achieving a responsive image layout design

Trying to implement this design using Tailwind CSS and Next.js for coding this component:

I'm facing challenges in achieving the desired grid layout with images, as they are not displaying properly.

Below is my current approach:

import Image from 'next/image';
import { WOM_1, WOM_2, WOM_3, WOM_4, WOM_5, WOM_6, WOM_7, WOM_8 } from 'public/images/index.js';

const ImageCard = ({ image, alignment, cardWidth, cardHeight }) => {
    return (
        <div className="flex flex-col items-center justify-center w-full h-full">
            <div className="flex flex-col items-center justify-center">
                {cardWidth && cardHeight ? (
                    <Image
                        src={image}
                        alt=""
                        width={cardWidth}
                        height={cardHeight}
                        layout="fixed"
                        className={`object-cover object-center overflow-hidden`}
                    />
                ) : (
                    <Image
                        src={image}
                        alt=""
                        width={193}
                        height={179}
                        layout="fixed"
                        className={`object-cover object-center `}
                    />
                )}
            </div>
        </div>


    );
};

const WallOfMemories = () => {
    const imageInfo1 = [
        { image: WOM_1, alignment: '0px' },
        { image: WOM_2, alignment: '-20px' },
        { image: WOM_3, cardWidth: '193px', cardHeight: '179px', alignment: '20px' },
        { image: WOM_4, alignment: '-20px' },
    ];
    const imageInfo2 = [
        { image: WOM_5, alignment: '0px' },
        { image: WOM_6, cardWidth: '193px', cardHeight: '179px',alignment: '20px' },
        { image: WOM_7, alignment: '-20px' },
        { image: WOM_8, alignment: '0px' },
    ];
    // attempting to display images in a 4x2 grid at the center of the page
    return (
        <div className="flex flex-col items-center justify-center w-full h-full">
            <div className="flex flex-row items-center justify-center w-full h-full">
                {imageInfo1.map((image, index) => (
                    <ImageCard
                        key={index}
                        image={image.image}
                        alignment={image.alignment}
                        cardWidth={image.cardWidth}
                        cardHeight={image.cardHeight}
                    />
                ))}
            </div>
            <div className="flex flex-row items-center justify-center w-full h-full">
                {imageInfo2.map((image, index) => (
                    <ImageCard
                        key={index}
                        image={image.image}
                        alignment={image.alignment}
                        cardWidth={image.cardWidth}
                        cardHeight={image.cardHeight}
                    />
                ))}
            </div>
        </div>
    );
};

export default WallOfMemories;

Looking for guidance on what I might be missing? How can I replicate the image layout effectively?

Answer №1

For the Image component, make sure to add the class name object-scale-down:

<Image
                       src={image}
                        alt=""
                        width={cardWidth}
                        height={cardHeight}
                        layout="fixed"
                        className={`object-scale-down object-center overflow-hidden`}
                    />

The object-cover class covers the screen at all times without scaling, but rather shrinks the image.

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

Unable to update to the most recent version of React-Bootstrap

I'm facing an issue while trying to upgrade to the newest version of react-bootstrap. When I run: npm install --save react-bootstrap The following message appears: npm notice created a lockfile as package-lock.json. You should commit this file. npm ...

How to send cross-domain AJAX requests to RESTful web services using jQuery?

I have been utilizing Jquery Ajax calls to access RESTful webservices in the following manner. The web service is being hosted on a different domain. $.ajax({ type: "GET", url: "url for the different domain hosting", crossDomain: true, ...

Keep moving the box back and forth by pressing the left and right buttons continuously

Looking to continuously move the .popup class left and right by clicking buttons for left and right movement. Here is the js fiddle link for reference. This is the HTML code structure: <button class="right">Right</button> <button class="l ...

Finding the correct value in Ajax is proving to be a challenge

In my development of a doctor management system, I am encountering an issue with updating the date field based on changes in the selected doctor. The system includes three form fields: department, doctor, and doctor_time. Through AJAX, I have successfully ...

Iterate over asynchronous calls

I am currently working with a code snippet that loops through an Object: for(var x in block){ sendTextMessage(block[x].text, sender, function(callback){ //increment for? }) } During each iteration, I need to make a request (send a Faceboo ...

Using Typescript does not generate any errors when indexing an object with brackets

One interesting thing I've noticed about TypeScript is that it allows me to use bracket notation to access an object via index, even when it only has keys. For example: interface testObject { name: string; id: number; } let first: testObject ...

Extracting a precise data point stored in Mongo database

I have been struggling to extract a specific value from my MongoDB database in node.js. I have tried using both find() and findOne(), but I keep receiving an object-like output in the console. Here is the code snippet: const mongoose = require('mongoo ...

How can I combine my Authorization Header with httpLink in Apollo Client and Next Js?

https://i.sstatic.net/S7ASb.pngI've been developing an application that requires authentication to create posts. To achieve this, I created two objects - httpLink (connecting to the backend) and authLink (handling the passing of headers in the form of ...

Getting JSON data from an API using $.ajax

Currently, I am working on creating a random quote machine. To start off, I wrote the following HTML code to outline the necessary elements: <div id="quoteDisplay"> <h1 id="quote">Quote</h1> <h2 id="author">- Author</h2> ...

Firebase Error: The property 'ac' is not defined and cannot be read

When authenticating on my web app, I utilize this straightforward code: firebase.auth().signInWithCustomToken(token).catch(function(error) { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; // ... }); Howev ...

What is the best way to transfer information from a parsed CSV file into select dropdown boxes?

I utilized JavaScript to parse the CSV file. Here is an example of a CSV: Year,Model,Mileage,Color,Vin,Image 2012,BUICK LACROSSE FWD V6 FFV 4D SEDAN LEATHER,113046,BROWN,1G4GC5E34CF177752,https:imagelink Below is the code used for parsing: $(document). ...

Is there a way to determine the distance in miles and feet between various sets of latitude and longitude coordinates?

I am working with an array of latitude and longitude coordinates and I am looking to use JavaScript or Typescript to calculate the distance in miles and feet between these points. const latsLngs = [ { lat: 40.78340415946297, lng: -73.971427388 ...

Once invoked by an ajax request, the $().ready function is executed

The functionality of this code is flawless when running on its own. However, once I make an ajax call to it, the code fails to execute. I suspect that the issue lies within $().ready, but I haven't yet identified a suitable replacement. Any suggestio ...

Which is better: Passing a large amount of data received from getStaticProps through prop drilling in Next.js or utilizing the Context API

I am currently working on a Next.js project for my school assignment, where I am creating a mini forum with Incremental Static Regeneration. However, I have encountered an issue related to prop drilling in the app architecture. The flow of props in my proj ...

Error: The "require" function is not recognized within the Rollup bundle JavaScript file

Recently, I've been utilizing rollup to bundle my JavaScript files designed for integration with three.js. It got me thinking about minimizing and bundling the three.js files using rollup.js as well. To achieve this, I used npm and executed the comman ...

Waiting for the outcome of a function that calls itself repeatedly

The solve function provided here is designed to sort elements in an array based on their frequency and value. However, when dealing with longer input arrays, it encounters a 'Cannot read property' error which disrupts the sorting process. To add ...

When trying to import axios from the 'axios.js' file in the 'lib' directory, a SyntaxError was encountered with the message: Unexpected identifier

My server.ts is causing issues. What am I doing wrong? const express = require('express'); const bodyParser = require('body-parser'); const cors = require('cors'); const morgan = require('morgan'); const axios = requ ...

`Save user edits on the webpage using Electron`

I am encountering an issue with my electron app. I use the window.loadUrl() method to navigate between pages. Some of these pages require users to input data that needs to be saved. The problem arises when a user enters information, moves to another page ...

The getTotalLength() method in SVG may not provide an accurate size measurement when dealing with non-scaling-stroke. To obtain the correct scale of

In my current project, I am utilizing JavaScript to determine the length of a path and apply half of that length to the stroke-DashArray. However, I have encountered an issue as I am using vector-effect="non-scaling-stroke" in order to maintain a consisten ...

Unresolved styles in React component linked to styles.css file

As I dive into creating a registration page in ReactJS, I encounter a frustrating issue with my styles not applying correctly from the styles.css file. Let's take a look at my RegisterPage.jsx component: export default function RegisterPage() { ret ...