Struggling to align elements in React/JS/M?

My challenge is aligning the elements in the middle of my page. I aim to have 3 elements per row, with n rows depending on the number of "VideoBox" components. It's crucial that these elements fit within the lettering of the P R O F E S S I O N A L P R O J E C T S title (refer to the image below).

As someone who struggles with JS/TS/CSS/React, any guidance would be greatly appreciated!

I've provided all the relevant code snippets below:

/*VideoFile.tsx*/
import React from 'react';
import './VideoFile.css'

export default function VideoBox({ name, image, link, release, popularity }) {
  return (
    <div>
        <div className="box">
            <img src={image} alt={name} />
        </div>
        <div className ="img-name">{name}</div>
    </div>
  );
};

export { VideoBox };
...Continued with other code snippets and explanations.

To visualize the current layout, here is an image for reference:

Click here to view the image (placeholder pictures shown)

Answer №1

To enhance the user experience, I prioritize leveraging MUI while minimizing the reliance on CSS styles until the foundational elements are in place and functioning as desired.

One suggestion is to transform your one-dimensional array of video files into a new two-dimensional grid (3 x N).

import { Grid, Container, Paper, Typography } from '@mui/material';

const testUrl =
  'https://fastly.picsum.photos/id/237/200/200.jpg?hmac=zHUGikXUDyLCCmvyww1izLK3R3k8oRYBRiTizZEdyfI';

// Sample data
const videoFiles = [
  {name: 'Awesome Adventure', imageUrl: testUrl, link: '#', release: '2023-01-01', popularity: 80},
  {name: 'Fantastic Journey', imageUrl: testUrl, link: '#', release: '2023-02-15', popularity: 92},
  {name: 'Epic Exploration', imageUrl: testUrl, link: '#', release: '2023-03-21', popularity: 65},
  {name: 'Amazing Discovery', imageUrl: testUrl, link: '#', release: '2023-04-10', popularity: 87},
  {name: 'Cool Quest', imageUrl: testUrl, link: '#', release: '2023-05-05', popularity: 75},
  {name: 'Unbelievable Voyage', imageUrl: testUrl, link: '#', release: '2023-06-18', popularity: 89},
  {name: 'Incredible Expedition', imageUrl: testUrl, link: '#', release: '2023-07-30', popularity: 78},
  {name: 'Exciting Trek', imageUrl: testUrl, link: '#', release: '2023-08-22', popularity: 94},
  {name: 'Breathtaking Safari', imageUrl: testUrl, link: '#', release: '2023-09-14', popularity: 82},
  {name: 'Adventurous Safari', imageUrl: testUrl, link: '#', release: '2023-10-09', popularity: 88},
];

const arrayToGrid = (arr, elementsPerRow) => {
  const result = [];
  for (let i = 0; i < arr.length; i += elementsPerRow) {
    result.push(arr.slice(i, i + elementsPerRow));
  }
  return result;
};

const videoGrid = arrayToGrid(videoFiles, 3);

// Component for displaying Video data with details
const VideoBox = ({ name, imageUrl }) => (
  <Paper
    elevation={3}
    style={{
      padding: 16,
      textAlign: 'center',
      display: 'flex',
      flexDirection: 'column',
    }}
  >
    <img
      src={imageUrl}
      alt={name}
      style={{ maxWidth: '100%', height: 'auto', marginBottom: 8 }}
    />
    <Typography variant='subtitle1'>{name}</Typography>
  </Paper>
);

// Main component rendering the grid
const Home = () => (
  <Container maxWidth='md' style={{ marginTop: 16 }}>
    <Grid container spacing={3}>
      {videoGrid.map((row) =>
        row.map((vidData, idx) => (
          <Grid key={idx} item xs={12} sm={4}>
            <VideoBox {...vidData} />
          </Grid>
        ))
      )}
    </Grid>
  </Container>
);

export default Home;

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 modify a specific item within a useState Array in Reactjs?

