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 error message "Module '@reduxjs/toolkit' or its type declarations are not found" to appear?

Although I have a good understanding of React-Redux, I decided to delve into TypeScript for further practice. Following the recommended approach from the react-redux team, I created a new project using the TS template: "npx degit reduxjs/redux-templa ...

Error encountered: Unexpected identifier in Reactjs code

I created a new application using create-react-app and encountered an Uncaught SyntaxError: Unexpected identifier for this particular line: import React, { Component } from 'react'; within the file public/scripts/app.js: 'use strict' ...

Best practices for dynamically rendering a React component within another React component when a button is clicked?

As I navigate my way through learning React and working on a Nextjs application, I’ve encountered an issue with rendering a React component every time a button is clicked. Despite inspecting the page and confirming that the component is being returned ...

Customize the appearance of parent components based on the content of their child elements

I am currently working on a component that contains multiple instances, each with its own unique internal component (acting as a modal wrapper). I need to customize the size of each modal instance independently. How can I achieve this customization when th ...

What are some ways to optimize the use of the jquery.mCustomScrollbar.js script?

I have a myriad of inquiries and would greatly appreciate your assistance in addressing them. Despite spending countless hours attempting to solve the issue independently, I have been unsuccessful. One question that plagues me is why, when using Google Ch ...

Tips for creating td/th elements with a consistent width?

I am currently working on a website where I need to create a table with fixed width td tags, rather than the entire table having a fixed width. This is necessary because I will be dynamically showing and hiding columns using jQuery, and also using a jQuery ...

Improved method for linking two enums with similar appearances

Currently, I use two enums as shown: enum Tab { Approved = "Approved", Pending = "Pending", Sold = "Sold", } enum ProductStatus { Approved = "Approved", Pending = "Pending", Sold = "Sold&q ...

react native ScrollView items with uniform padding at the top and bottom

I'm currently working on a scroll component that resembles a gallery of images. My goal is to center the images based on the device height with equal padding on both the top and bottom. However, I'm facing an issue where the top padding does not ...

Changing the height of a Bootstrap 3 row without using less variables

Can someone please explain how to adjust the .row width in Bootstrap so that the col grid still functions correctly, but certain .rows are tighter than default? Thank you. ...

Fixing the department display list in React Hook: A step-by-step guide

{ roomDept.map((item, index) => ( <div key={index} className="flex flex-col pb-2 items-center"> <div className="flex pb-2 w-full"> <SelectPick ...

The functionality of ellipsis, which consists of three dots, allows the text to expand

I am trying to implement a feature where the extra text is represented by three dots (...) as an ellipsis, and upon clicking the dots, the text should expand and contract. However, the current code only contracts the text and does not expand it upon clicki ...

Steps for verifying the state update following an API response

I am currently performing unit testing using jest and enzyme on my ReactJS code. I have a main functional component called "TrashDisplayFiles" in which there is a method named "getDeletedData". This method makes an API call to set the TrashFileState based ...

Fade in a CSS class using jQuery

I'm attempting to incorporate a basic fadeIn() CSS effect on specific objects that are tagged with the "button" class. My goal is to have the "hoverbutton" class fade in when the object is hovered over, and then fade out when the cursor moves away fro ...

Is the code generated by Webflow.com functional and practical?

Recently, I have discovered the wonders of www.webflow.com for creating an admin layout. It excels in generating a flexbox layout based on my PSD design without requiring me to manually code with Gulp. My plan is to further simplify the process by breaki ...

Hover or click on HTML input type using CSS in your webpage

I am currently working on adding hover effects to my elements. I have successfully added a hover effect to the outer list item, but I am wondering how I can achieve the same effect on the <input> element instead of the <li>. ul.slides li:hov ...

Steps to turn off fancybox for mobile and show the full image instead

In this scenario... I am seeking a way to deactivate fancybox functionality on mobile devices. I have already discovered one method to disable it... And now I want to directly show the large image in the a href="xxx_large.jpg" instead of using the img src= ...

Align the button at the center of the carousel

While working on a personal HTML/CSS/Bootstrap 5 project as a self-taught learner, I have encountered some beginner doubts. My challenge is to ensure that the site remains responsive across various devices such as mobile and tablet. Specifically, I am stru ...

What are some creative ways to design drop-down menus within an email?

The snippet of code I am currently using lacks the ability to format text, and it does not allow content below dropdown items to shift down properly in an email. <select> <option value="first">First-time users of the Vanguard Events app:&l ...

Displaying ISO date format within a date input field using React

Currently, I am working on a project where I am editing records retrieved through an API. Within the data, there are two fields that represent dates. The format of the date data from the API is in "2021-07-30T20:34:40.545Z", whereas the input field display ...

Leveraging ajax for showcasing page content in a floating div container

I am in need of creating a button on my page that, when clicked, will display a floating div above the page and load another internal page. I believe this can be achieved using Ajax, but I have no experience with it and don't know where to start. Her ...