I am using flex based layout in my HTML / CSS based layout, now I want to align a button at bottom of flexbox based div. Parent div of button has flex based CSS property.
Consider below image, I want to push "Select" button at bottom of main div, so how can I do it?
Here is the HTML
<section class="item">
<!--some html-->
<div class="fearures"></div>
<div class="priceSelectbtnDiv">
<input type="button" value="Select " class="btn btn-primary">
</div>
</section>
and current CSS of parent div
.item {
flex: 1 0 180px;
display: flex;
flex-flow: column;
padding: 15px 12px;
border-radius: 5px;
border: 1px solid #00C5A3;
}
Easiest solution for aligning an item to the bottom of div using Flex is using "auto-margins", so considering your HTML and CSS, your CSS should be as below
.features { margin-bottom: auto; } /* Push following elements to the bottom */
.priceSelectbtnDiv { margin-top: auto; } /* Push it and following elements to the bottom */
This works considering any positive free space is distributed to auto margins.
Here is the complete fiddle link https://jsfiddle.net/71pcd3bz/
OR
Simply follow these steps
display:flex; flex-direction:column; flex:1;
"
margin-top: auto;
"
Fiddle: https://jsfiddle.net/dktL4bun/
OR
Use flex-direction :column
.content {
display: flex;
justify-content: space-between;
flex-direction: column;
height: 300px;
}
.content .upper {
justify-content: normal;
}
sample fiddle: https://jsfiddle.net/0zc4vLsk/
Hope it helps, thanks.
I would like to add one more option to set "flex: none;" to the div you want to place in end and "flex:1" to the content you want to keep up inside "flex-direction: column;" div
HTML
<div class="content">
<div class="top">
<p>Some more or less text</p>
<p>Some more or less text</p>
</div>
<div class="bottom">
<a href="/" class="button">Bottom anchor</a>
</div>
</div>
CSS
.content {
height: 300px;
display: flex;
flex-direction: column;
}
.top {
flex: 1;
}
.bottom {
flex: none;
}
You can check sample fiddle here.
Try flex-grow: 1;
for the second last element, so last element is aligned at bottom automatically.
Here is sample Code
.content {
height: 200px;
border: 1px solid;
display: flex;
flex-direction: column;
}
.top{
flex-grow: 1;
}
With HTML
<div class="content">
<div class="top">
<p>Some more or less text</p>
<p>Some more or less text</p>
</div>
<div class="bottom">
<a href="/" class="button">Bottom anchor</a>
</div>
</div>
Fiddle: https://jsfiddle.net/yv85Lj2f/
Hope it helps.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly