将元素固定到Flex容器的底部

时间:2017-06-15 16:09:00

标签: html css css3 flexbox

我使用flexbox创建页面的一部分,我有2个按钮,可以在其中一个flexbox项目中打开模态。如果可能的话,我想像按钮一样将按钮固定到项目的底部。

我创建了一个CodePen来演示此问题。我尝试了几种解决方案,但似乎无法让按钮在底部对齐。我对HTML和CSS比较陌生,所以我很可能错过了一些微不足道的东西。

#container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: space-around;
  align-items: center;
  align-content: center;
  margin-top: 20px;
}

.one {
  background-color: black;
  color: white;
  font-size: 30px;
  padding: 10px;
  width: 450px;
  flex-grow: 1;
  height: 600px;
}

.two {
  background-color: grey;
  color: white;
  font-size: 30px;
  padding: 10px;
  flex-grow: 1.2;
  width: 524px;
  height: 600px;
}

button {
  background-color: green;
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
}
<div id="container">
  <div class="one">
    <p> I would like to align the buttons to the bottom of this item </p>
    <button id="Btn1">Open Modal 1</button>
    <button id="Btn2">Open Modal 2</button>
  </div>
  <div class="two">
    <p> No buttons for this item </p>
  </div>

3 个答案:

答案 0 :(得分:2)

您可以设置.one display: flex; flex-direction: column;,将按钮包装在元素中,然后使用justify-content: space-between上的.one将按钮按到列的底部。

#container{
  display: flex;
  flex-direction: row;
  flex-wrap: wrap; 
  justify-content: space-around;
  align-items: center;
  align-content: center;
  margin-top: 20px;
}

.one{
  background-color: black;
  color: white;
  font-size: 30px;
  padding: 10px; 
  width: 450px;
  flex-grow: 1; 
  height: 600px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}


.two{
  background-color: grey;
  color: white;
  font-size: 30px;
  padding: 10px; 
  flex-grow: 1.2;
  width: 524px;
  height: 600px;
}

button {
    background-color: green; 
    border: none;
    color: white;
    padding: 15px 32px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
}
<div id="container">
      <div class="one">
        <p> I would like to align the buttons to the bottom of this item </p>
        <div class="buttons">
        <button id="Btn1">Open Modal 1</button>
        <button id="Btn2">Open Modal 2</button>
        </div>
      </div>
      <div class="two">
        <p> No buttons for this item </p>
     </div>

答案 1 :(得分:1)

由于flex属性仅适用于Flex容器的子项,因此您的按钮不是flex项。

按钮是flex容器(#container)的后代,但不是 children ,因此它们超出了flex布局的范围。

解决问题的一种方法是使按钮的父级成为弹性容器。然后可以将flex属性应用于按钮。

&#13;
&#13;
#container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: space-around;
  align-items: center;
  align-content: center;
  margin-top: 20px;
}

.one {
  background-color: black;
  color: white;
  font-size: 30px;
  padding: 10px;
  width: 450px;
  flex-grow: 1;
  height: 600px;
  display: flex;           /* new */
  flex-wrap: wrap;         /* new */
}

p {
  flex: 0 0 100%;          /* new */
}

button {
  margin: auto 10px 0;     /* new */
  background-color: green;
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
}

.two {
  background-color: grey;
  color: white;
  font-size: 30px;
  padding: 10px;
  flex-grow: 1.2;
  width: 524px;
  height: 600px;
}
&#13;
<div id="container">
  <div class="one">
    <p> I would like to align the buttons to the bottom of this item </p>
    <button id="Btn1">Open Modal 1</button>
    <button id="Btn2">Open Modal 2</button>
  </div>
  <div class="two">
    <p> No buttons for this item </p>
  </div>
&#13;
&#13;
&#13;

在此处详细了解flex格式设置上下文:

点击此处了解有关flex auto页边距的详情:

答案 2 :(得分:0)

一种方法是将按钮放在他们自己的<div>中,如下所示:

<div id="container">
  <div class="one">
    <p> I would like to align the buttons to the bottom of this item </p>
      <div class="btn-container">
        <button id="Btn1">Open Modal 1</button>
        <button id="Btn2">Open Modal 2</button>
      </div>
    </div>
  <div class="two">
<p> No buttons for this item </p>
</div>

并将您的CSS更改为:

#container{
  display: flex;
  flex-direction: row;
  flex-wrap: wrap; 
  justify-content: space-around;
  align-items: center;
  align-content: center;
  margin-top: 20px;
}

.one {
  display: flex;
  flex-direction: column;
  flex-grow: 1;
  justify-content: space-between;
  height: 100vh;
  background-color: black;
  color: white;
  font-size: 30px;
  padding: 10px; 
  width: 450px;
  height: 600px;  
}

.two {
  background-color: grey;
  color: white;
  font-size: 30px;
  padding: 10px; 
  flex-grow: 1.2;
  width: 524px;
  height: 600px;
}

 .btn-container {
    justify-content: flex-end;
    display: flex;
  }

button {
    margin: 10px;
    background-color: green; 
    border: none;
    color: white;
    padding: 15px 32px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
}