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

              
                <div class="game">
  <div class="game__question"></div>
  <ul class="game__options">
    <li class="game__option game__option--skeleton"></li>
    <li class="game__option game__option--skeleton"></li>
    <li class="game__option game__option--skeleton"></li>
  </ul>
  <div class="game__stage"></div>
</div>
              
            
!

CSS

              
                body {
  font-family: sans-serif;
  margin: auto;
  background-color: #eaeaea;
  color: #555;
}

.game {
  max-width: 600px;
  margin: auto;
  margin-top: 20px;
  border-radius: 5px;
  background-color: #f7f7f7;
  box-shadow: 0 1px 5px 2px #e0e0e0;
  padding: 20px 30px;
}

.game__question {
  color: #555;
  font-size: 1.2em;
  padding: 5px 0;
  position: relative;
}

.game__question:empty,
.game__stage:empty {
  height: 1.2em;
}

.game__question:empty::before,
.game__stage:empty::before {
  content: "";
  position: absolute;
  height: 100%;
  width: 100%;
  background-color: #e0e0e0;
}

.game__options {
  display: flex;
  list-style: none;
  margin: 0 -10px;
  margin-top: 15px;
  padding: 0;
}

.game__option {
  flex: 1 1 0;
  margin: 0 10px;
  border-radius: 5px;
  cursor: pointer;
  height: 150px;
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

.game__option > img {
  max-width: 100%;
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}

.game__option--error {
  position: relative;
}

.game__option--error::before {
  content: "";
  background-size: cover;
  background-image: url("data:image/svg+xml,%0A%3Csvg height='24' width='24' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M22 13c0 5.523-4.477 10-10 10S2 18.523 2 13 6.477 3 12 3s10 4.477 10 10z' fill='%23c0392b'/%3E%3Cpath d='M22 12c0 5.523-4.477 10-10 10S2 17.523 2 12 6.477 2 12 2s10 4.477 10 10z' fill='%23e74c3c'/%3E%3Cpath d='M7.05 9.4l3.536 3.6-3.536 3.5 1.415 1.4L12 14.4l3.536 3.5 1.414-1.4-3.536-3.5 3.536-3.6L15.536 8 12 11.5 8.465 8 7.05 9.4z' fill='%23c0392b'/%3E%3Cpath d='M7.05 8.4l3.536 3.6-3.536 3.5 1.415 1.4L12 13.4l3.536 3.5 1.414-1.4-3.536-3.5 3.536-3.6L15.536 7 12 10.5 8.465 7 7.05 8.4z' fill='%23ecf0f1'/%3E%3C/svg%3E");
  width: 70px;
  height: 70px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.game__option:hover {
  box-shadow: 0 0 1px 4px inset #b9f;
}

.game__option--skeleton {
  background-color: #e0e0e0;
}

.game__stage {
  position: relative;
  margin-top: 15px;
  font-size: 0.9em;
}
              
            
!

JS

              
                const questions = [
  {
    title: "На какой из картинок изображён котик?",
    correctAnswer: 0,
    options: [
      {
        imageSrc: "https://habrastorage.org/webt/m_/bm/z2/m_bmz2s5fjyzrosnkg8njszwlzc.png",
        imageAlt: "Котик"
      },
      {
        imageSrc: "https://habrastorage.org/webt/70/7a/6u/707a6uritcrzezgmrlp7whkdv0w.png",
        imageAlt: "Собакен"
      },
      {
        imageSrc: "https://habrastorage.org/webt/tm/nf/zx/tmnfzxdew58uhvczizacmmzgw6a.png",
        imageAlt: "Енотик"
      }
    ]
  },
  {
    title: "На какой из картинок изображён енотик?",
    correctAnswer: 2,
    options: [
      {
        imageSrc: "https://habrastorage.org/webt/70/7a/6u/707a6uritcrzezgmrlp7whkdv0w.png",
        imageAlt: "Собакен"
      },
      {
        imageSrc: "https://habrastorage.org/webt/m_/bm/z2/m_bmz2s5fjyzrosnkg8njszwlzc.png",
        imageAlt: "Котик"
      },
      {
        imageSrc: "https://habrastorage.org/webt/tm/nf/zx/tmnfzxdew58uhvczizacmmzgw6a.png",
        imageAlt: "Енотик"
      }
    ]
  },
  {
    title: "На какой из картинок изображён песель?",
    correctAnswer: 1,
    options: [
      {
        imageSrc: "https://habrastorage.org/webt/tm/nf/zx/tmnfzxdew58uhvczizacmmzgw6a.png",
        imageAlt: "Енотик"
      },
      {
        imageSrc: "https://habrastorage.org/webt/70/7a/6u/707a6uritcrzezgmrlp7whkdv0w.png",
        imageAlt: "Собакен"
      },
      {
        imageSrc: "https://habrastorage.org/webt/m_/bm/z2/m_bmz2s5fjyzrosnkg8njszwlzc.png",
        imageAlt: "Котик"
      }
    ]
  }
];

const referenceLength = questions.length;
const game = document.querySelector(".game");
const gameQuestion = document.querySelector(".game__question");
const gameOptions = document.querySelector(".game__options");
const gameStage = document.querySelector(".game__stage");

const renderQuestion = question => {
  gameQuestion.textContent = question.title;
  gameOptions.innerHTML = question.options
    .map(option => `<li class="game__option"><img src="${option.imageSrc}" alt="${option.imageAlt}"></li>`)
    .join('');
}

const renderStage = () => {
  gameStage.textContent = `Вопрос: ${referenceLength - questions.length + 1} / ${referenceLength}`;
}

const renderResults = () => {
  game.innerHTML = 'Малаца! Всё прошёл.';
}

document.addEventListener("DOMContentLoaded", () => {
  renderQuestion(questions[0]);
  renderStage();
});

document.addEventListener("click", event => {
  if (!event.target.closest(".game__option")) return;
  
  const answerIndex = [...event.target.parentNode.children].indexOf(event.target);
  
  if (questions[0].correctAnswer == answerIndex) {
    questions.shift();
    
    if (!questions.length) {
      return renderResults();
    }
    
    renderQuestion(questions[0]);
    renderStage();
  } else {
    event.target.classList.add("game__option--error");
    setTimeout(() => event.target.classList.remove("game__option--error"), 2000);
  }
});
              
            
!
999px

Console