简体   繁体   English

无法在Flexbox中对齐元素

[英]Can't align an element in flexbox

I'm currently trying to align the last element in a flexbox with a height higher than its elements at the bottom of the flexbox, but I can't manage to do it. 我目前正在尝试将Flexbox中的最后一个元素的高度比其在Flexbox底部的元素的高度高,但是我无法做到这一点。 I know I can use align-content: stretch which is the default behavior, but I don't want gaps between my elements. 我知道我可以使用align-content:Stretch这是默认行为,但是我不希望元素之间存在间隙。

I just want "More Stuff" to be at the bottom of the box and aligned to the right, and I can't figure out how to do it by reading through the flexbox spec. 我只希望“更多内容”位于框的底部并向右对齐,而我无法通过阅读flexbox规范来弄清楚该如何做。

https://jsfiddle.net/k8dec2bx https://jsfiddle.net/k8dec2bx

HTML: HTML:

<div class="flex">
  <img/>
  <div>
    <p>
      Stuff
    </p>
  </div>
  <div class="bottom">
    <p>
      More Stuff
    </p>
  </div>
</div>

CSS: CSS:

.flex{
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  width: 500px;
  height: 600px;
  box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.75);
}

img{
  width: 100%;
  height: 250px;
}

.flex div{
  width: 100%;
}

.bottom{
  align-self: flex-end;
  text-align: right;
}

First you need to put flex-direction: column on your .flex element as you're attempting to align items vertically. 首先,当您尝试垂直对齐项目时,需要在.flex元素上放置flex-direction: column From here it's just a matter of manipulating flex-grow and flex-shrink to your desired result. 从这里开始,只需要调整flex-growflex-shrink即可达到所需的结果。

If you want the .bottom element hard up against the bottom, all you need to do is tell the other element (which I've given a class of .top to make things clearer) that it should grow to take up all the available space. 如果你想.bottom元素起来反对下,所有你需要做的就是告诉其他元素(我已经给了一类.top使事情更清晰),它应该成长为占用所有的可用空间。 This can be done with flex-grow: 1 . 这可以通过flex-grow: 1

This can be seen in the following: 可以在以下内容中看到:

 .flex { display: flex; flex-wrap: wrap; flex-direction: column; /* New */ align-content: flex-start; width: 500px; height: 600px; box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.75); } img { width: 100%; height: 250px; } .flex div { width: 100%; } .top { flex-grow: 1; /* New */ } .bottom { align-self: flex-end; text-align: right; } 
 <div class="flex"> <img src="http://placehold.it/100" /> <div class="top"> <p> Stuff </p> </div> <div class="bottom"> <p> More Stuff </p> </div> </div> 

It's easier to see if you put different colored backgrounds on your divs but your two elements are 100% width and on top of eachother in the center of the page because of the flex-wrap: wrap . 比较容易看出是否在div上放置了不同的彩色背景,但是由于flex-wrap: wrap两个元素的宽度为100%,并且在页面中心彼此重叠。

So, to align or justify the elements you need to set a flex direction first. 因此,要对齐或对齐元素,首先需要设置弯曲方向。 (Setting the direction as either row of column changes what is considered the main axis you can see a diagram on MDN ) (将方向设置为列的任一行会更改您认为可以在MDN上看到的主轴的方向)

Row will set the main axis as left-right and column will set the main axis as up-down. 行将主轴设置为左右,列将主轴设置为上下。

After that you can use Justify-content: space-beween to space the elements as much as possible in the box div along the main axis. 之后,您可以使用Justify-content: space-beween沿主轴在box div中尽可能地Justify-content: space-beween元素。 ( align-content controls whatever is set as the opposite axis) align-content控制设置为相反轴的任何align-content

    .flex{
      display: flex;
      flex-wrap: wrap;
      flex-direction: column;
      justify-content: space-between;
      align-content: flex-start;
      width: 500px;
      height: 600px;
      box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.75);
    }

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

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