简体   繁体   English

CSS中Flexbox的对齐问题

[英]Alignment issue with flexbox in css

I want to align menu on the left side and page content which has multiple dashboard panels/cards on the right side. 我想使左侧菜单与页面内容对齐,该页面内容在右侧具有多个仪表板面板/卡。 The problem is dashboard panels/cards are dynamic sometimes number of cards might be 1 and sometimes more than 1. I want all the dashboard panels on the right side to be center aligned. 问题是仪表板面板/卡是动态的,有时卡的数量可能是1,有时大于1。我希望右侧的所有仪表板面板居中对齐。

I tried with the following code but its not working as expected: 我尝试使用以下代码,但无法正常工作:

 main { display: flex; flex-wrap: wrap; } aside { width: 200px; height: 200px; border: 2px solid red; } .flex-items { display: flex; justify-content: center; flex-wrap: wrap; } .flex-item { margin-left: 50px; border: 2px solid red; flex: 1; width: 250px; height: 100px; } 
 <main> <aside>Menu</aside> <section class="flex-items"> <div class="flex-item">item1</div> <div class="flex-item">item2</div> <div class="flex-item">item3</div> </section> </main> 

Codepen Codepen

Finally, I would like to have all the items to be responsive when page is shrunk. 最后,我希望页面缩小时所有项目都能响应。

I removed the flex: 1; 我删除了flex: 1; attribute from .flex-item as it looks like you wanted them to be a specific size. .flex-item属性,因为您希望它们为特定大小。 width and flex don't make sense used together as flex: 1; widthflex没有意义,因为flex: 1; enables flex-grow and that tells the div to use all available space. 启用flex-grow并告诉div使用所有可用空间。
I then added flex-grow: 1; 然后我添加了flex-grow: 1; to .flex-items so it uses all the available space (before it was just as wide as it needed to be) so the justify-content: center can do it's work. .flex-items以便它使用所有可用空间(在它需要足够的宽度之前),以便justify-content: center可以完成工作。

 main { display:flex; } aside { width:200px; height:200px; border:2px solid red; } .flex-items { display:flex; justify-content:center; flex-wrap:wrap; flex-grow: 1; } .flex-item { margin-left:10px; border:2px solid red; width:80px; height:100px; } 
 <main> <aside>Menu</aside> <section class="flex-items"> <div class="flex-item">item1</div> <div class="flex-item">item2</div> <div class="flex-item">item3</div> </section> </main> 

Note: I changed the widths and margins to allow better rendering in small spaces 注意:我更改了宽度和边距以允许在较小的空间中更好地渲染

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

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