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 esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM 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.
<!-- 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 -->
@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;
}
}
// 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}`;
}
Also see: Tab Triggers