HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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>
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;
}
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
})
Also see: Tab Triggers