69°F

Aaron Parecki

  • Articles
  • Notes
  • Photos
  • Centered and Cropped Thumbnails with CSS

    August 13, 2016

    When working on my photo album thumbnail navigation for this site, I wanted a way to show a square thumbnail of a photo, centered and cropped from the original version. I wanted this to work without pre-rendering a square version on the server, and without using background image tricks.

    I found a great technique for doing exactly this at jonathannicol.com/blog/2014/06/16/centre-crop-thumbnails-with-css, adapted from the WordPress media library. The code below is modified slightly from this example.

    Markup

    <div class="album-thumbnails">
      <a href="photo-1.jpg">
        <img src="photo-1.jpg">
      </a>
      <a href="photo-2.jpg">
        <img src="photo-2.jpg">
      </a>
    </div>
    

    CSS

    .album-thumbnails a {
      /* set the desired width/height and margin here */
      width: 14%;
      height: 88px;
      margin-right: 1px;
    
      position: relative;
      overflow: hidden;
      display: inline-block;
    }
    .album-thumbnails a img {
      position: absolute;
      left: 50%;
      top: 50%;
      height: 100%;
      width: auto;
      -webkit-transform: translate(-50%,-50%);
          -ms-transform: translate(-50%,-50%);
              transform: translate(-50%,-50%);
    }
    .album-thumbnails a img.portrait {
      width: 100%;
      height: auto;
    }
    

    For my use, I am showing a series of 7 square thumbnails on one line, so I set the width to 14% (about 1/7) so that they will fit on one line. Since the width of my container is about 620px wide, I set the height to a fixed amount, 88px, so that the thumbnails will be approximately square.

    The neat thing about this is that when the container shrinks when viewed on smaller devices, the widths of the thumbnails will shrink as well. This does mean the thumbnails will no longer be square on narrow screens, but I'm okay with that result. You can also use a fixed pixel height and width if you don't want them to shrink at all.

    Javascript

    You may notice that the last CSS rule requires a class of "portrait" on image tags where the image is portrait orientation. You can either add that class server-side, or use the Javascript below to add the class when viewed.

    document.addEventListener("DOMContentLoaded", function(event) { 
    
      var addImageOrientationClass = function(img) {
        if(img.naturalHeight > img.naturalWidth) {
          img.classList.add("portrait");
        }
      }
    
      // Add "portrait" class to thumbnail images that are portrait orientation
      var images = document.querySelectorAll(".album-thumbnails img");
      for(var i=0; i<images.length; i++) {
        if(images[i].complete) {
          addImageOrientationClass(images[i]);
        } else {
          images[i].addEventListener("load", function(evt) {
            addImageOrientationClass(evt.target);
          });
        }
      }
    
    });
    
    Portland, Oregon
    Sat, Aug 13, 2016 11:30am -07:00 #css #layout
    4 likes 3 reposts 1 reply 1 mention
    • John Karabaic
    • Tyler Sticka
    • Paul Ledbrook
    • Juan
    • Juan
    • Scott Jordan
    • Amateur Technologist
    • Tyler Sticka tylersticka.com
      @aaronpk Neat! You can also pull this off with a little bit of SVG: codepen.io/tylersticka/pe…
      Sat, Aug 13, 2016 7:59pm +00:00 (via brid-gy.appspot.com)

    Other Mentions

    • cloudfour.com
      Thu, Aug 18, 2016 11:11am -07:00
Posted in /articles

Hi, I'm Aaron Parecki, Director of Identity Standards at Okta, and co-founder of IndieWebCamp. I maintain oauth.net, write and consult about OAuth, and participate in the OAuth Working Group at the IETF. I also help people learn about video production and livestreaming. (detailed bio)

I've been tracking my location since 2008 and I wrote 100 songs in 100 days. I've spoken at conferences around the world about owning your data, OAuth, quantified self, and explained why R is a vowel. Read more.

  • Director of Identity Standards at Okta
  • IndieWebCamp Founder
  • OAuth WG Editor
  • OpenID Board Member

  • 🎥 YouTube Tutorials and Reviews
  • 🏠 We're building a triplex!
  • ⭐️ Life Stack
  • ⚙️ Home Automation
  • All
  • Articles
  • Bookmarks
  • Notes
  • Photos
  • Replies
  • Reviews
  • Trips
  • Videos
  • Contact
© 1999-2025 by Aaron Parecki. Powered by p3k. This site supports Webmention.
Except where otherwise noted, text content on this site is licensed under a Creative Commons Attribution 3.0 License.
IndieWebCamp Microformats Webmention W3C HTML5 Creative Commons
WeChat ID
aaronpk_tv