I am working with an array of image objects called imageList and I am trying to implement a specific design layout using flex

I have an array of objects called imageList, and I need to achieve a specific pattern using flexbox. How can we utilize flex-grow for the images? https://i.sstatic.net/RHqfK.jpg

I have been able to create this pattern using grid (...pseudo(':nth-child(1)', { gridRowStart: 1, gridRowEnd: 3 }),) but struggling to do so with flexbox. Please assist me in achieving this layout using flexbox.

The current behavior is illustrated here: https://i.sstatic.net/mNxZG.jpg

I have provided the tsx and css files below:

Below is my code App.tsx

import React from "react";
import "./App.css";

const App = () => {
const imageList = [
{
  id: "1",
  url: "https://images.unsplash.com/photo-1558979158-65a1eaa08691?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80",
},
{
  id: "2",
  url: "https://images.unsplash.com/photo-1572276596237-5db2c3e16c5d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80",
},
{
  id: "3",
  url: "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1353&q=80",
},
{
  id: "4",
  url: "https://images.unsplash.com/photo-1551009175-8a68da93d5f9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1351&q=80",
},
{
  id: "5",
  url: "https://images.unsplash.com/photo-1549880338-65ddcdfd017b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80",
},
{
  id: "6",
  url: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883334/person-1_rfzshl.jpg",
},
{
  id: "7",
  url: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883409/person-2_np9x5l.jpg",
},
{
  id: "8",
  url: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883417/person-3_ipa0mj.jpg",
},
];

return (
<div className="mainWrapper">
  {imageList.map((image: any, index) => (
    <div key={image.id} className="imageWrapper">
      <img src={image.url} alt={image.url} width="100px" />
      <h4 className="heading">{image.id}</h4>
    </div>
  ))}
</div>
);
};

export default App;

Please find the App.css below:

*{
margin: 60;
padding: 60;
}
.mainWrapper{
display: flex;
flex-wrap: wrap;
gap: 30px;
margin-left: 160px;
margin-right: 160px;
}

.imageWrapper{
position: relative;
flex: 1 0 21%;
height: 200px;
}

img{
min-width: 100%; 
height: 100%;
border-radius: 20px;
}

.heading{
position: absolute; 
top: 0; 
color: white;
}

Answer №1

One approach is to set the main flex direction as row, with each column being a flex container with a column direction.

If you need to resize images and maintain aspect ratio, consider using object-fit (MDN) or specify min-height / max-height properties.

To adjust the relative size of image wrapper flex items, utilize the flex shorthand property or set min-height in percentages.

const imageList = [
  {
    id: "1",
    url: "https://images.unsplash.com/photo-1558979158-65a1eaa08691?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80",
  },
  {
    id: "2",
    url: "https://images.unsplash.com/photo-1572276596237-5db2c3e16c5d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80",
  },
  {
    id: "3",
    url: "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1353&q=80",
  },
  ...
];

class Example extends React.Component {
    render() {
      return (
        <div className="mainWrapper">
          {imageList.map((image, index) => {
            if (index % 2 !== 0) { return null; }
            return (
              ...
            )
          })}
        </div>
      );
    }
}

ReactDOM.render(
    <Example />,
    document.getElementById("root")
);
.mainWrapper {
  display: flex;
  flex-flow: row wrap;
  justify-content: stretch;
  gap: 20px;
}
...
}
<div id="root"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

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

Is there a way to protect the privacy of State variables within a Flux Store?

Currently, I've implemented my own version of the Flux pattern in a small scale in order to deepen my understanding of the concept. So far, it's been working well and I've been gaining valuable insights! However, I've encountered a chal ...

What is the location where UIWebView saves HTML5 offline files?

I'm curious about the location where HTML5 offline content is stored when saved from a UIWebView in an iOS app. Is it saved locally within my app's access, or somewhere else? ...

Are there any negative aspects to providing a state setter to a helper function?

Exploring React for the first time and experimenting with some basic helper functions. Any potential downsides to passing a state setter to a helper function? Below is a simplified example: App.js import { useState } from 'react'; import increme ...

What is the best way to retrieve information from a data set?

After borrowing some basic HTML, CSS, and JavaScript code from CodePen, I ran into an issue while attempting to convert it to React. The error message says that it cannot read properties of null (specifically 'dataset'). Here is the JavaScript c ...

