2017-06-15 41 views
3

我使用flexbox创建页面的一部分,并且我有2个按钮可以在其中一个柔性箱项目中打开模块。如果可能,我想将按钮固定到项目的底部。将元素固定到柔性容器的底部

我创建了一个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>

回答

1

您可以.onedisplay: 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

由于Flex属性只适用于柔性容器的孩子,你的按钮都没有弯曲的物品。

的按钮后裔柔性容器(#container)的,但不孩子,所以它们超出柔性布局的范围。

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

#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; 
 
}
<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>

了解更多有关Flex格式化的内容在这里:

了解更多关于Flex auto利润率在这里:

+0

谢谢你有用的答案和解释。快速的问题,为什么你在CSS中添加'flex'到p?这是干什么的? – MJH

+0

这使得'p'元素消耗行的整个宽度,强制按钮到下一行。这就是为什么容器需要'flex-wrap'。 –

+0

我试图在不改变HTML的情况下做到这一点(通常人们不需要修改标记)。但是,是的,如果您可以调整标记,那么[该选项](https://stackoverflow.com/a/44572522/3597276)是更好的解决方案。 –

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; 
} 
+0

我想有人打我一拳! :) – Belder

相关问题