简体   繁体   English

Flexbox对齐不起作用

[英]Flexbox alignment is not working

I have a container of items with the following style: 我有一个具有以下样式的物品容器:

 .container-ok { display: flex; flex: 0 1 auto; flex-direction: column; align-items: flex-end; } .container-error { display: flex; flex: 0 1 auto; flex-direction: column; justify-content: flex-end; } .item { width: 3rem; border: 1px black solid; } 
 <div class="container-ok"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> </div> <div class="container-error"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> </div> 

I'm expecting that all the items in the container are rendered in one column and are on the right side, but it's not working. 我期望容器中的所有项目都显示在一列中并且在右侧,但是它不起作用。 When I remove flex-direction , it works but I have as example 3 items and they are in a row (3 columns) not one column. 当我删除flex-direction ,它可以工作,但是我有3个示例,它们在一行(3列)中而不是一列中。 If I remove justify-content and replace it with align-items , it works. 如果我删除justify-content并将其替换为align-items ,那么它将起作用。 But align-items is for vertical alignment. 但是align-items是用于垂直对齐的。

What am I doing wrong? 我究竟做错了什么?

thank you 谢谢

If I understood you right, you want to achieve this result 如果我理解正确,那么您想要实现结果

HTML 的HTML

<div class="container-ok">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
</div>

<div class="container-error">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
</div>

CSS 的CSS

.container-ok {
  display: flex;
  flex: 0 1 auto;
  justify-content: flex-end;
}

.container-error {
  display: flex;
  flex: 0 1 auto;
  justify-content: flex-end;
}

.item {
  width: 3rem;
  border: 1px black solid;
}

Also there is a nice guide to flexbox, maybe it will help you 另外还有关于flexbox的不错的指南 ,也许会对您有所帮助

Justify content declaration works because it defines the alignment along the main axis. 证明内容声明有效是因为它定义了沿主轴的对齐方式。 Instead justify content not work because It defines the default behaviour for how flex items are laid out along the cross axis on the current line. 相反,证明内容无效是因为它定义了如何在当前行上沿交叉轴布局弹性项目的默认行为。 You can think of it as the justify-content version for the cross-axis (perpendicular to the main-axis). 您可以将其视为跨轴(垂直于主轴)的证明内容版本。

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

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