Having trouble sending data to my server using a POST request with Axios

Below is the React code I've written for form submission: const handleSubmit = (e) => { e.preventDefault(); console.log('item:', item); Axios.post('http://<MY_SERVER>/item/add', {name:item}) .then(resp ...

The issue of transform scale not functioning properly in conjunction with background clip and gradients in CSS

Looking to transform a div with the transform: scale(-1,1) property while using background-clip: text on the text within it. However, this causes the text to disappear. Here's what I've attempted: .reverse { transform: scale(-1, 1); } .gr ...

Tips for tackling drag and drop functionality with only HTML, CSS, and JavaScript

Currently, I am faced with a challenge involving drag and drop TEST. Observing the image provided below, you will notice 3 columns each containing 3 small boxes of varying colors. These boxes are draggable, meaning they can be moved between columns follow ...

Tips for automatically filling out a div class form:

I currently have an Autoresponder email form featured on my webpage. Here is a snippet of the code for the section where customers enter their email: <div id = "af-form-45" class = "af-form" > <div id = "af-body-45" class = "af-body af-standa ...

Tips for creating content with ellipsis after every 5 characters

I am new to coding and I need some assistance. I want to truncate text in my navigation bar if it exceeds 5 characters by displaying the first 5 characters followed by an ellipsis. This is to prevent display issues in my nav menu. For example: Input: T ...

integrating React frontend with Node.js RESTful API

When trying to connect my react frontend to a rest API in my node js backend folder, I encounter an issue after sending a post request resulting in the following errors: const handleSubmitData = async (event) => { try { const { user } = pr ...

What is the best way to centrally align a Card while keeping the text left-aligned?

New to coding and need some guidance. I'm attempting to create a card that is not the full width of the window. I have specified a cardStyle as shown below. cardStyle:{ width: '95vw' align? : 'center?' textAlign? : &a ...

Using React-Bootstrap to create smaller user inputs

Is there a way to reduce the size of the Input element? By default, the syntax looks like this: <Input type='text' value='' placeholder='Enter Entity'/> Using bsSize='xsmall' does not work for making the inp ...

Angular 6 and Typescript: How to Map Objects in Arrays within Arrays

We currently have two arrays named speisekarte (consisting of 10 objects) and essensplan (containing 8 objects). const speisekarte = [ { id: 11, name: 'Kabeljaufilet', price: 3.55, type: 'with fish' }, { id: 12, name: 'Spaghet ...

Could you provide guidance on how to toggle the visibility of a div using the click event of a button or link located on a different HTML page in Angular?

Is there a way to change the visibility of a div on a different page when a button or link is clicked? For example, I have a button on the "main.html" page that, when clicked, should display a hidden div on the "header.html" page. How can I achieve this? ...

Creating a dynamic anchor scrolling effect within a dropdown select menu

Having trouble achieving smooth scrolling with a select option element, only works with a link. Any suggestions? Check out the jsfiddle demo to see what I mean! $(function() { $('a[href*=#]:not([href=#])').click(function() { if (location. ...

Managing errors and error codes in React/Redux applications

I am currently exploring the most effective approach for managing errors, particularly when deciding between error codes and an errors object in response from my API. As I dive into building a multi-part form using react, each time a user progresses to th ...

A comprehensive guide on properly obtaining user details in React with Redux and OIDC

Although I've dabbled in OIDC before, I wouldn't consider myself an expert. Currently, I'm trying to integrate OIDC into a react app using oidc-client-js and redux-oidc libraries, following the redux-oidc-example as a guide. Encountering th ...

Can a JavaScript function be used with images to operate across multiple elements?

How can I make a function work on multiple elements? let image = document.getElementById("opacityLink"); image.onmouseover = function() { image.style = "opacity:0.9"; }; image.onmouseout = function() { image.style = "opacity:1"; }; <div class=" ...

Adjustable block with excess content

Forgive me if this question seems basic, but I'm not very familiar with CSS. I have a table with two columns that need to be equally sized and responsive when the page is resized. The header isn't an issue since it only contains a few words in ...

Is there a way in CSS to apply multiple colors to individual letters within a word?

Is it possible to add random colors to every pixel of a letter or multiple colors to a letter using CSS? Check out this example. ...