Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning images on two rows with flexbox

Tags:

html

css

flexbox

I am trying to create a flexbox with 6 boxes that go across the page.

However, for some reason it won't behave properly as they have different sizes.

enter image description here

they must behave like this:

enter image description here

/* ================================= 
      Base Styles
    ==================================== */

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
.row {
  flex: 1;
}
/* ================================= 
      Media Queries
    ==================================== */

@media (min-width: 769px) {
  .main-header,
  .main-nav,
  .row {
    display: flex;
  }
  .main-header {
    flex-direction: column;
    align-items: center;
  }
  .col {
    flex: 1 50%;
  }
  .row {
    flex-wrap: wrap;
  }
  .item-2 {
    order: -1;
  }
}
@media (min-width: 1025px) {
  .main-header {
    flex-direction: row;
    justify-content: space-between;
  }
  .col {
    flex-basis: 0;
  }
  .item-1 {
    flex-grow: 1.4;
  }
}
<div class="row">

  <div class="item-1 col">
    <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
  </div><!--/.primary-->

  <div class="item-2 col">
    <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
  </div><!--/.secondary-->

  <div class="item-3 col">
    <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
  </div><!--/.tertiary-->

  <div class="item-4 col">
    <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
  </div><!--/.tertiary-->

  <div class="item-5 col">
    <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
  </div><!--/.tertiary-->

  <div class="item-6 col">
    <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
  </div><!--/.tertiary-->

</div>

YOU CAN VIEW THE JSFIDDLE HERE: https://jsfiddle.net/ju157mnj/2/

like image 296
Kimberly Wright Avatar asked May 26 '16 13:05

Kimberly Wright


People also ask

How do you align images in CS flexbox?

Images can be aligned using both the text-align and flexbox properties. Applying text-align to an image's parent element can be used to right, left or center align the image. You can also use right and left float to position an image along the desired container's edge.

How do you position flex items?

Justify Content Flex Start positions element at the start of the page. Flex End sets the element to the end of the page. Space Around arranges the items evenly but with spaces between them. The spaces will be equal among all the elements inside a flex-box container, but not outside them.


1 Answers

.row {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
}

.col {
   flex: 0 0 calc(33.33% - 30px);   /* flex-basis adjustment for margin */
   margin: 5px;
}

img { 
    width: 100%;
}
<div class="row">

    <div class="item-1 col">
        <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
    </div><!--/.primary-->

    <div class="item-2 col">
        <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
    </div><!--/.secondary-->

    <div class="item-3 col">
        <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
    </div><!--/.tertiary-->

    <div class="item-4 col">
        <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
    </div><!--/.tertiary-->

    <div class="item-5 col">
        <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
    </div><!--/.tertiary-->

    <div class="item-6 col">
        <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
    </div><!--/.tertiary-->

</div>

Revised Fiddle

like image 91
Michael Benjamin Avatar answered Oct 24 '22 21:10

Michael Benjamin