简体   繁体   English

使用 flexbox 进行垂直对齐的问题

[英]Problems with using flexbox for vertically aligning

I have a page-wide wrapping div that has flexbox alignment to center:我有一个页面范围的包装 div,其中 flexbox alignment 居中:

.app_container{
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
overflow-y: auto;
overflow-x: hidden;
padding: 10px;
}

In this wrapper, I have a menu that can vary in height as the user expands submenus.在这个包装器中,我有一个菜单,当用户展开子菜单时,它的高度会有所不同。 The problem is when the menu becomes vertically bigger than the window height.问题是当菜单垂直大于 window 高度时。 Some parts of the menu gets cut off at the top.菜单的某些部分在顶部被切断。

Image: https://imgur.com/a/x00tnoJ图片: https://imgur.com/a/x00tnoJ

One solution that I found was to simply get overflow: auto on the menu.我发现的一种解决方案是简单地溢出:菜单上的自动。 But that causes the scroll bar to appear on the menu, not on the page wrapper.但这会导致滚动条出现在菜单上,而不是页面包装上。 I want the scroll bar to be on the page wrapper.我希望滚动条位于页面包装器上。

Image: https://imgur.com/a/0eZM5Iq图片: https://imgur.com/a/0eZM5Iq

Don't think it is relevant, but I use React.不要认为它是相关的,但我使用 React。

Here is codepen: https://codepen.io/GuacomoleCyclone/pen/xxEoary这是codepen: https://codepen.io/GuacomoleCyclone/pen/xxEoary

EDIT: I've stumbled upon a solution.编辑:我偶然发现了一个解决方案。 I've added this and it solved all problems:我添加了这个,它解决了所有问题:

html, body{
display: grid;
}

If I understand correctly what your codepen is showing, the issue seems to be coming from setting the width and height on the html element.如果我正确理解您的 codepen 显示的内容,则问题似乎来自在html元素上设置宽度和高度。 You want to change:你想改变:

html, body {
    width: 100%;
    height: 100%;
    padding: 0px;
    margin: 0px;
}

to

html, body { 
  padding: 0px;
  margin: 0px;
}
body {
    width: 100%;
    height: 100%;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM