Search code examples
htmlcssflexboxmultiple-columnsvertical-alignment

Flexbox layout with on column fluid and the other fixed width


On THIS page I have a layout with rows of text-photo pairs. I need the photo to have a fixed width and the text to have fluid width. The text has to be vertically centered. I have used flexbox to achieve these goals.

As can be seen below, in the list item with the id of box2, in order to place the photo to the left, I had to insert it before the text block in the DOM.

The problem: on small, mobile resolutions, when the display goes to block instead of flex, the photos of the first and second row are one after the other, when instead, there should be a block of text between them.

How can I fix that?

h3 {
  max-width: 1000px;
  margin: 0 auto;
  padding: 65px 0;
  font-size: 36px;
  line-height: 1.2;
  font-weight: 300;
  font-family: "Open Sans", sans-serif;
}
ul.boxes {
  list-style-type: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
}
ul.boxes li {
  color: #3e3e3e;
  display: flex;
  flex-direction: row;
  align-items: center;
}
ul.boxes #box1 {
  background: #dcdcdc;
}
ul.boxes #box3 {
  background: #6b6b6b;
  color: #fff;
}
ul.boxes li .photo {
  width: 402px;
  max-width: 100%;
}
ul.boxes li .content {
  padding: 0 65px;
  width: 100%;
}
ul.boxes li h4 {
  font-family: 'Oswald', sans-serif;
  font-size: 16px;
  margin-top: 0;
  text-transform: uppercase;
}
ul.boxes li .content ul {
  padding: 0;
}
ul.boxes li .content ul li {
  padding-left: 20px;
  background: transparent url('img/redbullet.png') no-repeat 5px 8px;
}
@media screen and (max-width: 480px) {
  ul.boxes,
  ul.boxes li {
    display: block;
    margin-bottom: 10px;
  }
}
<h3 class="text-center thin">Obiectivul nostru este să fim în fiecare zi mai buni, iar munca noastră se bazează pe 3 piloni principali:
	Calitate, Creativitate, Rezultate.</h3>
<ul class="boxes">
  <li id="box1">
    <div class="content">
      <h4>Lorem ipsum dolor</h4>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Soluta, reiciendis molestias inventore blanditiis ratione amet, dolore id doloribus minus iure esse, accusantium qui ex, nesciunt? Officiis, animi saepe libero quae!</p>
    </div>
    <div class="photo flex-left">
      <div class="photo flex-right">
        <img src="http://placehold.it/402x335">
      </div>
    </div>
  </li>
  <li id="box2">
    <div class="photo flex-right">
      <img src="http://placehold.it/402x335">
    </div>
    <div class="content">
      <h4>Lorem ipsum dolor</h4>
      Descoperim continuu soluții noi pentru a ne îndeplini sarcinile și pentru a trece peste provocările zilnice, ca să fim în fiecare zi mai buni.
    </div>
  </li>
  <li id="box3">
    <div class="content">
      <h4>Lorem ipsum dolor</h4>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Soluta, reiciendis molestias inventore blanditiis ratione amet, dolore id doloribus minus iure esse, accusantium qui ex, nesciunt? Officiis, animi saepe libero quae!</p>
    </div>
    <div class="photo flex-left">
      <div class="photo flex-right">
        <img src="http://placehold.it/402x335">
      </div>
    </div>
  </li>
</ul>


Solution

  • Instead of display: block below (and including) 480px, you can wrap your flexbox and set order for the flex children like this:

      ul.boxes,
      ul.boxes li {
        flex-wrap: wrap;
        margin-bottom: 10px;
      }
      ul.boxes li .photo {
        order: 2;
      }
      ul.boxes li .content {
        order: 1;
      }
    

    I guess now you can take it forward from here - see demo below:

    h3 {
      max-width: 1000px;
      margin: 0 auto;
      padding: 65px 0;
      font-size: 36px;
      line-height: 1.2;
      font-weight: 300;
      font-family: "Open Sans", sans-serif;
    }
    ul.boxes {
      list-style-type: none;
      margin: 0;
      padding: 0;
      display: flex;
      flex-direction: column;
    }
    ul.boxes li {
      color: #3e3e3e;
      display: flex;
      flex-direction: row;
      align-items: center;
    }
    ul.boxes #box1 {
      background: #dcdcdc;
    }
    ul.boxes #box3 {
      background: #6b6b6b;
      color: #fff;
    }
    ul.boxes li .photo {
      width: 402px;
      max-width: 100%;
    }
    ul.boxes li .content {
      padding: 0 65px;
      width: 100%;
    }
    ul.boxes li h4 {
      font-family: 'Oswald', sans-serif;
      font-size: 16px;
      margin-top: 0;
      text-transform: uppercase;
    }
    ul.boxes li .content ul {
      padding: 0;
    }
    ul.boxes li .content ul li {
      padding-left: 20px;
      background: transparent url('img/redbullet.png') no-repeat 5px 8px;
    }
    @media screen and (max-width: 480px) {
      ul.boxes,
      ul.boxes li {
        flex-wrap: wrap;
        margin-bottom: 10px;
      }
      ul.boxes li .photo {
        order: 2;
      }
      ul.boxes li .content {
        order: 1;
      }
    }
    <h3 class="text-center thin">Obiectivul nostru este să fim în fiecare zi mai buni, iar munca noastră se bazează pe 3 piloni principali:
    	Calitate, Creativitate, Rezultate.</h3>
    <ul class="boxes">
      <li id="box1">
        <div class="content">
          <h4>Lorem ipsum dolor</h4>
          <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Soluta, reiciendis molestias inventore blanditiis ratione amet, dolore id doloribus minus iure esse, accusantium qui ex, nesciunt? Officiis, animi saepe libero quae!</p>
        </div>
        <div class="photo flex-left">
          <div class="photo flex-right">
            <img src="http://placehold.it/402x335">
          </div>
        </div>
      </li>
      <li id="box2">
        <div class="photo flex-right">
          <img src="http://placehold.it/402x335">
        </div>
        <div class="content">
          <h4>Lorem ipsum dolor</h4>
          Descoperim continuu soluții noi pentru a ne îndeplini sarcinile și pentru a trece peste provocările zilnice, ca să fim în fiecare zi mai buni.
        </div>
      </li>
      <li id="box3">
        <div class="content">
          <h4>Lorem ipsum dolor</h4>
          <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Soluta, reiciendis molestias inventore blanditiis ratione amet, dolore id doloribus minus iure esse, accusantium qui ex, nesciunt? Officiis, animi saepe libero quae!</p>
        </div>
        <div class="photo flex-left">
          <div class="photo flex-right">
            <img src="http://placehold.it/402x335">
          </div>
        </div>
      </li>
    </ul>