0

Please see the following example.

ul {
  display: flex;
}

li {
  flex: 1;
  display: flex;
  flex-direction: column;
  border: 1px solid green;
  justify-content: flex-start;
  align-items: center
}

ul, h3, h4, p {
  margin: 0;
  padding: 0;
  width: 100%;
}

h3 {
  margin-bottom: auto;
}

h4 {
  border-top: 1px solid green;
  margin-bottom: auto;
}

p {
  border-top: 1px solid green;
}
<ul>
  <li>
    <h3>h3<br>h3<br>h3</h3>
    <h4>h4</h4>
    <p>p</p>
  </li>
  <li>
    <h3>h3</h3>
    <h4>h4</h4>
    <p>p</p>
  </li>
  <li>
    <h3>h3</h3>
    <h4>h4</h4>
    <p>p<br>p</p>
  </li>
</ul>

I am using ul li html layout and apply flex-box style like that. The problem is that I have to fix this flex box, in which the top of each item aligns equally with each other. I tried to use margin-bottom: auto, but since the height varies, the layout is 'distorted'.

Could anyone please tell me what to do in this situation? Any other perspective / approach would be appreciated. My description is not good, so if there is anything unclear, just feel free to discuss with me. I really need your help.

Thanks in advance!


Update

This image is my expected result:

Expected result

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Tyler Bean
  • 417
  • 1
  • 4
  • 14

2 Answers2

0

You cannot do that with the HTML you provided, because the li elements are not communicating their content sizes to each other. Instead, you will need to seperate all of your content to take the advantage of automatically scaling the height.

To do so, you have to seperate your li elements more then you already have.

You can do that with flex, but you will need to change the HTML layout.

See the example below:

/* Flexbox */

ul {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

li {
  width:33%;
}



/* Extra styles */

ul, h3, h4, p {
  margin: 0;
  padding: 0;
  width: 100%;
}

h3 {
  margin-bottom: auto;
}

h4 {
  border-top: 1px solid green;
  margin-bottom: auto;
}

p {
  border-top: 1px solid green;
}
<ul>
  <li>
    <h3>h3<br>h3<br>h3</h3>
  </li>
  <li>
    <h3>h3</h3>
  </li>
  <li>
    <h3>h3</h3>
  </li>
  <li>
    <h4>h4</h4>
  </li>
  <li>
    <h4>h4</h4>
  </li>
  <li>
    <h4>h4</h4>
  </li>
  <li>
    <p>p</p>
  </li>
  <li>
    <p>p</p>
  </li>
  <li>
    <p>p<br>p</p>
  </li>
</ul>
Randy
  • 9,419
  • 5
  • 39
  • 56
0

I have changed the structure of the HTML and this will help you:

ul {
  display: flex;
  flex-flow: column nowrap;
  border: 1px solid green;
  border-collapse: collapse;
}

li {
  display: -webkit-box;
  display: -moz-box;
  display: box;
  display: -webkit-flex;
  display: -moz-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-flex-flow: row nowrap;
  -moz-flex-flow: row nowrap;
  flex-flow: row nowrap;
}

ul,
h3,
h4,
p {
  margin: 0;
  padding: 0;
  width: 100%;
}

h3,
h4,
p {
  flex-grow: 1;
  border: 1px solid green;
  box-sizing: border-box;
}
<ul>
  <li>
    <h3>h3<br>h3<br>h3</h3>
    <h3>h3</h3>
    <h3>h3</h3>
  </li>
  <li>
    <h4>h4</h4>
    <h4>h4</h4>
    <h4>h4</h4>
  </li>
  <li>
    <p>p</p>
    <p>p</p>
    <p>p<br>p</p>
  </li>
</ul>
Div_P
  • 159
  • 4