Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <main>
    <h1>Flexible, fluid photoset (advanced)</h1>
    <p>The photoset is fluid: try resizing your browser. Dealing with margins and photos of different aspect ratios is also never a problem. Made by <a rel="author" href="http://terrymun.com" title="Visit me">Terry</a>.</p>
  <p>This example hides the original <code>&lt;img&gt;</code> element and instead assigns the image source as a background image to the <code>&lt;a&gt;</code> element instead. This allows us to dictate the ratio to use, without distorting the original image. Try resizing your browser, and see magic happens!</p>
    <hr />
	<div class="photoset">
		<div class="photoset-row">
			<figure class="photoset-item">
				<a href="http://500px.com/photo/4772670"><img src="https://i.imgur.com/uh5YLj5.jpg" alt="ARoS Aarhus Kunstmuseum" title="ARoS Aarhus Kunstmuseum" /></a>
				<figcaption>ARoS Aarhus Kunstmuseum. Photo &copy; Terry Mun</figcaption>
			</figure>
		</div>
		<div class="photoset-row">
			<figure class="photoset-item">
				<a href="http://500px.com/photo/4820790"><img src="https://i.imgur.com/24hrPQn.jpg" alt="A lady walks briskly on a train platform in Bern, Switzerland." title="A lady walks briskly on a train platform in Bern, Switzerland." /></a>
				<figcaption>A lady walks briskly on a train platform in Bern, Switzerland. Photo &copy; Terry Mun</figcaption>
			</figure>
			<figure class="photoset-item">
				<a href="http://500px.com/photo/20245695"><img src="https://i.imgur.com/BXo4qAz.jpg" alt="Inside Rundetårn, Copenhagen." title="Inside Rundetårn, Copenhagen." /></a>
				<figcaption>Inside Rundetårn, Copenhagen. Photo &copy; Terry Mun</figcaption>
			</figure>
		</div>
		<div class="photoset-row">
			<figure class="photoset-item">
				<a href="http://500px.com/photo/39822810"><img src="https://i.imgur.com/Vo922iy.jpg" alt="By Twilight." title="By Twilight." /></a>
				<figcaption>By Twilight. Photo &copy; Terry Mun</figcaption>
			</figure>
			<figure class="photoset-item">
				<a href="http://500px.com/photo/35823988"><img src="https://i.imgur.com/80WaVuY.jpg" alt="God fest på Mejlgade." title="God fest på Mejlgade." /></a>
				<figcaption>God fest på Mejlgade. Photo &copy; Terry Mun</figcaption>
			</figure>
			<figure class="photoset-item">
				<a href="http://500px.com/photo/13998781"><img src="https://i.imgur.com/uDPcnM3.jpg" alt="Sit Back" title="Sit Back" /></a>
				<figcaption>Sit Back. Photo &copy; Terry Mun</figcaption>
			</figure>
		</div>
    <div class="photoset-row">
      <figure class="photoset-item">
        <a href="http://500px.com/photo/5889975"><img src="https://i.imgur.com/esWWGbF.jpg" alt="Basilique du Sacré-Cœur." title="Basilique du Sacré-Cœur." /></a>
        <figcaption>Basilique du Sacré-Cœur. Photo &copy; Terry Mun</figcaption>
      </figure>
      <figure class="photoset-item">
        <a href="http://500px.com/photo/4772520"><img src="https://i.imgur.com/ZCogT10.jpg" alt="Cyclist in Berlin" title="Cyclist in Berlin" /></a>
        <figcaption>Cyclist in Berlin. Photo &copy; Terry Mun</figcaption>
      </figure>
    </div>
    <div class="photoset-row">
      <figure class="photoset-item">
        <a href="http://500px.com/photo/5489150"><img src="https://i.imgur.com/UvGHJjo.jpg" alt="At the annual sailing race in Aarhus University." title="At the annual sailing race in Aarhus University." /></a>
        <figcaption>At the annual sailing race in Aarhus University. Photo &copy; Terry Mun</figcaption>
      </figure>
      <figure class="photoset-item">
        <a href="http://500px.com/photo/5889990"><img src="https://i.imgur.com/rWuQotb.jpg" alt="Dark corridors." title="Dark corridors." /></a>
        <figcaption>Dark corridors. Photo &copy; Terry Mun</figcaption>
      </figure>
    </div>
	</div>
</main>
              
            
!

CSS

              
                html {
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
	font-size: 16px;
}
body {
  color: #333;
  line-height: 1.5em;
  margin-bottom: 1.5em;
}
main {
	margin: 0 auto;
  padding: 1.5rem 0;
	width: 80%;
}
h1 {
  font-weight: bold;
  font-size: 2em;
  line-height: 1.5em;
  text-align: center;
}
p {
  margin-bottom: 1.5rem;
}
hr {
  border: none;
  border-top: .125rem solid #ddd;
  margin-bottom: 1.375rem;
}
a {
  border-bottom: 2px solid #333;
  color: #333;
  text-decoration: none;
  transition: all .125s ease-in-out;
}
a:hover{
  border-bottom-color: #4a7298;
  color: #4a7298;
}