Having a useState hook that stores data in the following structure: const [orderData, setOrderData] = useState({ demoData1: '', demoData2: '', demoData3: '', demoArrayData: [{itemName: '', itemNumber: ...

What is the best way to apply attributes to all titles throughout a webpage?

My goal is to locate all elements on the page that have a title attribute and add a new attribute to each of them. For example: <div title='something 1'></div> <p>Test<div title='something 2'></div></p ...

using a tag in flex can disrupt the arrangement of the box's design

What could be the reason for this issue? Is it possible to make the hyperlink appear inline with the rest of the text? .test { padding: 25px; display: flex; height: 100%; text-align: center; font-size: 25px; font-weight: 600; ...

Tips for adding a "Select All" feature to a dropdown list?

Currently, I have a dropdown list with a filter for IN and OUT values. The functionality is working as expected: <select class="form-select" style="max-width: 100px" [ngModel]="selectedBrand" (ngModelChange)="onChangeT ...

AngularJS is unable to properly display (parse) data fetched using $http.get from Laravel5

Working on my SPA project, I have integrated Laravel 5 for database management and used AngularJS for the front-end. All Angular files are stored in the public folder. Instead of displaying a list of users when visiting localhost, it shows: {{ user1.name ...

Automatically changing the color of the navigation bar text

I am experiencing an issue with the navigation bar on my webpage. Currently, it is styled with white text against a dark background color using the following CSS code snippet: a{ color: white; text-decoration:none; font-weight:bold; font-s ...

Center-align the text in the navigation bar

Is there a way to center the text within my navigation and ensure it remains centered across all resolutions (left at 1920x1080, centered at 1420)? I understand that much of this code is inefficient and not working correctly, but for now I just want to fi ...

Ways to prevent modal from flickering during event changes

I'm struggling with a current issue and need help identifying the cause and finding a solution. The problem arises from having a nested array of Questions, where I display a Modal onClick to show Sub questions. However, when clicking on the Sub Quest ...

Quickest method for skimming through an extremely lengthy document beginning at any specified line X

In my current project, there is a text file that is written to by a python program and read by another program to display on a web browser. JavaScript handles the reading process at the moment, but I am considering moving this functionality to python. The ...

Is it possible to install the lib ldap-client module in node.js?

I'm having trouble installing the lib ldap-client package in Node.js. To try and solve this issue, I consulted the following page: https://github.com/nodejs/node-gyp. In an attempt to fix the problem, I have installed python, node-gyp, and Visual St ...

How can I pass the content of a pug input element to a JavaScript function?

Attempting to manipulate data on a database using HTML buttons and encountering an issue when trying to insert data. The setup involves a Pug page called by Node Express, which generally functions well until the insertion process. Here is a snippet from th ...

Custom Sizing for Bootstrap Modals

I need assistance with creating a dynamic bootstrap modal that adjusts its width based on the content and includes a vertical scrollbar when needed. Although the vertical scrollbar is functioning correctly, the horizontal width appears to be static. C ...

Learning to implement forwardRef in MenuItem from Material-UI

Encountering an error when pressing Select due to returning MenuItem in Array.map. Code const MenuItems: React.FC<{ items: number[] }> = (props) => { const { items } = props; return ( <> {items.map((i) => { return ( ...

Node.js async.each triggers the error message "Callback has already been invoked"

I seem to be facing a problem that I can't pinpoint despite searching for mistakes extensively. The issue arises when async.each throws a "Callback was already called" error. Below is the code snippet where I encounter this problem: async.each(this. ...

tips for exporting database information to an excel file with the help of ajax request and javascript

Recently, I built an application using NDWS with sapui5 _javascript. Within the application, there is a table that contains data synced with the server's database. My goal is to retrieve this data from the table and export it to an Excel document. Her ...

What is the process for extracting the time value from a jQuery timepicker?

I have been attempting to retrieve values from my time picker when a selection is made, but I am not seeing any values returned nor displayed in my HTML timepicker input field. Despite trying various methods, none have proven successful thus far. Method ...

Rearranging information within a JSON structure

Here is a sample of Javascript object and code to consider: Javascript Object { "thing":{ "data":"some data", "thumb":"some data", "data1":"some data", "data2":"some data", "data3":"some data", }, "extra1":[ ...

Adding flair to a fresh element and then removing it upon its inception

I'm working with a JavaScript code that creates a new element when a button is clicked. I have a question about it. Here's the JavaScript code snippet: var comment = document.querySelector("#AddComment"); var req = new XMLHttpRequest(); if(comm ...

The word "yargs" will not activate a command within a script

I have a NodeJs script that requires parsing command line arguments. Here is the code I have written: import yargs from "yargs"; import { hideBin } from 'yargs/helpers'; //.... yargs(hideBin(process.argv)).command('--add-item ...

Create several designated perspectives from a template

I'm currently working with ui-router and have set up a parent state that includes several child states as shown below: .state('portfolio.patent', { url: '/:patentId', controller: 'patentItemCtrl', controllerA ...