cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="gallery">
  <img src="https://31.media.tumblr.com/tumblr_loumsfBCuE1qeo682o1_500.jpg">
  <img src="https://24.media.tumblr.com/tumblr_m1yru9PNMV1qeo682o1_500.jpg">
  <img src="https://24.media.tumblr.com/tumblr_m24ic6kY6Q1qeo682o1_500.jpg">
  <img src="https://31.media.tumblr.com/tumblr_lriz0lDN2y1qeo682o1_500.jpg">
  <img src="https://24.media.tumblr.com/tumblr_lrmgjaL62O1qeo682o1_500.jpg">
  <img src="https://24.media.tumblr.com/tumblr_lqtwuiEf8j1qeo682o1_r1_500.jpg">
  <img src="https://31.media.tumblr.com/tumblr_lqsdws3ihB1qeo682o1_500.jpg">
  <img src="https://31.media.tumblr.com/tumblr_lqscbwxUt51qeo682o1_500.jpg">
  <img src="https://31.media.tumblr.com/tumblr_lq6hl0PhyN1qeo682o1_500.jpg">
  <img src="https://24.media.tumblr.com/tumblr_lo6iddvEi51qeo682o1_500.jpg">
</div>
            
          
!
            
              body {
  padding: 2px 0 0 2px;
}

.gallery img {
  float: left;
  padding: 0 2px 2px 0;
  /* Yes, you *can* adjust/remove padding. */
}
            
          
!
            
              function scaleGallery()
{
  // This is roughly the max pixels width/height of a square photo
  var widthSetting = 400;
  
  // Do not edit any of this unless you know what you're doing
  var containerWidth = $(".gallery").width();
  var ratioSumMax = containerWidth / widthSetting;
  var imgs = $(".gallery img");
  var numPhotos = imgs.length, ratioSum, ratio, photo, row, rowPadding, i = 0;

  while (i < numPhotos) {
    ratioSum = rowPadding = 0;
    row = new Array();
    while (i < numPhotos && ratioSum < ratioSumMax) {
        photo = $(imgs[i]);
        // reset width to original
        photo.width(""); 
        ratio = photo.width() / photo.height();
        rowPadding += getHorizontalPadding(photo);
        // if this is going to be first in the row, clear: left
        if(ratioSum == 0) photo.css("clear", "left"); else photo.css("clear", "none");
        ratioSum += ratio;
        row.push(photo);
        i++;
        // if only 1 image left, squeeze it in
        if(i == numPhotos - 1) ratioSumMax = 999;
    }
    unitWidth = (containerWidth - rowPadding) / ratioSum;

    row.forEach(function (elem) {
      elem.width(unitWidth * elem.width() / elem.height());
    });
  }
}

function getHorizontalPadding(elem)
{
    var padding = 0;
    var left = elem.css("padding-left");
    var right = elem.css("padding-right");
    padding += parseInt(left ? left.replace("px", "") : 0);
    padding += parseInt(right ? right.replace("px", "") : 0);
    return padding;
}

$(window).load(scaleGallery);
$(window).resize(scaleGallery);
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console