reactjs - Centering element in react using MUI components while ignoring additional elements in the border - Stack Overflow

I am working on a project in react using the MUI library. I have a main page layout which essentially l

I am working on a project in react using the MUI library. I have a main page layout which essentially looks like

<Container>
  Page content
</Container>

I want the content to be aligned in the header. If I use the same format, that will work perfectly fine. However, I want to add an additional element outside the container. After adding this element, the container will no longer center the content because it takes into consideration the space taken up by the additional content.

How am I able to centre the content in the same way while adding this additional component?

In reality, the project is much more complicated with tons of components and several nested boxes, containers, stacks and grids. But I have attempted to capture here the essential problem that I am unable to solve.

To clarify, in the main body I have the structure shown above. In an additional element (a header), I want

{/* This element is off to the side */}
<SideElement />

{/* This should be centered with respect to the entire page */} 
<Container>
  <MainContent />
<Container />

I am working on a project in react using the MUI library. I have a main page layout which essentially looks like

<Container>
  Page content
</Container>

I want the content to be aligned in the header. If I use the same format, that will work perfectly fine. However, I want to add an additional element outside the container. After adding this element, the container will no longer center the content because it takes into consideration the space taken up by the additional content.

How am I able to centre the content in the same way while adding this additional component?

In reality, the project is much more complicated with tons of components and several nested boxes, containers, stacks and grids. But I have attempted to capture here the essential problem that I am unable to solve.

To clarify, in the main body I have the structure shown above. In an additional element (a header), I want

{/* This element is off to the side */}
<SideElement />

{/* This should be centered with respect to the entire page */} 
<Container>
  <MainContent />
<Container />
Share Improve this question asked Mar 24 at 7:10 Jojo CalabazaJojo Calabaza 11 bronze badge 1
  • 1 Try using position: absolute or grid layout where you need overlapping or non-intrusive side elements. – shruti Commented Mar 24 at 8:12
Add a comment  | 

1 Answer 1

Reset to default 0

It is difficult to answer without the code but as you saying and considering you are using MUI try using box and positioning

<Box sx={{ position: 'relative' , width: '100%' , height: 'auto' }}>
        {/* Side element is absolutely positioned */}
        <Box sx={{ position: 'absolute' , left: 0, top: '50%' , transform: 'translateY(-50%)' }}>
            <SideElement />
        </Box>
        {/* Main container is centered */}
        <Container sx={{ textAlign: 'center' }}>
            <MainContent />
        </Container>
</Box>

Instead of container you can add a box too

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744256450a4565423.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信