2
display: flex; //Parent element

and

flex-wrap: wrap; //Direct child elements

Each grid has to fill 25% of the containers width and go down automatically..

But currently they do not act like flex-wrap: wrap; they act like they are 20% width when there are 5 grids... its a bit weird because they are set to be 25% ?

If I put Flex-wrap: wrap; on the parent element where I currently have display:flex; it works, but then they are not equal heights?

What do I do in this situation? I can't figure out how to do this

Find the example here: http://www.chri126g.wigf1.sde.dk/DUER1/

HTML

<div class="global-wrapper">

<div class="dueWrapper flex">

  <div class="dueOuter col25 flex-w-w">
    <div class="dueInner flex-d-r">
      <img src="img/salg/1.jpg" alt="due">
      <table>
        <tr>
          <td>Navn</td>
          <td>Smith</td>
        </tr>
        <tr>
          <td>Far</td>
          <td>Jackson</td>
        </tr>
        <tr>
          <td>Mor</td>
          <td>Emelie</td>
        </tr>
        <tr>
          <td>Pris</td>
          <td>500kr</td>
        </tr>
      </table>
    </div>
  </div>

  <div class="dueOuter col25 flex-w-w">
    <div class="dueInner flex-d-r">
      <img src="img/salg/2.jpg" alt="due">

    </div>
  </div>

  <div class="dueOuter col25 flex-w-w">
    <div class="dueInner flex-d-r">
      <img src="img/salg/3.jpg" alt="due">

    </div>
  </div>

  <div class="dueOuter col25 flex-w-w">
    <div class="dueInner flex-d-r">
      <img src="img/salg/3.jpg" alt="due">

    </div>
  </div>

  <div class="dueOuter col25 flex-w-w">
    <div class="dueInner flex-d-r">
      <img src="img/salg/3.jpg" alt="due">

    </div>
  </div>

</div><!--/DUE-WRAPPER-->
</div><!--/GLOBAL-WRAPPER-->
CSS

.flex {
  display: flex;
}
.flex1{
  flex: 1;
}
.flex-d-r{
  display: flex;
  flex-direction: column;
}
.flex-w-w{
  display: flex;
  flex-wrap: wrap;
}
body{
  background-color: lightgrey;
}
.global-wrapper{
  padding: 5px;
  width: 90%;
  min-height: 100vh;
  max-width: 1000px;
  margin: 0 auto;
  background-color: #fff;
}

.dueOuter{
  padding: 5px;
}

.dueInner{
  outline: 1px solid #000;
  min-height: 100px;
  transition: 0.3s ease;
  -webkit-box-shadow: 0px 0px 9px 0px rgba(0,0,0,0.75);
  -moz-box-shadow: 0px 0px 9px 0px rgba(0,0,0,0.75);
  -webkit-box-shadow: 0px 0px 9px 0px rgba(0,0,0,0.75);
  -moz-box-shadow: 0px 0px 9px 0px rgba(0,0,0,0.75);
  box-shadow: 0px 0px 9px 0px rgba(0,0,0,0.75);
}

.dueInner:hover{
  b: 3px solid #000;
}
.dueInner img{
  width: 100%;
}
td{
  width: 100%;
}
tr:nth-of-type(odd){
  background-color: lightgrey;
}
j08691
  • 204,283
  • 31
  • 260
  • 272

2 Answers2

3

You can not do that using flexbox. As I quote this answer by Michael_B on this question

  1. Flex Lines

In a multi-line flex container (even one with only a single line), the cross size of each line is the minimum size necessary to contain the flex items on the line (after alignment due to align-self), and the lines are aligned within the flex container with the align-content property.

In other words, when there are multiple lines in a row-based flex container, the height of each line (the "cross size") is the "minimum size necessary to contain the flex items on the line".

If you are looking for a javascript alternative. Here's what you need to add just before </body>:

<script type="text/javascript">
  var SdueOuter = document.querySelectorAll(".dueOuter");
  var maxHeight = 0;
  for(i=0;i<SdueOuter.length;i++){
   if(SdueOuter[i]){
     var currentHeight = SdueOuter[i].offsetHeight;
     if(currentHeight>=maxHeight){
        maxHeight = currentHeight;
      }
    }
    else{
      break;
    }
  }
  for(i=0;i<SdueOuter.length;i++){
    SdueOuter[i].style.height = maxHeight+"px";
  }
</script>
Community
  • 1
  • 1
ab29007
  • 7,611
  • 2
  • 17
  • 43
0

For the parent element (the one with display:flex) You need to add: flex-wrap: wrap; align-items: stretch;

Then to have size no-matter what content you have inside you have two options: 1. Calculate highiest div height with js and apply min-height: [top-height] to all children divs. 2. OR if You can estimate max height - just do it in css using min-height.

I'm 99% sure there is no way to achieve it using pure flexbox with flex-wrap and different contents.

  • there is no need to add `align-items: stretch` that is by default – Huangism Dec 29 '16 at 16:28
  • Well i can't talk this through with my teachers before 2. Jan.. but it should be possible atleast i think, maybe a hack of some sort but i dunno i cant seem to figure it out... The problem with the CSS calc (i think) is that there will be different descriptions, and i havent worked that much with calc yet hmm – Christian Lauridsen Dec 29 '16 at 16:39