简体   繁体   English

父 div 底部的跨度

[英]Span at the bottom of Parent div

I have a flex container in which I have two boxes which are side by side (and collapse in one column when the screen is too small).我有一个 flex 容器,其中有两个并排的盒子(当屏幕太小时会折叠在一列中)。

Now, when boxes are side by side, I've set that they have the same height, like in the following image.现在,当盒子并排时,我已经设置它们具有相同的高度,如下图所示。 在此处输入图片说明

Now, what I want to achieve is to put the EDIT button of both boxes at the bottom (this means that only the EDIT button of the box with less content will need to move).现在,我想要实现的是将两个框的 EDIT 按钮放在底部(这意味着只有内容较少的框的 EDIT 按钮需要移动)。

I've searched, but the solution are to use position absolute, because one of the two boxes won't need it, and if I put position absolute on both EDIT button, the box on the right will have less height because it won't count the EDIT button anymore.我已经搜索过,但解决方案是使用绝对位置,因为两个框之一不需要它,如果我在两个 EDIT 按钮上都放置了绝对位置,则右侧的框的高度将降低,因为它不会不再计算 EDIT 按钮。

I tried to play with flex, but I rather not set the boxes as display flex.我尝试使用 flex,但我宁愿不将框设置为显示 flex。

There may be some solution with Jquery, I've read something about it, but it was just something like "You could achieve that with Jquery!" Jquery 可能有一些解决方案,我读过一些关于它的内容,但就像“你可以用 Jquery 实现那个!” but.. I honestly don't know how.但是..老实说,我不知道如何。

 #parent { flex-flow: row wrap; display: flex; align-items: stretch; } .box { margin: 10px; padding: 15px; flex: 0 1 calc(50% - 50px); } .box:last-of-type { background-color: green; } .box:first-of-type { background-color: red; } .cool-form { display: flex; flex-direction: column; } .button-container { display: block; margin-left: 5px; margin-top: auto; align-self: center; } .button { background-color: white; display: inline-block; width: 120px; height: 48px; line-height: 48px; border: 1px solid black; text-align: center; cursor: pointer; }
 <div id="parent"> <div class="box"> <form class="cool-form"> <h1>Box on left</h1> <p>This is a shorter box</p> <span class="button-container"> <span class="button">EDIT</span> </span> </form> </div> <div class="box"> <form class="cool-form"> <h1>Box on right</h1> <p>This is</p> <p>a</p> <p>bigger</p> <p>waaay bigger</p> <p>Box</p> <span class="button-container"> <span class="button">EDIT</span> </span> </form> </div> </div>

Adding flexbox to these divs (using flex-direction:column ) seems minimally invasive (which was, perhaps a concern).flexbox添加到这些 div(使用flex-direction:column )似乎微创(这可能是一个问题)。

.box {
  display: flex;
  flex-direction: column;
}

Then you can give the button container margin-top:auto and it's auomatically pushed to the bottom.然后你可以给按钮容器margin-top:auto ,它会margin-top:auto推到底部。

Then it's just a matter of aligning it horizontally/centrally然后只需将其水平/居中对齐

.button-container {
  margin-top: auto;
  align-self: center;
}

 #parent { flex-flow: row wrap; display: flex; align-items: stretch; } .box { margin: 10px; padding: 15px; flex: 0 1 calc(50% - 50px); display: flex; flex-direction: column; } .box:last-of-type { background-color: green; } .box:first-of-type { background-color: red; } .button-container { display: block; margin-left: 5px; margin-top: auto; align-self: center; } .button { background-color: white; display: inline-block; width: 120px; height: 48px; line-height: 48px; border: 1px solid black; text-align: center; cursor: pointer; }
 <div id="parent"> <div class="box"> <h1>Box on left</h1> <p>This is a shorter box</p> <span class="button-container"> <span class="button">EDIT</span> </span> </div> <div class="box"> <h1>Box on right</h1> <p>This is</p> <p>a</p> <p>bigger</p> <p>waaay bigger</p> <p>Box</p> <span class="button-container"> <span class="button">EDIT</span> </span> </div> </div>

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

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