Position items at the center of the grid in material UI

I am having difficulty centering the grid items of material UI. I have tried using the justifyItems and alignItems props as mentioned in the documentation, but I'm still unable to achieve the desired result. Can anyone provide some guidance?

Below is an example of what I have tried, but the grid items seem to be aligning from the beginning instead of being centered:

const HeaderCopy: React.FC = () => {
    const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);

    const handleMenu = (event: React.MouseEvent<HTMLElement>) => {
        setAnchorEl(event.currentTarget);
    };

    const handleClose = () => {
        setAnchorEl(null);
    };
    return (
        <AppBar>
            <Grid container  alignItems="center" justifyContent="center">
                <Grid item xs={3}>
                    <img src="/public/logo.png" />
                </Grid>
                <Grid item xs={6}>
                    <Typography component="div">ALl IS WELL</Typography>
                </Grid>
                <Grid item xs={3}>
                    <IconButton
                        size="large"
                        aria-label="account of current user"
                        aria-controls="menu-appbar"
                        aria-haspopup="true"
                        onClick={handleMenu}
                        color="inherit"
                    >
                        <AccountCircle />
                    </IconButton>
                    <Menu
                        id="menu-appbar"
                        anchorEl={anchorEl}
                        anchorOrigin={{
                            vertical: "bottom",
                            horizontal: "right",
                        }}
                        keepMounted
                        transformOrigin={{
                            vertical: "top",
                            horizontal: "right",
                        }}
                        open={Boolean(anchorEl)}
                        onClose={handleClose}
                    >
                        <MenuItem>Home</MenuItem>
                        <MenuItem>My Profile</MenuItem>
                        <MenuItem>My Settings</MenuItem>
                        <MenuItem>Logout</MenuItem>
                    </Menu>
                </Grid>
            </Grid>
        </AppBar>
    );
}

Answer №1

To align the items in each Grid, center them using this code snippet:

<Grid item xs={3} display={"flex"} justifyContent="center">
Does this meet your expectations?

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

Attention: certain content may have been removed due to HTML sanitation

Hi there, I'm currently working on incorporating a search bar into a modal window by embedding HTML within the modal body. Here's how I've written the code: onClick() { const dialogRef = this.modal.alert() .size('lg' ...

The Chrome Extension is unable to recognize a portion of the URL following the # symbol in the contentscript.js code

I am currently developing an extension for a specific website that I don't have ownership of. The URLs on this site only change after a /#/. For instance, starting from the URL .../#/PF/charts/patients/419d3081-5e20-4347-a852-52b2c333ce85/summary W ...

Utilizing the onscroll feature to trigger a function within a ScrollView

I am facing an issue with an animated view where I need to execute multiple events within the onScroll property. The current onScroll implementation is as follows: onScroll={ Animated.event( [{ nativeEvent: { conten ...

The Catcomplete widget malfunctioned after the update of jQuery UI from version 1.8 to 1.12

I've encountered an issue with my code after updating to jQuery UI 1.12 var currentCategory = ""; $.widget("custom.catcomplete", $.ui.autocomplete, { _renderMenu: function (ul, items) { var self = this; $.each( ...

Switching the cursor to an image when hovering over an element is causing inconsistency in hover events triggering

Currently, I am attempting to implement an effect that changes the cursor to an image when hovering over a text element and reverts back to the normal cursor upon leaving the text element. However, this functionality is not working as expected when using R ...

vue-router incorrectly updates parameters upon reload

One question I have is related to routing in Vue.js: // routes.js { path: '/posts', name: 'Posts', component: PostsView }, { path: '/post/:post_id', name: 'PostRead', component: PostReadView, }, { pat ...

Directly within Angular, map the JSON data to an object for easy assignment

I came across this information on https://angular.io/guide/http. According to the source, in order to access properties defined in an interface, it is necessary to convert the plain object obtained from JSON into the required response type. To access pr ...

Trouble with npm installation on Windows following node update

After updating my Node.JS last night, my npm install function stopped working. I tried uninstalling and reinstalling Node, but it didn't solve the issue. My system is running on Windows 8.1 with Node version 8.9.4 and NPM version 3.3.12. The error mes ...

Packager freezing after running npm audit and the component directory is nonfunctional

Greetings, To begin with, I would like to acknowledge that this issue may have been addressed in previous posts, but despite following suggestions on Stack Overflow and GitHub, I am still facing two specific problems. Here are the issues I am encounterin ...

The select2 plugin seems to be having trouble recognizing the Li click functionality

I have a select menu that is using the select2 plugin. I am trying to add a class to the <select> element when a menu option is clicked, but it doesn't seem to be working as expected even after testing with an alert. https://jsfiddle.net/ysm4qh ...

Alexa Skills Issue: Problem with Playing AudioPlayer HLS Stream URL

I'm currently using the "AudioPlayer" feature from the "Alexa Skill Kit" to stream an HLS audio format URL. No errors are showing up from AWS Lambda or the Developer Portal. I've been testing it with Silent Echo (). Alexa can play MP3 URLs and so ...

How can you selectively export a single function from a JavaScript file?

Within my project, I have two separate modules - one written in ts and the other in js. There is a utility within the js module that needs to be accessed by the ts module. The utility service.js looks like this: module.exports = { helloFriends: functi ...

When utilizing Reactjs, accessing an element by its id becomes challenging when the element is nested within a map() function

Currently, I am encountering an issue related to reactjs. In my scenario, I have a requirement to compare the height of the screen with a specific div to determine its maximum width. The challenge lies in the fact that the particular div I need to analyz ...

Arranging a dropdown list of options in alphabetical order using Javascript

Could you assist me in sorting my select list alphabetically? Below is the code I currently have:- $.getJSON("php/countryBorders.geo.json", (data) => { $select.html(""); for (let i = 0; i < data["features"].leng ...

"Exploring ways to pass live data from the controller to the view in CodeIgniter for dynamic chart values

I currently have the following code where I am statically assigning values. Now, I need to dynamically retrieve values and display a chart. I want to populate the 'items' variable from the controller in the same format, and then display the chart ...

What etiquette should be followed when using the enter key while a file input is selected?

I have a straightforward form with a file input and a submit button. Our 508 compliance testers are encountering an issue during testing. When navigating to the FILE input field and pressing 'enter,' Internet Explorer submits the form. As far as ...

Clearing user's browsing data

I managed to successfully set up a user cookie, but I'm having trouble getting it to be destroyed or terminated when the user logs out. Any assistance on this matter would be highly appreciated! I am new to PHP and still in the learning process. Here ...

Is there a way to make text within a Div selectable even if it is positioned behind another Div with a higher z-index?

Currently grappling with some code, unfortunately cannot easily paste the problematic part here as I am unsure of its exact location. Here's the situation: there is a div containing text that needs to be selectable, however, there is another div with ...

Having trouble getting Global.css to apply on your Next.js pages/components?

Hello, I am just starting to work with nextjs. I defined a css class in my styles/globals.css file: .section-sidebar-filter-name { margin: 0 0 .5rem; } In my pages/_app.js file, I have imported the styles like this: import '../styles/globals.css&a ...

Trouble with Nested Routing in React Router Version 6: Route not Rendering

I'm facing issues nesting a path within another path in react-router-dom version 6. When I try to visit the nested argument's page (/blog/1), it shows up as a blank non-styled HTML page. However, when I add a child path to the root like /blog, it ...