Tips for aligning one item in the center and another item on the right with MUI v5

My goal is to center 3 navigation tabs in the middle of my page and have a dropdown on the far right for sorting. I managed to get the dropdown on the far right, but I'm having trouble perfectly centering the 3 navigation tabs inside the <Box> <Box/> component.

Here is an image illustrating what I'm trying to achieve: https://i.stack.imgur.com/RDJrD.png

You can view my code sandbox here: https://codesandbox.io/s/45159071-how-to-align-a-component-to-the-center-right-forked-0y60s3?file=/demo.js

<Grid
    container
    sx={{ marginTop: 3, marginBottom: 3, alignItems: 'center' }}
>
    <Box
        sx={{ marginRight: 'auto' }}
    >
        <Tabs
            value={activeMenuTab}
            onChange={handleChangeTab}
        >
            <Tab label="Tab 1" />
            <Tab label="Tab 2" />
            <Tab label="Tab 3" />
        </Tabs>
    </Box>
    <Grid
        item
        xs
        style={{ flexGrow: 0 }}
    >
        <FormControl
            sx={{ minWidth: 150 }}
            size="small"
        >
            <InputLabel id="sort-by">Sort By</InputLabel>
            <Select
                labelId="sort-by"
                value={selectedValue}
                label="Sort By"
                onChange={handleSorting}
            >
                <MenuItem value="oldest">Oldest</MenuItem>
            </Select>
        </FormControl>
    </Grid>
</Grid>

I came across a similar post that uses pure CSS for alignment, but I struggled to implement it with MUI components: Center one and right/left align other flexbox element

Answer №1

Check out this solution where I utilized the Box component instead of Grid. This setup mirrors the example you referenced in the post, but with a grid.

<Box
  display="grid"
  gridTemplateColumns="1fr 1fr 1fr"
  gridAutoRows="auto"
  rowGap={2}
  alignItems="center"
  marginY={3}
>
  <Box gridColumn="2">
    <Tabs centered value={'null'} onChange={handleChangeTab}>
      <Tab label="Tab 1" />
      <Tab label="Tab 2" />
      <Tab label="Tab 3" />
    </Tabs>
  </Box>
  <Box
    gridColumn={{ xs: '1 / -1', sm: '3' }}
    gridRow={{ xs: '2', sm: '1' }}
    justifySelf={{ xs: 'center', sm: 'end' }}
  >
    <FormControl sx={{ minWidth: 150 }} size="small">
      <InputLabel id="sort-by">Sort By</InputLabel>
      <Select labelId="sort-by" value="Some Value" label="Sort By" onChange={handleSorting}>
        <MenuItem value="oldest">Oldest</MenuItem>
        <MenuItem value="newest">Newest</MenuItem>
      </Select>
    </FormControl>
  </Box>
</Box>;

https://codesandbox.io/s/how-to-align-a-component-to-the-center-right-708jo1?file=/demo.js

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

What is causing the z-index to not function properly on my elements?

In my current code setup, I have positioned the hero image at the bottom with an overlay on top that contains text and a button. Additionally, there is a navigation bar with a z-index applied. However, I am facing an issue where the button for viewing my r ...

Setting dynamic image dimensions within a div element

Is there a way to display the div width in percentage even when the image is not loaded? The image sizes are mentioned in percentage and they load dynamically, making it impossible to fix their size. How can we ensure that the div maintains the correct wid ...

Icons that are clickable in ionic

I have successfully created a list card with 2 icons in each row. However, I am facing a challenge in making these icons clickable without disrupting the layout of the list. I attempted to add an ng-click to the icon element, but it did not work as expecte ...

Tips on preventing users from navigating backwards in a React Redux application

Seeking guidance on how to restrict user access after successful login in react redux. Can anyone assist me? I want to prevent users from navigating back to certain routes once they are logged in I am currently working on securing the routes to block unau ...

Issue: Configuration Error - CKEditor5 cannot be loaded for extension in Vuejs

