Ways to center card components (rmwc) horizontally within a React application

Within my parent component:

        {
          this.state.projectList.map(data => <Item details = {data}/>)
        }

Inside the child component Item

render() {
    return (
        <div style={{'width':'100%', 'textAlign':'center'}}>
            <Card style={{ width: '25rem', padding: '1rem', display: 'inline-block' }}>
                <CardPrimaryAction>
                    <div style={{ padding: '0 1rem 1rem 1rem' }}>
                        <Typography use="headline6" tag="h2">
                            {this.props.details.title}
                        </Typography>
                        <Typography use="body1" tag="div" theme="text-secondary-on-background">
                            {this.props.details.content}
                        </Typography>
                    </div>
                </CardPrimaryAction>
            </Card>
        </div>
    );
}

The rmwc Card component is used to display certain details.

https://i.sstatic.net/vjWZO.png

Currently, all items are stacked vertically. I aim for the layout depicted in the image on the right side, drawn in blue.

I attempted to set the wrapping div's style to

'width':'100%', 'textAlign':'center'
and made the Card itself an inline-block, but the issue remains.

Answer №1

To organize your content in the parent component, utilize the GridList component from material-ui. Specify the desired number of columns for the grid layout. Within the GridList, nest your data within GridListTile components as illustrated below:

 <div>
      <GridList cols={2} spacing={16}>
        this.state.projectList.map(dat => 
          <GridListTile item key={dat} xs={6}> 
            <Item data = {dat}/>
          <GridListTile/>
       ))}
     </GridList>
 </div>

In the child component, you can omit the parent <div> and proceed with the implementation. Remember to import any required dependencies.

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

How can I make a sticky element appear behind a different element?

https://jsfiddle.net/30suam6z/1/ .first { height: 100vh; background-color: red; position: relative; z-index: 2; /* Increase z-index to ensure it covers .test */ } .second { height: 100vh; background-color: yellow; position: relative; z- ...

Enable the image to extend beyond the boundaries of its containing div

How can I prevent a div from extending its width, allowing an image to be visible outside of it in IE8? Specifically, my div may be 200px wide while the image is 250px wide. ...

How can I ensure that an absolutely positioned element [created with React] snaps to the edge of its parent div when the page loads

I am currently developing a unique two-thumb slider bar component using React. Each thumb in the slider snaps to the closest discrete tick so that users can easily select values along a number line visually. One challenge I'm facing is that the thumbs ...

What are some strategies for managing scroll restoration when using react-router v4?

Currently facing issues with scroll positions when using the back button (history popstate) in react-router. React router v4 doesn't provide built-in scroll management as browsers now implement automatic scroll behavior. This works well in most cases, ...

What is the best way to modify the date format from the input field of type date in a reactjs application?

In my ReactJS project, I am using the input type date and it is functioning properly. However, I have a requirement to change the date format from dd/mm/yyyy to yyyy-mm-dd. I have attempted to make this change but have not found a solution yet. Here is my ...

When using Material-UI with TypeScript, an error is thrown stating that global is not defined

After running the following commands: npm install --save react react-dom @material-ui/core npm install --save-dev webpack webpack-cli typescript ts-loader @types/react @types/react-dom I transpiled main.tsx: import * as React from "react"; import * as R ...

Obtain the YouTube video identifier from a YouTube embedded link

Is there a way to extract just the YouTube ID from a given URL? https://www.youtube.com/embed/cqyziA30whE?controls=1&showinfo=0&rel=0&autoplay=0&loop=0 $(".youtube").click(function () { console.log(this.href.replace(new RegExp("em ...

What causes background-color to create gaps of white space?

How can I remove the white spaces on the right and left side of a section when applying a background-color? https://i.sstatic.net/WbVuv.png I want the background-color to fill out the entire width of the section. Should I use extra padding to solve this ...

Connecting static CSS files to Django

I am fairly new to working with Django and I am having trouble linking CSS to my project. I have included the necessary static settings in my app's configuration, which allows me to access images in the static folder without any issues. However, when ...

Why does the last-of-type selector work, but the first-of-type doesn't seem to?

The SCSS code snippet below is causing me some confusion: li { &.menu-item-type-custom { margin-bottom: 10px; a { // } &:last-of-type { margin-bottom: 40px; } &:first-of-type { margin-top: 40px; ...

discovered a total of 63 minor vulnerabilities during the npm dependencies installation process

pm WARN [email protected] is in need of typescript@* as a peer dependency, however it has not been installed. It is necessary to manually install the required peer dependencies. ...

Can the second function be modified to incorporate the first one's syntax?

export const composeValidators = (...validators) => value => validators.reduce((error, validator) => error || validator(value), undefined); export const composeAccreditionValidators = (...validators) => value => validators.reduce((error, va ...

Dynamic content with Socket.io in Node.js

I am attempting to create a scenario where nodejs triggers an event in an irc chat that causes a html page (Running on *:3000) to execute some JavaScript. However, I am facing an issue where the showDiv(); function is not being executed as expected. Curre ...

How to Safely Swap Background Images on a Website Using CSS without Affecting the Body

How can I create a background similar to this one, but using an image instead of the blue background? Take a look at I noticed that resizing the browser window doesn't affect the background. What steps should I take to achieve this effect? ...

How to use jQuery to recursively assign numbered classes to groups of div elements

I am dynamically grouping a fixed number of divs from an array (e.g. 4 in each group). The total number of .item divs retrieved from the array is uncertain... I need to recursively assign the same class to these groups of wrapped divs: <div class="wrap ...

What is the correct placement of DESC in a MySQL query when using PHP?

Here's the issue I'm facing: $query = "SELECT * FROM table ORDER BY 'timestamp' LIMIT $startAt, $perPage"; I've been attempting to sort the results in descending order (latest on top) by adding DESC to the query. However, every t ...

Uh oh! It looks like there's an error with the actions in the ToDo app. Remember, actions must be plain

I have scoured numerous blogs in search of a solution to this error, but to no avail. Can anyone shed light on why this error is occurring and how to resolve it? I am a newbie when it comes to redux. I have been following the instructions from a Youtube vi ...

Achieving cell merging in react-table can be accomplished by utilizing the appropriate

Currently, I am using react-table and have the need to combine cells in specific columns based on their content. Essentially, I want to remove the divider border between these merged cells. To give you a visual idea, this is how it currently looks like: h ...

Encountering difficulties in setting up a Next.js app due to a node_modules/node-sass error

Encountering problems while attempting to set up a new nextjs application due to node_modules/node-sass issue My goal is to create a nextjs app using the command provided in their documentation (npx create-next-app@latest) with specific configurations inc ...

Building a custom login page with axios in a react-redux application

I've encountered an issue with my login page. When I click the submit button, it does not redirect to the main Dashboard page. Check out the code below for authentication/login.js: import React, { Component } from 'react' import { Field, ...