Skip to content Skip to sidebar Skip to footer

Fix Elements To The Bottom Of A Flex Container

I am using flexbox to create part of a page and I have 2 buttons that will open modals in one of the flexbox items. I would like to fix the buttons to the bottom of the item like a

Solution 1:

You can make .onedisplay: flex; flex-direction: column;, wrap the buttons in an element, and use justify-content: space-between on .one to push the buttons to the bottom of the column.

#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: 15px32px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
}
<divid="container"><divclass="one"><p> I would like to align the buttons to the bottom of this item </p><divclass="buttons"><buttonid="Btn1">Open Modal 1</button><buttonid="Btn2">Open Modal 2</button></div></div><divclass="two"><p> No buttons for this item </p></div>

Solution 2:

Since flex properties only apply to the children of a flex container, your buttons are not flex items.

The buttons are descendants of the flex container (#container), but not children, so they are beyond the scope of flex layout.

One method to resolve the issue would be to make the parent of the buttons a flex container. Then flex properties can be applied to the buttons.

#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: 00100%;          /* new */
}

button {
  margin: auto 10px0;     /* new */background-color: green;
  border: none;
  color: white;
  padding: 15px32px;
  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;
}
<divid="container"><divclass="one"><p> I would like to align the buttons to the bottom of this item </p><buttonid="Btn1">Open Modal 1</button><buttonid="Btn2">Open Modal 2</button></div><divclass="two"><p> No buttons for this item </p></div>

Learn more about the flex formatting context here:

Learn more about flex auto margins here:

Solution 3:

One approach would be to put your buttons in their own <div> like so:

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

and change your css to:

#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: 15px32px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
}

Post a Comment for "Fix Elements To The Bottom Of A Flex Container"