Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ 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

Auto Save

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

              
                <!-- nest in a wrapping container 
  one div for each question and connected answer(s)
  a header in which to show the result of the small quiz
-->
<div class="container">

  <!-- each question in this "faq"  comprises a details element
    in said details element:
      a summary element provides the actual question
      a div contains then four possible answers

    in the JS script, upon choosing an answer, the correct answer is applied a class which visually remarks its rightful nature
  -->

  <div class="question">
    <details>
      <summary>
        <h2>Which framework supports this project?</h2>
      </summary>
      <div class="answers">
        <div class="answer">
          Bulma
        </div>
        <div class="answer">
          Bootstrap
        </div>
        <div class="answer">
          Bibbity Bobbity Boo
        </div>
        <div class="answer correct-answer">
          None of the above
        </div>
      </div>
    </details>
  </div><!-- close question div -->

  <div class="question">
    <details>
      <summary>
        <h2>Which CSS property creates this background?</h2>
      </summary>
      <div class="answers">
        <div class="answer">
          mask
        </div>
        <div class="answer correct-answer">
          clip-path
        </div>
        <div class="answer">
          border
        </div>
        <div class="answer">
          background
        </div>
      </div>
    </details>
  </div><!-- close question div -->

  <div class="question">
    <details>
      <summary>
        <h2>Which color is <span class="highlight">not</span> used in this page?</h2>
      </summary>
      <div class="answers">
        <div class="answer">
          #422980
        </div>
        <div class="answer">
          #EB761F
        </div>
        <div class="answer correct-answer">
          #252525
        </div>
        <div class="answer">
          #00D539
        </div>
      </div>
    </details>
  </div><!-- close question div -->

  <div class="question">
    <details>
      <summary>
        <h2>Which font is used in this page?</h2>
      </summary>
      <div class="answers">
        <div class="answer correct-answer">
          Source Sans Pro
        </div>
        <div class="answer">
          Open Sans
        </div>
        <div class="answer">
          Lato
        </div>
        <div class="answer">
          Montserrat
        </div>
      </div>
    </details>
  </div><!-- close question div -->

  <div class="question">
    <details>
      <summary>
        <h2>Which layout system arranges the elements of this page?</h2>
      </summary>
      <div class="answers">
        <div class="answer">
          Grid
        </div>
        <div class="answer">
          Flexbox
        </div>
        <div class="answer">
          Neither
        </div>
        <div class="answer correct-answer">
          Both
        </div>
      </div>
    </details>
  </div><!-- close question div -->

  <h1 class="result"></h1>

</div><!-- close the wrapping .container div -->
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Source+Sans+Pro');

