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.
<div id="app"></div>
@import url("https://fonts.googleapis.com/css2?family=Be+Vietnam+Pro:wght@300;400;500;600;700&display=swap");
$font: "Be Vietnam Pro", sans-serif;
$color: #1f8197;
* {
box-sizing: border-box;
font-family: $font;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
button {
font-family: $font;
}
body {
padding: 0 10vh;
background-color: #1c1d20;
background: rgb(149, 233, 227);
background: linear-gradient(
62deg,
rgba(149, 233, 227, 1) 30%,
rgba(31, 190, 210, 1) 41%,
rgba(11, 38, 50, 1) 85%
);
font-family: $font;
width: 100%;
height: 100vh;
overflow: hidden;
display: flex;
&:after {
content: "";
background-image: url(https://images.unsplash.com/photo-1623479322729-28b25c16b011?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1740&q=80);
position: absolute;
left: 0;
top: 0;
width: 100%;
z-index: -1;
height: 100%;
background-repeat: no-repeat;
transition: 0.3s;
background-size: cover;
background-attachment: fixed;
background-position: center;
}
@media (max-width: 980px), (max-height: 580px) {
padding: 60px 20px;
}
}
#app {
width: 100%;
height: 100%;
display: flex;
align-items: center;
flex-wrap: wrap;
justify-content: center;
}
.game-started:after {
transform: translateX(calc(-50% - 100px));
@media (max-width: 980px), (max-height: 580px) {
transform: scale(1.2);
}
}
.game {
position: relative;
max-width: 600px;
width: 100%;
height: 36vh;
min-height: max-content;
margin: 32vh auto;
box-shadow: 0 0 80px rgba(0, 0, 0, 1), 0 0 0 16px rgba(152, 236, 255, 0.1);
border-radius: 12px;
overflow: hidden;
scroll-behavior: smooth;
display: flex;
transition: 0.3s;
@media (max-width: 980px), (max-height: 580px) {
margin: auto;
height: 52vh;
flex-direction: column;
overflow: auto;
align-items: flex-start;
}
&[data-game-started] {
max-width: 1200px;
margin: 15vh auto;
height: 70vh;
@media (max-width: 980px), (max-height: 580px) {
height: auto;
overflow: auto;
margin: auto;
max-height: 100%;
}
.intro {
width: calc(50% - 100px);
transition: 0.3s;
@media (max-width: 980px), (max-height: 580px) {
width: 100%;
height: auto;
}
&-inner {
padding: 20px;
@media (max-width: 980px), (max-height: 580px) {
padding: 40px 20px;
}
}
}
.game-area {
flex-shrink: 0;
width: calc(50% + 100px);
display: flex;
align-items: center;
flex-wrap: wrap;
transition: 0.3s;
flex-grow: 1;
@media (max-width: 980px), (max-height: 580px) {
width: 100%;
padding: 20px;
}
}
}
}
.game-area {
background-color: #fff;
overflow: auto;
padding: 40px;
@media (max-width: 980px), (max-height: 580px) {
padding: 0;
}
}
.intro {
background-size: cover;
height: 100%;
width: 100%;
flex-shrink: 0;
color: #fff;
background-position: center;
position: relative;
display: flex;
align-items: center;
background: linear-gradient(
251deg,
#00b8ff52 -53%,
rgba(255, 255, 255, 0) 65%
);
justify-content: center;
backdrop-filter: blur(30px);
&-inner {
text-align: center;
padding: 30px 60px;
width: 100%;
}
&-title {
margin: 0;
font-size: 42px;
text-shadow: 1px 2px 0 rgba(0, 0, 0, 0.35);
}
&-desc {
opacity: 0.8;
font-weight: 300;
}
&-button {
background-color: $color;
border: 0;
padding: 12px 26px;
color: #fff;
border-radius: 4px;
font-size: 18px;
transform: translateY(20px);
cursor: pointer;
font-weight: 500;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}
}
.indicator {
display: flex;
justify-content: center;
position: relative;
max-width: 80%;
margin: 20px auto 0;
&-item {
width: 12px;
height: 4px;
border-radius: 6px;
& + & {
margin-left: 8px;
}
&:last-child:before {
background: #fff;
content: "";
width: 12px;
height: 4px;
position: absolute;
border-radius: 6px;
left: 0;
top: 0;
opacity: 0;
}
&:last-child:after {
content: "COMPLETED";
position: absolute;
width: 100%;
left: 0;
font-size: 14px;
letter-spacing: 1px;
font-weight: 600;
top: 0;
opacity: 0;
}
&:last-child[style*="255, 255)"]:before {
animation: indicator 1s both;
}
&:last-child[style*="255, 255)"]:after {
animation: indicator-completed 0.4s 0.8s both;
}
}
}
@keyframes indicator {
0%,
100% {
opacity: 1;
}
50% {
height: 6px;
}
100% {
height: 40px;
top: -2px;
left: -2px;
background-color: $color;
}
80% {
background-color: #fff;
}
50%,
100% {
width: calc(100% + 1px);
}
}
@keyframes indicator-completed {
70%,
100% {
opacity: 1;
}
100% {
transform: translatey(10px);
}
}
.question {
display: flex;
height: 100%;
flex-direction: column;
width: 100%;
&-inner {
margin: auto 0;
}
&-answers {
padding: 0;
list-style: none;
}
li,
&-text {
opacity: 0;
}
&-button {
background-color: $color;
border: 0;
padding: 14px 26px;
color: #fff;
border-radius: 4px;
margin-top: 20px;
font-size: 16px;
cursor: pointer;
font-weight: 500;
}
&-text {
color: #333;
margin: 20px 0;
}
input {
display: none;
}
label {
display: flex;
padding: 12px 24px 12px 16px;
font-size: 15px;
border-radius: 4px;
overflow: hidden;
align-items: center;
border: 2px solid #dde7eb;
color: #333;
cursor: pointer;
position: relative;
&:before {
height: 16px;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='24' height='24' stroke='%23333' stroke-width='3' fill='none' stroke-linecap='round' stroke-linejoin='round' class='css-i6dzq1'%3E%3Cpolyline points='20 6 9 17 4 12'%3E%3C/polyline%3E%3C/svg%3E");
content: "";
background-size: cover;
opacity: 0.5;
transition: 0.3s;
flex-shrink: 0;
position: absolute;
right: -16px;
top: 50%;
transform: translatey(-50%);
width: 16px;
}
}
li + li {
margin-top: 10px;
}
input:checked + label {
background-color: #dde7eb;
&:before {
transform: translatex(-24px) translatey(-50%);
}
}
}
.is-true label {
background-color: rgba($color, 0.2);
border-color: $color;
&:before {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='24' height='24' stroke='%231f8197' stroke-width='3' fill='none' stroke-linecap='round' stroke-linejoin='round' class='css-i6dzq1'%3E%3Cpolyline points='20 6 9 17 4 12'%3E%3C/polyline%3E%3C/svg%3E");
opacity: 1;
transform: translatex(-24px) translatey(-50%);
}
}
.correction {
width: 100%;
}
.correction [data-selected="true"].is-true label {
border-color: #0ca50c;
background-color: #a3ebab;
&:before {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='24' height='24' stroke='%230ca50c' stroke-width='3' fill='none' stroke-linecap='round' stroke-linejoin='round' class='css-i6dzq1'%3E%3Cpolyline points='20 6 9 17 4 12'%3E%3C/polyline%3E%3C/svg%3E");
opacity: 1;
transform: translatex(-24px) translatey(-50%);
}
}
.correction [data-selected="true"]:not(.is-true) label {
background-color: #ffd5dc;
border: 2px solid #ff6161;
&:before {
opacity: 1;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='24' height='24' stroke='%23ff6161' stroke-width='3' fill='none' stroke-linecap='round' stroke-linejoin='round' class='css-i6dzq1'%3E%3Cline x1='18' y1='6' x2='6' y2='18'%3E%3C/line%3E%3Cline x1='6' y1='6' x2='18' y2='18'%3E%3C/line%3E%3C/svg%3E");
}
}
.correction input {
pointer-events: none;
}
.result {
transition: 0.3s;
display: flex;
flex-direction: column;
width: 100%;
max-width: 80%;
margin: auto;
height: 0;
overflow: hidden;
&-item {
display: inline-flex;
flex-direction: row-reverse;
justify-content: space-between;
width: 100%;
padding: 5px 10px;
& + & {
border-top: 1px solid rgba(255, 255, 255, 0.05);
}
}
&-count {
font-size: 32px;
font-weight: 600;
}
&-text {
font-size: 12px;
letter-spacing: 2px;
opacity: 0.8;
display: flex;
font-weight: 500;
align-items: center;
line-height: 1;
svg {
margin-right: 8px;
position: relative;
top: -1px;
}
}
}
.result-item.is-correct {
color: #42ff73;
}
.result-item.is-wrong {
color: #ffa2a2;
}
[data-game-finished] {
.result {
opacity: 1;
margin-top: 40px;
padding-top: 20px;
display: flex;
animation: result 0.6s 0.5s both;
&-item {
animation: result-item 0.6s 1.3s both;
&.is-correct {
animation-delay: 1s;
}
&.is-wrong {
animation-delay: 1.15s;
}
}
}
.restart-button {
display: block;
}
}
.correction .question + .question {
margin-top: 30px;
}
@keyframes result {
0% {
height: 0;
}
100% {
height: 150px;
}
}
@keyframes result-item {
0% {
opacity: 0;
transform: translateY(10ox);
}
}
.restart-button {
height: 40px;
border: 1px solid rgba(#fff, 0.4);
margin: 30px auto 0;
background-color: transparent;
color: #fff;
border-radius: 4px;
padding: 0 16px;
font-weight: 500;
transition: 0.3s;
cursor: pointer;
display: none;
animation: result-item 0.4s 1.45s both;
&:hover {
background-color: rgba(#fff, 0.1);
}
}
import React, {
useRef,
useState,
useEffect
} from "https://cdn.skypack.dev/react";
import { render } from "https://cdn.skypack.dev/react-dom";
import gsap from "https://cdn.skypack.dev/gsap";
const questions = [
{
id: 0,
text: "What does CSS stand for?",
answers: [
"Computer Style Sheets",
"Cascading Style Sheets",
"Creative Style Sheets",
"Colorful Style Sheets"
],
correct: 1,
selection: null
},
{
id: 1,
text:
"The property in CSS used to change the background color of an element is?",
answers: ["bgcolor", "color", "background-color", "All of the above"],
correct: 2,
selection: null
},
{
id: 2,
text: "The property in CSS used to change the text color of an element is?",
answers: ["bgcolor", "color", "background-color", "All of the above"],
correct: 1,
selection: null
},
{
id: 3,
text: "The CSS property used to control the element's font size is?",
answers: ["text-style", "font-style", "text-size", "font-size"],
correct: 3,
selection: null
},
{
id: 4,
text: "The HTML attribute used to define the inline styles is?",
answers: ["style", "inline", "class", "None of the above"],
correct: 0,
selection: null
},
{
id: 5,
text: "Are the negative values allowed in padding property?",
answers: ["Yes", "No", "It depends on HTML", "None of the above"],
correct: 1,
selection: null
},
{
id: 6,
text:
"The CSS property used to specify whether the text is written in the horizontal or vertical direction?",
answers: ["word-break", "text-transform", "writing-mode", "text-direction"],
correct: 2,
selection: null
},
{
id: 7,
text: "Which selector is used to specify a style for one unique element?",
answers: ["class", "attribute", "text", "id"],
correct: 3,
selection: null
},
{
id: 8,
text:
"Which of the following is the correct syntax to select the p siblings of a div element?",
answers: ["p", "div + p", "div p", "div ~ p"],
correct: 3,
selection: null
},
{
id: 9,
text: "How can you add a comment in a CSS file?",
answers: [
"/* this is a comment */",
"// this is a comment",
"<!-- this is a comment -->",
"** this is a comment **"
],
correct: 0,
selection: null
}
];
function useCounter(initialState) {
const [value, setValue] = useState(initialState);
const reset = () => setValue(0);
const add = () => setValue((value) => (value += 1));
return { value, add, reset };
}
function Question({
data,
buttonText,
hasButton = true,
onQuestionButtonClick,
showAnswer = false,
markSelection = null
}) {
const [answer, setAnswer] = useState(null);
const parseValue = (value) => (value ? parseInt(value.split("-")[1]) : null);
const questionRef = useRef(null);
useEffect(() => {
gsap.fromTo(
questionRef.current.querySelector(".question-text"),
{
x: 40,
opacity: 0
},
{
x: 0,
opacity: 1,
duration: 0.4
}
);
gsap.fromTo(
questionRef.current.querySelectorAll("li"),
{
opacity: 0,
x: 40
},
{
x: 0,
opacity: 1,
duration: 0.4,
stagger: 0.1
}
);
}, [data]);
return (
<div className="question" ref={questionRef}>
<div className="question-inner">
<h2 className="question-text">{data.text}</h2>
<ul className="question-answers">
{data.answers.map((text, index) => {
const value = `q${data.id}-${index}`;
return (
<li
className={
index === data.correct && showAnswer ? "is-true" : ""
}
data-selected={markSelection === index ? true : null}
>
<input
type="radio"
name={`q_${data.id}`}
value={value}
id={value}
onChange={(e) => setAnswer(e.target.value)}
checked={
!showAnswer ? answer === value : markSelection === index
}
/>
<label className="question-answer" htmlFor={value}>
{text}
</label>
</li>
);
})}
</ul>
</div>
{hasButton && (
<button
className="question-button"
onClick={() => onQuestionButtonClick(parseValue(answer), data)}
>
{buttonText}
</button>
)}
</div>
);
}
function Results({ wrong, correct, empty }) {
return (
<div className="result">
<div className="result-item is-correct">
<span className="result-count">{correct}</span>
<span className="result-text">
<svg
width="16"
height="16"
fill="none"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
className="css-i6dzq1"
viewBox="0 0 24 24"
>
<path d="M22 11.08V12a10 10 0 11-5.93-9.14"></path>
<path d="M22 4L12 14.01 9 11.01"></path>
</svg>
CORRECT
</span>
</div>
<div className="result-item is-wrong">
<span className="result-count">{wrong}</span>
<span className="result-text">
<svg
width="16"
height="16"
fill="none"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
className="css-i6dzq1"
viewBox="0 0 24 24"
>
<circle cx="12" cy="12" r="10"></circle>
<path d="M15 9L9 15"></path>
<path d="M9 9L15 15"></path>
</svg>
WRONG
</span>
</div>
<div className="result-item is-empty">
<span className="result-count">{empty}</span>
<span className="result-text">
<svg
width="16"
height="16"
fill="none"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
className="css-i6dzq1"
viewBox="0 0 24 24"
>
<circle cx="12" cy="12" r="10"></circle>
<path d="M8 12L16 12"></path>
</svg>
EMPTY
</span>
</div>
</div>
);
}
function QuestionCorrection({ wrong, correct, empty }) {
return (
<div className="correction">
{questions.map((question) => {
return (
<Question
hasButton={false}
markSelection={question.selection}
showAnswer={true}
data={question}
/>
);
})}
</div>
);
}
function App() {
const [gameStarted, setGameStarted] = useState(false);
const [gameFinished, setGameFinished] = useState(false);
const [gameSize, setGameSize] = useState({});
const totalQuestion = questions.length - 1;
const gameRef = useRef(null);
const gameOverlayRef = useRef(null);
const question = useCounter(0);
const correct = useCounter(0);
const wrong = useCounter(0);
const empty = useCounter(0);
const handleNewQuestionClick = (selectedValue, currQuestion) => {
if (totalQuestion >= question.value) {
if (selectedValue === currQuestion.correct) {
correct.add();
} else if (
selectedValue !== null &&
selectedValue !== currQuestion.correct
) {
wrong.add();
} else {
empty.add();
}
questions[currQuestion.id].selection = selectedValue;
question.add();
}
};
const resetSelection = () => {
questions.forEach((q) => (q.selection = null));
};
const handleRestartClick = () => {
setGameFinished(false);
setGameStarted(false);
resetSelection();
question.reset();
correct.reset();
wrong.reset();
empty.reset();
};
const indicatorBg = (index) => {
if (question.value > index) {
return "#fff";
} else if (question.value === index) {
return "#29b5d5";
} else {
return "rgba(255,255,255,.2)";
}
};
useEffect(() => {
if (gameStarted) {
document.body.classList.add("game-started");
} else {
document.body.classList.remove("game-started");
}
}, [gameStarted]);
useEffect(() => {
if (question.value > totalQuestion) {
gameRef.current.scrollTop = 0;
}
}, [question.value]);
return (
<div
className="game"
ref={gameRef}
data-game-started={gameStarted ? true : null}
data-game-finished={question.value > totalQuestion ? true : null}
>
<div className="intro">
<div className="intro-inner">
<h1 className="intro-title">CSS Quiz</h1>
{!gameStarted && (
<>
<p className="intro-desc">
{`The test contains ${questions.length} questions and there is no time limit.`}
</p>
<button
className="intro-button"
onClick={() => setGameStarted(true)}
>
Start Quiz
</button>
</>
)}
{gameStarted && (
<div className="indicator">
{questions.map((q, index) => {
return (
<span
className="indicator-item"
style={{
backgroundColor: indicatorBg(index)
}}
/>
);
})}
</div>
)}
<Results
wrong={wrong.value}
correct={correct.value}
empty={empty.value}
/>
<button
className="restart-button"
onClick={() => handleRestartClick()}
>
Restart Quiz
</button>
</div>
</div>
<div className="game-area">
{questions[question.value] && (
<Question
data={questions[question.value]}
buttonText={
question.value !== totalQuestion ? "Next Question" : "Finish Quiz"
}
onQuestionButtonClick={handleNewQuestionClick}
/>
)}
{!questions[question.value] && (
<>
<QuestionCorrection data={questions} />
</>
)}
</div>
</div>
);
}
render(<App />, document.querySelector("#app"));
Also see: Tab Triggers