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 includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                	<div class="container">
		  <div class="box" data-width="1" data-height="3">A</div>
  		<div class="box" data-width="2" data-height="3">B</div>
  		<div class="box" data-width="5" data-height="3">C</div>
  		<div class="box" data-width="1" data-height="3">D</div>
  		<div class="box" data-width="3" data-height="4">E</div>
  		<div class="box" data-width="3" data-height="5">F</div>
  		<div class="box" data-width="6" data-height="2">G</div>
  		<div class="box" data-width="3" data-height="3">H</div>
  		<div class="box" data-width="1" data-height="4">I</div>
  		<div class="box" data-width="3" data-height="4">J</div>
  		<div class="box" data-width="2" data-height="3">K</div>
  		<div class="box" data-width="3" data-height="6">L</div>
  		<div class="box" data-width="2" data-height="5">M</div>
  		<div class="box" data-width="1" data-height="1">N</div>
  		<div class="box" data-width="5" data-height="4">O</div>
  		<div class="box" data-width="2" data-height="5">P</div>
	</div>
              
            
!

CSS

              
                body, html {
	margin: 0;
	padding: 0;
	background: #353234;
	min-width: 1024px;
}

.container {
	position: relative;
	width: 100%;
	background: lime;
	/*top: 50%;
	-webkit-transform: translateY(-50%);
	-ms-transform: translateY(-50%);
	transform: translateY(-50%);*/
	min-width: 1024px;
}

.box {
	-webkit-box-sizing: border-box;
	-moz-box-sizing: border-box;
	box-sizing: border-box;

	position: absolute;
	border: 1px solid black;
	background: rgba(0,0,0,0.25);

	opacity: 0;
}

.box--shown {
	transition: opacity 1s;
	opacity: 1;
}
              
            
!

JS

              
                var pythagoras = {

	/**
	 * Position all boxes in the container
	 */
	positionBoxes : function($container, $boxes, numCols, numRows, heightFactor) {

		// Define width and height of boxes
		var boxSizeWidth = 100 / numCols;
		// var boxSizeHeight = 100 / numRows;
		var boxSizeHeight = ((boxSizeWidth * heightFactor) / 100 * $(window).width());

		// Build empty grid to work on
		var grid = [];
		for (var i = 0; i < numRows; i++) {
			grid.push(new Array(numCols+1).join('0').split(''));
		}

		// Loop all boxes
		$boxes.each(function() {

			// Get the box along with it's (proportional) height and width
			var $box = $(this),
				width = $box.data('width') || 1,
				height = $box.data('height') || 1;

			// Find a place in the grid for the box and inject it there
			yLoop:
			for (var y = 0; y < numRows; y++) {
				xLoop:
				for (var x = 0; x < numCols; x++) {

					// Extract the cell
					var cell = grid[y][x];
					console.log('checking cell ' + x + ',' + y + ' with value ' + cell);

					// Cell isn't taken yet
					if (cell == 0) {

						// Check if there's enough space on the X-axis
						for (var i = 0; i < width; i++) {
							if (grid[y][x+i] != 0) {
								continue xLoop;
							}
						}

						// Check if there's enough space on the Y-axis
						for (var j = 0; j < height; j++) {
							if (grid[y+j][x] != 0) {
								continue yLoop;
							}
						}

						// All seems ok: lock it in the grid
						for (var i = 0; i < width; i++) {
							for (var j = 0; j < height; j++) {
								grid[y+j][x+i] = 'X';
							}
						}

						$box.css({
							width: (boxSizeWidth * width) + '%',
							height: (boxSizeHeight * height) + 'px',
							left: (boxSizeWidth * x) + '%',
							top: (boxSizeHeight * y) + 'px'
						}).addClass('box--shown');

						// store X and Y on box
						$box.data('x', x);
						$box.data('y', y);

						break yLoop;

					}

				}
			}

		});

		pythagoras._fixContainerHeight($container, numCols, numRows, heightFactor);

	},


	fixHeights: function($container, $boxes, numCols, numRows, heightFactor) {
		pythagoras._fixBoxesHeight($container, $boxes, numCols, numRows, heightFactor);
		pythagoras._fixContainerHeight($container, numCols, numRows, heightFactor);
	},

	_fixBoxesHeight: function($container, $boxes, numCols, numRows, heightFactor) {

		var windowWidth = Math.max($(window).width(), $container.width());

		// Fix all box heights (width goes automatically via CSS %)
		$boxes.each(function() {
			var $box = $(this),
				height = $box.data('height') || 1,
				y = $box.data('y');

			var boxSizeWidth = 100 / numCols;
			var boxSizeHeight = ((boxSizeWidth * heightFactor) / 100 * windowWidth);

			$box.css({
				height: (boxSizeHeight * height) + 'px',
				top: (boxSizeHeight * y) + 'px',
			});
		});

	},

	/**
	 * Fix the height of the container when all boxes are positioned inside it
	 * Because we use absolute positioning, we need to do this.
	 * @param  jQuery $container     The element wrapping all boxes
	 * @param  int numCols           [description]
	 * @param  int numRows           [description]
	 * @param  double heightFactor   [description]
	 * @return void
	 */
	_fixContainerHeight : function($container, numCols, numRows, heightFactor) {

		var windowWidth = Math.max($(window).width(), $container.width());

		// Fix container height
		$('.container').css({
			height: windowWidth * heightFactor * numRows / numCols + 'px'
		});

	}

};

// Thunderbirds are go!
jQuery(function($) {

  // Config
  var numCols = 12,
    numRows = 13,
    heightFactor = 0.45,
    $container = $('.container'),
    $boxes = $container.find('.box');

  pythagoras.positionBoxes($container, $boxes, numCols, numRows, heightFactor);

  $(window).on('resize', function() {
    pythagoras.fixHeights($container, $boxes, numCols, numRows, heightFactor);
  }); // @TODO: throttle this
  
})
              
            
!
999px

Console