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.
Private Pens are hidden everywhere on CodePen, except to you. You can still share them and other people can see them, they just can't find them through searching or browsing.
Upgrade to PROIf 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.
Templates are Pens that can be used to start other Pens quickly from the create menu. The new Pen will copy all the code and settings from the template and make a new Pen (that is not a fork). You can view all of your templates, or learn more in the documentation.
Screenshots of Pens are shown in mobile browsers, RSS feeds, to users who chose images instead of iframes, and in social media sharing.
PRO members can see and edit the Pen thumbnail here after the Pen has been saved.
<div id='root'></div>
@import url('https://fonts.googleapis.com/css2?family=Anek+Malayalam:wght@100;200;300;400;500;600;700;800&display=swap');
body {
font-family: 'Anek Malayalam', sans-serif;
background: linear-gradient(90.04deg, #800080 0.03%, #ffc0cb 99.96%);
color: #11052c;
display: flex;
justify-content: center;
margin: 0;
padding: 0 30px;
}
.quiz-container {
max-width: 500px;
min-width: 250px;
background: #ffffff;
border-radius: 4px;
margin-top: 100px;
padding: 30px 60px;
}
.quiz-container .active-question-no {
font-size: 32px;
font-weight: 500;
color: #800080;
}
.quiz-container .total-question {
font-size: 16px;
font-weight: 500;
color: #e0dee3;
}
.timer {
display: flex;
align-items: center;
}
.timer span {
font-size: 18px;
font-weight: 500;
color: #800080;
margin-left: 6px;
}
.quiz-container h2 {
font-size: 20px;
font-weight: 500;
margin: 0;
}
.quiz-container ul {
margin-top: 20px;
margin-left: -40px;
}
.quiz-container ul li {
text-decoration: none;
list-style: none;
color: #2d264b;
font-size: 16px;
background: #ffffff;
border: 1px solid #eaeaea;
border-radius: 16px;
padding: 11px;
margin-top: 15px;
cursor: pointer;
}
.quiz-container ul .selected-answer {
background: #ffd6ff;
border: 1px solid #800080;
}
.quiz-container button {
background: linear-gradient(90.04deg, #800080 0.03%, #ffc0cb 99.96%);
border-radius: 9px;
font-size: 18px;
color: #ffffff;
padding: 10px 42px;
outline: none;
border: none;
cursor: pointer;
margin-top: 15px;
}
.quiz-container button:disabled {
background: #e7e8e9;
color: #9fa3a9;
cursor: not-allowed;
}
.flex-right {
display: flex;
justify-content: flex-end;
}
.result h3 {
font-size: 24px;
letter-spacing: 1.4px;
text-align: center;
}
.result p {
font-size: 16px;
font-weight: 500;
}
.result p span {
color: #800080;
font-size: 22px;
}
.flx-end {
display: flex;
justify-content: space-between;
align-items: center;
}
// Question Types
// 1. MCQs | Multiple Choice | single
const quiz = {
topic: 'Javascript',
level: 'Beginner',
totalQuestions: 10,
perQuestionScore: 5,
totalTime: 60, // in seconds
questions: [
{
question:
'Which function is used to serialize an object into a JSON string in Javascript?',
choices: ['stringify()', 'parse()', 'convert()', 'None of the above'],
type: 'MCQs',
correctAnswer: 'stringify()',
},
{
question:
'Which of the following keywords is used to define a variable in Javascript?',
choices: ['var', 'let', 'var and let', 'None of the above'],
type: 'MCQs',
correctAnswer: 'var and let',
},
{
question:
'Which of the following methods can be used to display data in some form using Javascript?',
choices: [
'document.write()',
'console.log()',
'window.alert',
'All of the above',
],
type: 'MCQs',
correctAnswer: 'All of the above',
},
{
question: 'How can a datatype be declared to be a constant type?',
choices: ['const', 'var', 'let', 'constant'],
type: 'MCQs',
correctAnswer: 'const',
},
],
}
const Timer = () => (
<svg
width="34"
height="34"
viewBox="0 0 44 44"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M21.9999 41.7083C12.3933 41.7083 4.58325 33.8983 4.58325 24.2917C4.58325 14.685 12.3933 6.875 21.9999 6.875C31.6066 6.875 39.4166 14.685 39.4166 24.2917C39.4166 33.8983 31.6066 41.7083 21.9999 41.7083ZM21.9999 9.625C13.9149 9.625 7.33325 16.2067 7.33325 24.2917C7.33325 32.3767 13.9149 38.9583 21.9999 38.9583C30.0849 38.9583 36.6666 32.3767 36.6666 24.2917C36.6666 16.2067 30.0849 9.625 21.9999 9.625Z"
fill="#800080"
/>
<path
d="M22 25.2084C21.2483 25.2084 20.625 24.5851 20.625 23.8334V14.6667C20.625 13.9151 21.2483 13.2917 22 13.2917C22.7517 13.2917 23.375 13.9151 23.375 14.6667V23.8334C23.375 24.5851 22.7517 25.2084 22 25.2084Z"
fill="#800080"
/>
<path
d="M27.5 5.04175H16.5C15.7483 5.04175 15.125 4.41841 15.125 3.66675C15.125 2.91508 15.7483 2.29175 16.5 2.29175H27.5C28.2517 2.29175 28.875 2.91508 28.875 3.66675C28.875 4.41841 28.2517 5.04175 27.5 5.04175Z"
fill="#800080"
/>
</svg>
)
const SevenSecondQuiz = () => {
const [activeQuestion, setActiveQuestion] = React.useState(0)
const [selectedAnswer, setSelectedAnswer] = React.useState('')
const [showResult, setShowResult] = React.useState(false)
const [selectedAnswerIndex, setSelectedAnswerIndex] = React.useState(null)
const [result, setResult] = React.useState({
score: 0,
correctAnswers: 0,
wrongAnswers: 0,
})
const { questions } = quiz
const { question, choices, correctAnswer } = questions[activeQuestion]
const onClickNext = () => {
setSelectedAnswerIndex(null)
setResult((prev) =>
selectedAnswer
? {
...prev,
score: prev.score + 5,
correctAnswers: prev.correctAnswers + 1,
}
: { ...prev, wrongAnswers: prev.wrongAnswers + 1 }
)
if (activeQuestion !== questions.length - 1) {
setActiveQuestion((prev) => prev + 1)
} else {
setActiveQuestion(0)
setShowResult(true)
}
}
const onAnswerSelected = (answer, index) => {
setSelectedAnswerIndex(index)
if (answer === correctAnswer) {
setSelectedAnswer(true)
} else {
setSelectedAnswer(false)
}
}
const addLeadingZero = (number) => (number > 9 ? number : `0${number}`)
return (
<div className="quiz-container">
{!showResult ? (
<div>
<div className="flx-end">
<div>
<span className="active-question-no">
{addLeadingZero(activeQuestion + 1)}
</span>
<span className="total-question">
/{addLeadingZero(questions.length)}
</span>
</div>
<div className="timer">
<Timer />
<span>00:07</span>
</div>
</div>
<h2>{question}</h2>
<ul>
{choices.map((answer, index) => (
<li
onClick={() => onAnswerSelected(answer, index)}
key={answer}
className={
selectedAnswerIndex === index ? 'selected-answer' : null
}
>
{answer}
</li>
))}
</ul>
<div className="flex-right">
<button
onClick={onClickNext}
disabled={selectedAnswerIndex === null}
>
{activeQuestion === questions.length - 1 ? 'Finish' : 'Next'}
</button>
</div>
</div>
) : (
<div className="result">
<h3>Result</h3>
<p>
Total Question: <span>{questions.length}</span>
</p>
<p>
Total Score:<span> {result.score}</span>
</p>
<p>
Correct Answers:<span> {result.correctAnswers}</span>
</p>
<p>
Wrong Answers:<span> {result.wrongAnswers}</span>
</p>
</div>
)}
</div>
)
}
ReactDOM.render(<SevenSecondQuiz />, document.getElementById('root'));
<main>
thing the <main>
thing.
Also see: Tab Triggers