Hey everyone, I'm currently facing an issue. I'm trying to customize a build and everything works fine in the cloned ckeditor along with the sample.html file. However, when attempting to implement the customized build in Vue 2, I encounter an err ...

What is the most effective way to retrieve IDs from Firestore and add them to the object arrays?

Currently working on a project that involves React and Firestore, I'm attempting to retrieve the Ids back into the objects array. Below is the code snippet: function grabIds() { setIsLoading(true); reference.onSnapshot((querySnapshot) => ...

Animations with absolute positioning not rendering correctly in Safari

I recently created a basic animation that involves setting an element to "position: absolute" in order to translate it. Everything functions perfectly as intended on Chrome, however, when testing it on Safari, the absolute positioning seems to be completel ...

CSS rules for organizing the stacking order of elements in the SuperFish Menu with

I've been struggling with a z-index issue on a website I'm currently managing. It seems to stem from the z-index values in the SuperFish Menu and a specific div element. Despite my attempts to apply position:relative/absolute & z-index: 99999 dec ...

Is it possible to utilize CSS rules for a non-existent div within CSS?

Can this be achieved using CSS? If .div1 is not present, enforce the following rule: .div2{ property: value; } For example, <div class="div1"> ... </div> <div class="div2"> <!-- it exists, so no action needed --> </div& ...

Struggling to make type definitions work in react-hook-form 7

Upon defining the types of my form fields, I encountered an issue where each field seems to take on all three different types. This is puzzling as I have specified 3 distinct types for my 3 different fields. What could be causing this confusion? https://i ...

Exploring Material-UI in React: A guide to integrating and utilizing breadcrumbs

Can breadcrumbs be integrated using material-ui in a reactjs-based setup? If so, how can I go about implementing them? ...

Tips for centering text in a react flexbox layout

Utilizing react-flexbox-grid in my project has led to the following layout: const SpicyMenu = (props) => ( <List> {props.foodItems.map(foodItem => <SpicyMenuItem key={foodItem.name} {...foodItem}/>)} </List> ); co ...

What is the best way to eliminate "?" from the URL while transferring data through the link component in next.js?

One method I am utilizing to pass data through link components looks like this: <div> {data.map((myData) => ( <h2> <Link href={{ pathname: `/${myData.title}`, query: { ...

Is it possible to streamline the restart process in my game and eliminate the extra unnecessary click?

This game requires you to click each image once without clicking one twice. The issue arises when trying to reset the game after all images are clicked. Currently, it requires an extra random click after all 12 images have been clicked in order to render t ...

Having trouble retrieving react environment variables from process.env?

When developing my React App, I utilized a .env file to securely store some essential API keys. However, as I attempted to access these variables using process.env, I encountered an issue where the values appeared to be set as undefined. To launch the app ...

Transforming the value property of an input field following a subsequent user interaction in React

Currently, I am facing an issue with a form that contains two input elements - one for the name (type="text") and the other for age (type="number"). The form state is managed in the parent component, with all the necessary handler methods set up. While the ...

Explore a React application that employs Firebase Authentication within a JavaScript environment

When testing my Login component, I encountered the following error: ● Test suite failed to run FIREBASE FATAL ERROR: Can't determine Firebase Database URL. Be sure to include a Project ID when calling firebase.initializeApp(). This is what I ...

"Enhance Your Slide Up/Down Menu with a Unique Hover Effect in jQuery

HTML: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="navigation_desktop"> <div class="button_desktop_01">1.0 Main Menu <div class="SlideItem ...

Instructions on setting div opacity when Overflow is set to visibleExplanation on setting opacity for div with Overflow

In my HTML code, I have a div element and an image tag stacked one on top of the other. The div is smaller than the image. I have set the overflow property to visible for the div. My query is, when I add an image to the image tag, the content that overflow ...

React - the state fluctuates

The Goal I'm Striving For: Transmitting data from child to parent. My Approach: Utilizing this.state as outlined here Having trouble summarizing the issue: Upon calling console.log(this.state) in the function where I update the state, the correct va ...