2

The following code produces a set of blue boxes and within two of these boxes a small white box is displayed. How can I make these white boxes stick to the bottom of the blue boxes? I tried to add margin-top: auto; to .test but that did not work.

.flex-container {
    display: -webkit-flex;
    display: flex;
    width: 400px;
    height: 250px;
    background-color: lightgrey;
    flex-wrap: wrap;
}

.flex-item {
    background-color: cornflowerblue;
    width: 100px;
    height: 100px;
    margin: 10px;
}

.test {
    background-color: white;
    width: 20px;
    height: 20px;
    margin-bottom: 0 auto;
}
<div class="flex-container">
  <div class="flex-item">flex item 1flex item 1flex item 1flex item 1flex 
  item 1<div class="test"></div></div>
  <div class="flex-item">flex item 2<div class="test"></div></div>
  <div class="flex-item">flex item 3</div>
  <div class="flex-item">flex item 3</div>  
  <div class="flex-item">flex item 3</div>  
</div>
Ivar
  • 6,138
  • 12
  • 49
  • 61
user2237168
  • 305
  • 1
  • 3
  • 17

4 Answers4

0

Just edit your css and add position: relative to 'flex-item, and position: absolute to 'test'. Of course you have to add bottom: 0px to send it to the bottom

.flex-item {
  position: relative;
background-color: cornflowerblue;
width: 100px;
height: 100px;
margin: 10px;
}

.test {
  position: absolute;
background-color: white;
width: 20px;
height: 20px;
bottom: 0px;
}
TalGabay
  • 238
  • 2
  • 9
0

Add position: absolute; and bottom: 0; to those .test cubes (you could also add left: 0, but that's the default anyway), and position: relative; to their containers to have them serve as an "anchor" for the absolute position of their children.

.flex-container {
  display: -webkit-flex;
  display: flex;
  width: 400px;
  height: 250px;
  background-color: lightgrey;
  flex-wrap: wrap;
}

.flex-item {
  background-color: cornflowerblue;
  width: 100px;
  height: 100px;
  margin: 10px;
  position: relative;;
}

.test {
  background-color: white;
  width: 20px;
  height: 20px;
  position: absolute;
  bottom: 0;
}
<div class="flex-container">
  <div class="flex-item">flex item 1flex item 1flex item 1flex item 1flex item 1
    <div class="test"></div>
  </div>
  <div class="flex-item">flex item 2
    <div class="test"></div>
  </div>
  <div class="flex-item">flex item 3</div>
  <div class="flex-item">flex item 3</div>
  <div class="flex-item">flex item 3</div>
</div>

ADDITION AFTER COMMENT:

Conterning the additional question in the comment:

You can define the flex items as flex containers with flex-direction: column and apply margin-top: auto to .test. However, you need to change the height of flex-item to min-height in order to allow it to expand:

.flex-container {
  display: -webkit-flex;
  display: flex;
  width: 400px;
  height: 250px;
  background-color: lightgrey;
  flex-wrap: wrap;
}

.flex-item {
  background-color: cornflowerblue;
  width: 100px;
  min-height: 100px;
  margin: 10px;
  display: flex;
  flex-direction: column;
}

.test {
  background-color: white;
  width: 20px;
  height: 50px;
  margin-top: auto;
}
<div class="flex-container">
  <div class="flex-item">flex item 1flex item 1flex item 1flex item 1flex item 1
    <div class="test"></div>
  </div>
  <div class="flex-item">flex item 2
    <div class="test"></div>
  </div>
  <div class="flex-item">flex item 3</div>
  <div class="flex-item">flex item 3</div>
  <div class="flex-item">flex item 3</div>
</div>
Johannes
  • 64,305
  • 18
  • 73
  • 130
  • Tnx for your answer. Could you also tell me how to make sure that when resizing the height of .test (for instance from 20px to 50px) that .flex-item resizes along with it such that the white box (.test) stays at the bottom and below the text and does not overlap the text above it? – user2237168 Aug 09 '17 at 14:49
  • That won't work - absolutetey positioned elements are independent from their parents, so changing their size won't affect any othe objects (and might overlap them). – Johannes Aug 09 '17 at 14:51
  • Yeah I just tried it and noticed it. Is there a way to cope with this? – user2237168 Aug 09 '17 at 14:53
  • I just added a second solution, have a look at it. – Johannes Aug 09 '17 at 14:56
  • Thank you very much Johannes for the clear solution and explanation – user2237168 Aug 09 '17 at 15:09
0

margin would work if the parent is also a flexbox (Also if there is one value for margin-x).

here : margin:auto 0 0; should do.

(margin:auto 0 0 auto ;would set it at bottom right )

.flex-container {
    display: -webkit-flex;
    display: flex;
    width: 400px;
    height: 250px;
    background-color: lightgrey;
    flex-wrap: wrap;
}

.flex-item {
    background-color: cornflowerblue;
    width: 100px;
    height: 100px;
    margin: 10px;
        display: -webkit-flex;
    display: flex;
    flex-direction:column;
}

.test {
    background-color: white;
    width: 20px;
    height: 20px;
    margin-top:auto;
    margin-bottom: 0;
}
<div class="flex-container">
  <div class="flex-item">flex item 1flex item 1flex item 1flex item 1flex 
  item 1<div class="test"></div></div>
  <div class="flex-item">flex item 2<div class="test"></div></div>
  <div class="flex-item">flex item 3</div>
  <div class="flex-item">flex item 3</div>  
  <div class="flex-item">flex item 3</div>  
</div>
G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129
0

You can work with align-self directly on a flex child.

.container {
  display: flex;
}

.place-at-bottom {
  align-self: flex-end;
}


/* ignore them */
.container {
  width: 200px;
  height: 200px;
  background: yellow;
}

.place-at-bottom {
  width: 50px;
  height: 50px;
  background: cyan;
  border: 1px solid black;
}
<div class="container">

  <div class="place-at-bottom"></div>
</div>
Hitmands
  • 13,491
  • 4
  • 34
  • 69