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>
              
            
!

CSS

              
                div {
  box-sizing: border-box;
}

.container {
  display: grid;
  margin: 0 auto;
  /* 
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  transition: all 1s ease; 
  transition doesn't work on grid-template-columns / rows
  */
}

.container div {
/*    border: 1px solid white;  */
   transition: all 0.2s ease;
  background-size: cover;
}
              
            
!

JS

              
                /*Play with these constants*/
const columns = 3;
const rows = 3;
const factor = 0.8;

const containerWidth = columns < rows ? (columns / rows * 100): 100;
const containerHeight = rows < columns ? (rows / columns * 100): 100;
const squareSize = 100/Math.max(columns, rows);
const bigSide = factor * Math.min(containerWidth, containerHeight);

const container = document.getElementsByClassName('container')[0];

container.style.width = containerWidth + "vmin";
container.style.height = containerHeight + "vmin";
container.style.gridTemplateColumns = "repeat(" + columns + ", 1fr)";
container.style.gridTemplateRows = "repeat(" + rows + ", 1fr)";

let items = [];

for (let i = 1; i <= columns * rows; i++) {
  let item = document.createElement('div');
  item.id = 'item-'+i;
  item.className = 'item';
  // item.style.background = colorize();
  item.style.background = 'url('+picturize()+') no-repeat center center';
  item.style.backgroundSize = 'cover';
  item.addEventListener('mouseenter', function(){
    resize(this.id);
  })
  item.addEventListener('mouseleave', reset);
  container.appendChild(item);
  items.push(item);
}

function setAttribute (width, height, background) {
  return 'width:' + width + 'vmin;height: ' + height + 'vmin; background:' + background;
}

function resize (id) {
  let itemWidth;
  let itemHeight;
  let itemAttribute;
  let target = id.split('-')[1];
  
  for (let i=0; i < items.length; i++){
    let background = items[i].style.background
    let itemNr = items[i].id.split('-')[1];
    if (itemNr === target){
      //This is the hovered item
      itemWidth = bigSide;
      itemHeight = bigSide;
    } else if (Math.ceil(itemNr/columns) === Math.ceil(target/columns)){
      //This item is on the same row as the hovered item
      itemWidth = (containerWidth - bigSide) / (columns -1);
      itemHeight = bigSide
    } else if ((itemNr - target) % columns === 0) {
      //This item is in the same column as the hovered item
      itemWidth = bigSide
      itemHeight = (containerHeight - bigSide) / (rows -1);
    } else {
      itemWidth = (containerWidth - bigSide) / (columns -1);
      itemHeight = (containerHeight - bigSide) / (rows -1);
    }
    itemAttribute = setAttribute(itemWidth, itemHeight, background);
    items[i].setAttribute("style", itemAttribute);
  }
}

function reset () {
  for (let i = 0; i < items.length; i++) {
    const itemWidth = squareSize;;
    const itemHeight = squareSize;;
    let itemAttribute;
    const background = items[i].style.background;
    itemAttribute = setAttribute(itemWidth, itemHeight, background);
    items[i].setAttribute("style", itemAttribute);
  }
}

function colorize () {
  const letters = 'ABCDEF'.split('');
  let color = '#';
  for (let i = 0; i < 6; i++ ) {
    color += letters[Math.round(Math.random() * 5)];
  }
  return color;
}

function picturize (){
  let i = Math.floor(Math.random() * 10);
  const images = [
    'https://picsum.photos/300',
    'https://picsum.photos/301',
    'https://picsum.photos/302',
    'https://picsum.photos/303',
    'https://picsum.photos/304',
    'https://picsum.photos/305',
    'https://picsum.photos/306',
    'https://picsum.photos/307',
    'https://picsum.photos/308',
    'https://picsum.photos/309',
  ]
  return images[i]
} 
              
            
!
999px

Console