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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <header class="masthead">
        <div class="site-branding">
            <h1 class="site-title">Grid Image Gallery with Element Queries</h1>
            <p class="site-description">A simple image gallery leveraging CSS Grid and Element Queries to change its appearance based on how many images are displayed. Images are fitted in their respective cells using flex and object-fit.</p>
        </div><!-- .site-branding -->
    </header><!-- .masthead -->

    <section class="the-boxes">
      <nav class="interface">
        <button id="more" class="button">Add image</button>
        <button id="less" class="button">Remove image</button>
      </nav>
      <ul class="box-grid">
        <li>
          <img src="https://source.unsplash.com/vMRMYoLQzwo/1200x800" >
        </li>        
      </ul>
    </section><!-- .the-grid -->

            
          
!

CSS

            
              /*--------------------------------------------------------------
Global
--------------------------------------------------------------*/
body {
  max-width: 60em;
	margin: 1em auto;
	color: black;
	font-family: "Fira Code", monospace;
	font-size: 18px;
	line-height: 1.5;
}

img {
  width: 100%;
  height: auto;
  object-fit: cover;
}

.masthead {
	text-align: center;
}

.site-description {
  max-width: 40em;
  margin: 0 auto;
}

.interface {
  display: flex;
  justify-content: center;
  margin-top: 1em;
}

.button {
  border: 5px solid black;
  border-radius: .75em;
  padding: .7em 1.25em;
  margin: 1em .5em 0;
  font-size: 1em;
  font-weight: bold;
}

.button:focus,
.button:hover {
  background: black;
  color: white;
}

.box-grid {
  margin: 2em;
  padding: .5em;
	border: 5px solid #41444B;
}

/*--------------------------------------------------------------
Box styles
--------------------------------------------------------------*/

.box-grid li {
  display: flex;
  list-style-type: none;
}

/* Base grid */
.box-grid {
	display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
	justify-content: center;
  align-content: center;
	grid-gap: .5em;
}

/* Two columns */
@element '.box-grid' and (min-children: 2) {
  :self {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* Three items */
@element '.box-grid' and (min-children: 3) and (max-children: 3) {
  :self li:first-child {
    grid-column: span 2;
  }
}

/* Five items */
@element '.box-grid' and (min-children: 5) and (max-children: 5) {
  :self li:first-child {
    grid-row: span 2;
  }
}

/* Six items */
@element '.box-grid' and (min-children: 6) and (max-children: 6) {
  :self {
    grid-template-columns: repeat(3, 1fr);
  }
  :self li:first-child {
    grid-row: span 2;
  }
  :self li:nth-child(2) {
    grid-column: span 2;
  }
  :self li:nth-child(4) {
    grid-row: span 2;
  }
}

/* Seven items */
@element '.box-grid' and (min-children: 7) and (max-children: 7) {
  :self {
    grid-template-columns: repeat(3, 1fr);
  }
  :self li:first-child {
    grid-column: span 2;
    grid-row: span 2;
  }
  :self li:nth-child(4) {
    grid-row: span 2;
  }
  :self li:last-child {
    grid-column: span 2;  
  }
}

/* Eight items */
@element '.box-grid' and (min-children: 8) and (max-children: 8) {
  :self {
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
  }
  :self li:first-child {
    grid-column: span 2;
    grid-row: span 2;
  }
  :self li:nth-child(3) {  
    grid-row: span 2;
  }
  :self li:nth-child(4) {
    grid-row: span 2;
  }
  :self li:nth-child(5) {
    grid-column: span 2;
  }
  :self li:nth-child(6) {
    grid-row: span 2;
  }
  :self li:last-child {
    grid-column: span 2;
  }
}
            
          
!

JS

            
              const GRID = document.querySelector('.box-grid');
const MORE = document.querySelector('#more');
const LESS = document.querySelector('#less');
var count = 0;
const IMAGES = [
  'https://source.unsplash.com/xkMArBuwK44/1200x800',
  'https://source.unsplash.com/lr81rOg-gys/1200x800',
  'https://source.unsplash.com/KUfkX6gVwBU/1200x800',
  'https://source.unsplash.com/i9FLJwYhVQs/1200x800',
  'https://source.unsplash.com/crVO0UMdoVU/1200x800',
  'https://source.unsplash.com/w3lQVmuK8fw/1200x800',
  'https://source.unsplash.com/S2YssLw97l4/1200x800',
  'https://source.unsplash.com/5d2ETacuSgw/1200x800',
  'https://source.unsplash.com/S2YssLw97l4/1200x800'
];

function addItem() {
  let listItem = document.createElement("li");
  let newFigure = document.createElement("figure");
  let newImage = new Image();
  newImage.src = IMAGES[count];

  listItem.append(newImage);         
  GRID.append(listItem);
  ++count;
}

function removeItem() {
  let list = GRID.querySelectorAll('li');
  list[list.length - 1].remove();
  count--;
}

MORE.addEventListener('click', addItem, false);
LESS.addEventListener('click', removeItem, false);

            
          
!
999px

Console