/* General photoset style */
 .photoset {
	overflow: hidden;
	width: 100%;
}
.photoset .photoset-row {
	margin-bottom: .5rem;
	overflow: hidden;
  width: 150%;
}
	.photoset .photoset-row:last-child { margin: 0; }
.photoset .photoset-item {
	display: block;
	float: left;
	margin: 0 .25rem;
}
	.photoset .photoset-item:first-child { margin-left: 0; }
	.photoset .photoset-item:last-child  { margin-right: 0; }
.photoset figure {
	margin: 0;
	overflow: hidden;
	position: relative;
}
.photoset figcaption {
	background-color: rgba(255, 255, 255, .75);
	box-sizing: border-box;
  font-size: .75rem;
	padding: .5rem;
	position: absolute;
	bottom: 0;
	left: 0;
	width: 100%;
	-webkit-transform: translateY(100%);
	transform: translateY(100%);
	transition: all .5s ease-in-out;
}
.photoset-item a {
  background-position: center center;
  background-size: cover;
  border: 0;
  display: block;
  position: relative;
  width: 100%;
  height: 100%;
}
.photoset .photoset-item:hover a + figcaption {
	-webkit-transform: translateY(0);
	transform: translateY(0);
}
.photoset img {
	display: block;
  max-width: 100%;
	transition: all .25s ease-in-out;
}

@media screen
and (min-width: 480px)
and (max-width: 768px) { 
  .photoset figcaption {
    display: none;
  }
}

@media screen
and (max-width: 480px) { 
  .photoset .photoset-row {
    width: 100%;
  }
  .photoset .photoset-item {
    float: none;
    margin: 0 0 .75em 0;
  }
  .photoset figcaption {
    display: block;
    -webkit-transform: none;
    transform: none;
    position: relative;
  }
  .photoset img {
    max-width: 100%;
  }
}
              
            
!

JS

              
                (function($,sr){

  // debouncing function from John Hann
  // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
  var debounce = function (func, threshold, execAsap) {
      var timeout;

      return function debounced () {
          var obj = this, args = arguments;
          function delayed () {
              if (!execAsap)
                  func.apply(obj, args);
              timeout = null;
          };

          if (timeout)
              clearTimeout(timeout);
          else if (execAsap)
              func.apply(obj, args);

          timeout = setTimeout(delayed, threshold || 100);
      };
  }
  // smartresize 
  jQuery.fn[sr] = function(fn){  return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };

})(jQuery,'smartresize');

var loadComplete = false;

/* Wait for DOM to be ready */
$(function() {
  
	// Detect resize event
	$(window).smartresize(function () {
    // If loading is complete, hide images
    if(loadComplete) $('.photoset img').hide();
    
    // Detect viewport size
    if($(window).width() > 768) {
      
		// Set photoset image size
		$('.photoset-row').each(function () {
			var $pi    = $(this).find('.photoset-item'),
				  cWidth = $(this).parent('.photoset').width();

			// Generate array containing all image aspect ratios
			var ratios = $pi.map(function () {
				return $(this).find('img').data('org-width') / $(this).find('img').data('org-height');
			}).get();

			// Get sum of widths
			var sumRatios = 0, sumMargins = 0,
          minRatio  = Math.min.apply(Math, ratios);
			for (var i = 0; i < $pi.length; i++) {
				sumRatios += ratios[i]/minRatio;
			};
      
      $pi.each(function (){
        sumMargins += parseInt($(this).css('margin-left')) + parseInt($(this).css('margin-right'));
      });

			// Calculate dimensions
			$pi.each(function (i) {
				var minWidth = (cWidth - sumMargins)/sumRatios;
				$(this).find('a')
          .height(Math.floor(minWidth/minRatio))
					.width(Math.floor(minWidth/minRatio) * ratios[i]);
			});
		});
    } else if($(window).width() <= 768 && $(window).width() > 480) {
      $('.photoset-row').each(function (){
        var $pi = $(this).find('.photoset-item'),
            cWidth = $(this).parent('.photoset').width(),
            sumMargins = 0;
        
        $pi.each(function (){
          sumMargins += parseInt($(this).css('margin-left')) + parseInt($(this).css('margin-right'));
        });
        
        $pi.each(function (){
          $(this).find('a')
            .width((cWidth-sumMargins)/$pi.length)
            .height((cWidth-sumMargins)/$pi.length);
        });
      });
    } else {
      $('.photoset-item').each(function (){
        $(this).find('img').show();
        $(this).find('a').css({
          width: '100%',
          height: 'auto'
        });
      });
    }
	});
});

/* Wait for images to be loaded */
$(window).load(function () {

  // Indicate loading is complete
  loadComplete = true;
  
	// Store original image dimensions
	$('.photoset-item img').each(function () {
      $(this)
        .data('org-width', $(this)[0].naturalWidth)
        .data('org-height', $(this)[0].naturalHeight)
        .hide()
        .parent('a')
        .css({
          "background-image": "url("+$(this).attr('src')+")"
        });
	});

  $(window).resize();
});
              
            
!
999px

Console