5

I have a <ul> that has a fixed height and I need the <li>'s inside it to stack at the bottom (instead of the top).

I tried vertical-align: bottom on the <ul> but that doesn't change anything.

The content li have a chance of overflowing, and if they do I need the scrollbar to work.

ul {
  border: 1px solid red;
  height: 250px;
  vertical-align: bottom;
}
li {}
<ul>
  <li>a</li>
  <li>b</li>
  <li>c</li>
</ul>

JsFiddle: https://jsfiddle.net/hbcnvk4s/

Thanks !

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Alexandre Danault
  • 8,602
  • 3
  • 30
  • 33
  • Flexbox is your friend! – Chaim Jul 26 '16 at 01:12
  • `vertical-align` controls how two (or more) inline or inline-block elements are aligned to each other (it doesn't effect the content inside of an element) – Chaim Jul 26 '16 at 01:15

3 Answers3

6

Try CSS flexbox:

ul {
  display: flex;           /* establish flex container */
  flex-direction: column;  /* align children vertically */
  border: 1px solid red;
  height: 250px;
}

li:first-child {
  margin-top: auto;        /* shift top li, and all li's beneath, to the bottom */  
}
<ul>
  <li>a</li>
  <li>b</li>
  <li>c</li>
</ul>

Flex auto margins are used to align the lis to the bottom.

Although justify-content: flex-end would also align the items to the bottom, the scroll mechanism doesn't work when the items overflow the container (demo | bug report).

As an aside, scrolling would work if justify-content were flex-start or center (demo).

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701