I am new to material UI, and am having problems centering a component (horizontally and vertically) whilst including an AppBar for navigation at the top my screen. I understand that there are solutions like this, using a grid, however, this will align ALL items, including my AppBar in the middle, which I do not want. Any help would be greatly appreciated, as I have been pulling out my hair trying to work this out (I'm usually a backend dev).
- 积分
0 - 话题
0 - 评论
3205 - 注册排名
1610
A common way to do this is to position your AppBar
sticky
as a sibling of an element containing the "main" section of your app.A standard approach would be the following (I will use
Grid
since you are usingmaterial-ui
):You can check it here.
You can see the rationale: We have a container element that has a height and is positioned (
#whole-app
) and has two children, the stickyAppBar
and the main content area. The main section container hasdisplay: flex
(thecontainer
prop), a height so you can see the vertical alignment and the corresponding props for centering its children.Your code sandbox was buggy because
align
is not a validGrid
prop and you don't need thespacing
prop.As a side note, I suggest you check tailwindcss, which I think is easier for beginners.
您可以将添加display flex到父组件的任何元素居中,如下所示: