jacmaes
9/5/2016 - 6:22 PM

List with thumbnails enhanced with Grid and Flexbox

List with thumbnails enhanced with Grid and Flexbox

<link href="http://codepen.io/rachelandrew/pen/c05f966e1a29d9120c06f0bae61f9cd9" rel="stylesheet" />
.grid {
  overflow: hidden;
  padding: 0.5em 0 0 0.5em;
  max-width: 56em;
  margin: 0 auto 20px auto;
  list-style: none;
}

.grid li {
  padding: 0 0.5em 0.5em 0;
}

.grid li img {
  display: block;
}

.grid h2 {
  font-size: 100%;
}

.grid li > div,
.grid li > h2 {
  background: #808080;
  color: #fff;
  text-align: center;
  margin: 0;
}

.grid li.title h2 {
  padding: 1em;
  width: 100%;
}

.grid li a {
  color: #fff;
}

@media all and (min-width: 500px) {
  .grid li {
    float: left;
    width: 33.3333333%;
  }
  .grid li.title {
    width: 100%;
  }
}

@media all and (min-width: 60em) {
  .grid li {
    width: 25%;
  }
  .grid li.title {
    width: 25%;
    clear: left;
  }
  .grid li.title h2 {
    padding: 7em 2em;
  }
}


/* for flexbox */

@supports(display: flex) {
  @media all and (min-width: 500px) {
    .grid {
      display: flex;
      flex-wrap: wrap;
    }
    .grid li {
      display: flex;
      flex-direction: column;
    }
    /* the original pattern and therefore our fallback styles uses padding on the li and then puts the background color on the inner element. We can't therefor make it stretch just by way of the default alignment of the flex item. By setting the li to a flex column we can then make the inner grow to the height of the external container.  */
    .grid li > * {
      flex: 1;
    }
  }
}


/* for grid */

@supports(display: grid) {

 
  @media all and (min-width: 500px) {
    .grid {
      display: grid;
    grid-gap: 15px;
    padding-right: 10px;
      grid-template-columns: repeat(3, 1fr);
    }
    .grid li {
      padding: 0;
    display: block;
    background: #808080;
      float: none;
      width: auto;
    }
    .grid li.title {
      width: auto;
      grid-column: 1 / 4;
    }
  }
  @media all and (min-width: 60em) {
    .grid {
      grid-template-columns: repeat(4, 1fr);
    }
    .grid li {
      width: auto;
    }
    .grid li.title {
      grid-column: 1 / 2;
    }
  }
}

List with thumbnails enhanced with Grid and Flexbox

Taking a starting point of the pattern Brad Frost lists with his responsive patterns http://codepen.io/bradfrost/pen/GFHmf

The original pattern does not cope well with different height of content added to the boxes. With minimal changes to the original I am dealing with that issue and then enhancing the display for browsers that support newer features.

I'm using CSS Feature Queries to enhance the pattern.

Flexbox and grid enable the boxes to line up even if we add additional content to any box.

Note that we use Feature Queries in such a way that we start with the version for the most limited of browsers and then build up to those that support grid, making use of the fact that in the CSS Cascade properties and values defined later in the stylesheet override those that come before.

A Pen by rachelandrew on CodePen.

License.

<div class="supports"></div>

<ul class="grid">
			<li class="title">
				<h2>
					Some Balloons
				</h2>
			</li>
			<li>
				<div>
					<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/12005/balloon-sq5.jpg" alt="balloons">
          The Pattersons Balloon
          
				</div>
			</li>
			<li>
				<div>
					<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/12005/balloon-sq1.jpg" alt="balloons">
          The Hardy's wine balloon, launching alongside a nice checked one.
				</div>
			</li>
			<li>
				<div>
					<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/12005/balloon-sq3.jpg" alt="balloons">
          Virgin looking a bit skew.
				</div>
			</li>
			<li class="title">
				<h2>
					More balloons
				</h2>
			</li>
			<li>
				<div>
					<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/12005/balloon-sq12.jpg" alt="balloons">
          The Swatch balloon
				</div>
			</li>
			<li>
				<div>
					<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/12005/balloon-sq11.jpg" alt="balloons">
          Fortnum and Mason. What we know about this one is that it uses the correct Pantone colour.
				</div>
			</li>
			<li>
				<div>
					<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/12005/balloon-sq10.jpg" alt="balloons">
          This one is quite snazzy.
				</div>
			</li>
			<li class="title">
				<h2>
					What do you know? Balloons!
				</h2>
			</li>
			<li>
				<div>
					<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/12005/balloon-sq9.jpg" alt="balloons">
          Top of a balloon
				</div>
			</li>
			<li>
				<div>
					<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/12005/balloon-sq8.jpg" alt="balloons">
          Lots of balloons
				</div>
			</li>
			<li>
				<div>
					<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/12005/balloon-sq7.jpg" alt="balloons">
          Wahey!
				</div>
			</li>

		</ul>