Creating a Pinterest-style grid layout for dynamic images in a React application using CSS Grid

I am attempting to use CSS grid to create a gallery for images, similar to Pinterest. I want pictures of different heights to be placed next to each other and fill the empty space. However, all the examples I have seen involve adding different classes to each picture based on their desired height. I want to dynamically add pictures from a database.

This is how I approached it: My gallery component:



<div className=‘gallery’>
<img className=‘image’> </img>
<img className=‘image’> </img>
<img className=‘image’> </img>
<img className=‘image’> </img>
</div>



And my CSS file:

.gallery{
max-width: 80vh;
display: grid;
grid-template-columns:repeat(3,1fr);
}

.image{
max-width: 200px;
height: 100%;
object-fit: cover;
}

With this setup, the smaller pictures are in one row and the larger pictures in another, but I need them to be randomized.

Is there a way to achieve this without assigning different classes to each picture?

Answer №2

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

Guide on setting up a MERN stack with a multi-tenant architecture

After successfully creating a full stack e-commerce website using React, Node, and MongoDB, I am now looking to deploy it with a multi-tenant architecture. Each store owner should have their own domain name. The Admin Dashboard will include various APIs s ...

What is the best way to integrate JSS styles into a React application that is using Material UI

I have multiple .js files that utilize { makeStyles } from "@material-ui/core/styles"; to create jss. I am using className={classes.myClass} and then calling it with const classes = useStyles();. This is a common setup, but the issue arises when I need to ...

CSS: Position an element based on the size of the screen

I'm currently developing a program that will dynamically resize elements based on the viewer's screen size. The program is being built using JSP/SQL/XHTML/CSS, and I have a couple of queries. Is there a way to select a CSS file by storing the sc ...

Ways to remove the highlighting from a selected list item using CSS/Javascript

I have a problem with a list of images and keywords that act as selections for users. When a user clicks on a selection, it highlights the corresponding image using CSS shadow. However, I am trying to figure out how to deactivate this highlight later on, e ...

Confused about arranging a list in React.js?

I have a list named theme that I need to sort before sending it to the backend, but I'm facing challenges in doing so. I have indicated where I want the sorting to take place and I would like to retain the same variable name. How can I effectively sor ...

Personalizing the expansion panel in Material UI by customizing and overriding the default styles

Looking to customize the expansion panel in Material UI, I've noticed that the data I want to display takes up too much space due to the default styling of the expansion panel. <ExpansionPanel defaultExpanded={isCurrentInning}> <Expansio ...

Unable to change the color of ul.menu

I've been experimenting with different methods but I can't seem to change the color, there must be something I'm missing! How can I update the text color for all the lower links in the footer section? Here is my HTML code: <!-- Footer ...

I am attempting to create a presentation featuring the identical title

How can I enhance this code by turning it into a slideshow with additional images while maintaining the h1 and h2 tags within it? Here's the existing code displaying an image with titles overlayed on top: <header class="header-image"> <d ...

Building a stylish table using Material UI in ReactJS

I've been working on creating a dashboard using the Material UI minimized table. You can check out the documentation here: Material UI Tables However, I've encountered an issue that has me stuck for the past 5 hours. While I was able to successf ...

Adjusting the background color of the custom input range thumb when the input is disabled

I've customized the input thumb on my range slider, and I'm looking to change its color when it's disabled. I attempted adding a class to the thumb like this: input[type=range]::-webkit-slider-thumb.disabled and also tried adding the disa ...

Detecting the existence of a scrollbar within the document object model using JavaScript - a guide

Could someone provide a JavaScript syntax for detecting the presence of a scrollbar in the DOM by measuring the body height and window height? ...

Error: The function `map` cannot be applied to the components

My issue involves using the same components in two different locations, where one isn't functioning properly. The error is occurring in Board.js: TypeError: components.map is not a function const Board = () => { const [components, ...

Select a single card to expand from a collection of cards

I am currently using material ui Card to showcase a group of cards. Here is my current state: state = { items: [], anchortEl: null, expand: false } The "expand" property is used as a flag to indicate whether a card has been expanded or not. Be ...

The CSS stylesheet is not being applied to the components in Next.js

I am currently facing an issue with styling my React/Next component. Despite trying to apply a stylesheet to enhance its appearance, the CSS doesn't seem to be taking effect. Could someone please provide guidance on how I can resolve this? I have att ...

challenges arising from using the css overflow: hidden attribute

Struggling with a popup displaying issue caused by an overflow: hidden property on the containing div. The background graphics of the popup are crossing over due to this setting, leading to a display problem where the width is cut off at the overflow bound ...

I encountered an issue with socket.io on my node server where the connection was unsuccessful during deployment, despite working perfectly

Issue After setting up socket.io on a Node server following the guidance from both here and the socket.io documentation, the connection is successful when running a client on localhost. However, the connection is not successful when running a remote-hoste ...

Is it possible to utilize standard Bootstrap toast in React?

Struggling to grasp the concept of using toast, I am turning to this platform for assistance. Are there any advantages to using react-bootstrap as opposed to traditional bootstrap? I'm beginning to wonder if I will be restricted in utilizing all the f ...

CSS: Design a sleek container for the QR code scanner

As I work on a website that requires a QR Code scan function, the interface specifications are quite precise. The camera must occupy the full screen, with a transparent black layer covering it. However, at the center, there should be an unshielded square ...

Ways to verify the functionality of this specific custom hook (useRef)

Can anyone help me figure out how to pass the useRef as a parameter for testing this custom hook that uses ElementRef from React? import { MutableRefObject, useEffect, useState } from "react" export default function useNearScreen(elementRef: ...

What is the best way to incorporate a feature that flips all choices in react-select?

I'm working with a multi-option Select component from react-select, and I need to add a special option that will invert the selected options into unselected ones, and vice versa. When this 'Invert selection' option is chosen, I don't w ...