<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sonny Zhang on Medium]]></title>
        <description><![CDATA[Stories by Sonny Zhang on Medium]]></description>
        <link>https://medium.com/@sonnyzhang_?source=rss-26c635cc2195------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*1S1Csil2DYhJo5JN.jpg</url>
            <title>Stories by Sonny Zhang on Medium</title>
            <link>https://medium.com/@sonnyzhang_?source=rss-26c635cc2195------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 17:42:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sonnyzhang_/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[CSS: How to easily build a thumbnail gallery involving various image sizes]]></title>
            <link>https://medium.com/@sonnyzhang_/css-how-to-easily-build-a-thumbnail-gallery-involving-various-image-sizes-869d9ce75528?source=rss-26c635cc2195------2</link>
            <guid isPermaLink="false">https://medium.com/p/869d9ce75528</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Sonny Zhang]]></dc:creator>
            <pubDate>Tue, 02 May 2017 03:51:58 GMT</pubDate>
            <atom:updated>2017-05-02T03:52:54.054Z</atom:updated>
            <content:encoded><![CDATA[<p>As a front-end developer in training, one of the more common component of a website I’ve had to learn to make is a thumbnail gallery. At first glance this seemed simple to me, utilize a flexbox container and throw in some images nested in anchor tags and voila, a finished gallery. One problem that soon became apparent however is when you are working with images of varying dimensions, like this for example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nErOD4Fw7C0QCy0XmAdnXQ.png" /><figcaption>A collection of images with different aspect ratios</figcaption></figure><p>To demonstrate the problem, let’s first put these inside a 3x2 flexbox container:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/1*YnH_ksSS2v6e7XP8TkgScA.png" /><figcaption>The HTML</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*Te71ExLepdp5umuVPoE-Qg.png" /><figcaption>The CSS</figcaption></figure><p>This gets us halfway there to a thumbnail gallery, but obviously the images are way too zoomed in and not centered:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nS6BNPF4kmCMK0nF1rIqow.png" /><figcaption>Where did the dog go?? :(</figcaption></figure><p>Giving the images 100% width still doesn’t solve the problem, thanks to their different aspect ratios:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8sILFh-pwf3MA5_OVz1I3Q.png" /></figure><p>One easy solution for this problem is to have these images set as background images instead, and then use background-size: cover along with background-position-x: 50% and background-position-y: 50% to center it. However we often don’t want to do this, one reason being we no longer have alt texts available for each image, and it could also affect the SEO of our website. So how do we fit the images into our grid without stretching them and also keep them centered?</p><p>It was at this point that I began trying all sorts of convoluted methods to try to resize and center the images, and either it didn’t give me the results I was looking for or it worked but involved way too much CSS than it seemed was needed. Finally I came across object-fit: cover , which along with explicit height and width will resize our images to the perfect fit and center them:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/204/1*WqUJkAaLR-_ey7Is-okFkA.png" /><figcaption>Only three lines of code :)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JIYs_g7tidduZWajjMPqCw.png" /><figcaption>Ta-da!</figcaption></figure><p>The object-fit property is quite similar to the background-size property, allowing us to use cover, contain, or other values to size our images as needed. In our case, cover along with 100% height and width does exactly what we are looking for <em>(this is very important, you need to set an explicit height and width for </em><em>object-fit to work)</em>, with the added bonus of centering our images for us so no additional CSS is needed. Well there you have it, how to build a thumbnail gallery with perfectly sized and centered images, hope you found it helpful!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=869d9ce75528" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>