/* define variables for the font and colors used in the project */
:root {
  --font-main: 'Source Sans Pro', sans-serif;
  --color-bg: #422980;
  --color-txt: #EBEEF3;
  --color-accent: #EB761F;
  --color-accent-right-answer: #00D539;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
body {
  min-height: 100vh;
  width: 100%;
  font-family: var(--font-main);
  color: var(--color-txt);
  background: linear-gradient(to right, #5e3ead, var(--color-txt), #5e3ead);
}

.container {
  /* display the questions and the header in a single column layout */
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  padding: 1rem;
}

.container .question details summary {
  background: var(--color-bg);
  margin: 1rem;
  max-width: 550px;
  text-align: center;
  /* create the peculiar background by creating a polygon with clip-path */
  clip-path: polygon(10% 0, 90% 0, 100% 50%, 90% 100%, 10% 100%, 0 50%);
}
.container .question details summary h2 {
  padding: 1rem 3rem;
  font-size: 2rem;
}
.container .question details summary h2 .highlight {
  /* style highlighted words */
  text-transform: uppercase;
  text-decoration: underline;
}

.container .question details .answers {
  /* display the four answers in a 2x2 grid which occupies the entirety of the width and height allocated by the details element */
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  font-size: 1.4rem;
}

.container .question details .answer {
  background: var(--color-bg);
  clip-path: polygon(5% 0, 95% 0, 100% 50%, 95% 100%, 5% 100%, 0 50%);
  margin: 1rem;
  padding: 0.5rem 1.2rem;
  cursor: pointer;
  text-align: center;
}
.container .question details .answer:hover {
  background: var(--color-accent);  
}

/* remove the default triangle for every summmary element */
summary::-webkit-details-marker {
  display: none;
}

.container .question details .reveal-chosen-answer {
  /* change the background color for the answer which is given, but which is wrong (this class is applied in the JS script to the wrong answer) */
  background: var(--color-accent);
}

.container .question details .reveal-correct-answer {
  /* include an animation for the answer which is revealed to be correct (this class is applied in the JS script to the correct answer) */
  animation: correctAnswer 1s step-start forwards 4;
}
@keyframes correctAnswer {
  /* change background color from and to the chosen accent color
    as per step-start, the animation moves discretely from one value to another
    as per forwards, the animation retains the property described in the final keyframe
  */
  0%, 100% {
    background: var(--color-accent-right-answer);
  }
  50% {
    background: var(--color-bg);
  }
}

h1.result {
  background: var(--color-bg);
  clip-path: polygon(10% 0, 90% 0, 100% 50%, 90% 100%, 10% 100%, 0 50%);
  padding: 0.5rem 2rem;  
}

/* for small screen devices, include each of the four answers in a single row and reduce the size of the questions' header */
@media screen and (max-width:700px) {
  .container .question details summary h2 {
    font-size: 1.5rem;
  }
  .container .question details .answers {
    grid-template-columns: 1fr;
  }
}


              
            
!

JS

              
                // target all answers in the HTML document
const answers = document.querySelectorAll(".container .question .answers .answer");

// target the h1 header in which to include the results of the quiz
const headerResult = document.querySelector(".result");

// listen for a click event on each answer, at which point reveal the correct answer for the connected question
answers.forEach(answer => answer.addEventListener("click", revealAnswer));

// initialize two variables to keep track of 1) the number of answers given and 1) the number of correct answers
let counterAnswers = 0;
let counterCorrectAnswers = 0;

// declare a function to reveal the correct answer, count the number of correct answers and show a result once all questions are answered
function revealAnswer(e) {
    // consider the parent element of the clicked answer
    let parentElement = e.target.parentElement;
    // target, inside of the parent element, the correct answer
    let correctAnswer = parentElement.querySelector(".correct-answer");

    // if the correct element is not already revealed
    if(!correctAnswer.classList.contains("reveal-correct-answer")) {
        // add a class to the correct answer, visually informing the user of the right choice
        correctAnswer.classList.add("reveal-correct-answer");
        // if the chosen answer was the correct one, increment the correct answer's counter
        if(e.target == correctAnswer) {
            counterCorrectAnswers++;
        } 
        // else change the background color of the chosen answer
        else {
            e.target.classList.add("reveal-chosen-answer");
        }
    }

    // consider all answers of the chosen question and remove the event listener from all them, to avoid firing the function twice on the same question
    let answers = parentElement.querySelectorAll(".answer");
    answers.forEach(answer => {
        answer.removeEventListener("click", revealAnswer);
        answer.style.cursor = "default";
    });

    // increment the counter keeping track of the number of questions answered
    counterAnswers++;

    // once all questions are answered, call a function to display the result
    if(counterAnswers == 5) {
        displayResult(counterCorrectAnswers);
    }

}

// declare a function which takes as argument the number of correct answers and displays a message tailored to said number
function displayResult(number) {
    // initialize a string which includes a comment, tailored to how many correct answers were given
    let comment = '';
        switch(number) {
            case 5:
                comment = "😎";
                break;
            case 4:
            case 3:
                comment = "😉";
                break;
            case 2:
            case 1:
                comment = "😏";
                break;
                break;
            case 0:
                comment = "😶";
                break;
        }
        // include in the main header the result and the comment
        headerResult.textContent = `${number} Correct Answers ${comment}`;
}
              
            
!
999px

Console