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.
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ドラえもんクイズ</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container" id="container">
<!-- 全問題数表示 -->
<div class="quiz-info">
全 <span id="total-questions"></span> 問
</div>
<h1>ドラえもんクイズ</h1>
<div class="quiz-container" id="quiz-container">
<div class="question-number" id="question-number"></div>
<div class="question" id="question"></div>
<div class="grid-container" id="choices-container"></div>
<!-- アコーディオン式ヒント -->
<div id="hint-button" onclick="toggleHint()">ヒントを見る</div>
<div id="hint-content"></div>
</div>
<div class="result-section" id="answer-section">
<h2>回答結果</h2>
<p id="answer-result"></p>
<div id="next-question" onclick="nextQuestion()">次の問題へ</div>
</div>
<div id="final-result" class="final-section">
<h2>最終結果</h2>
<p id="score"></p>
<div id="restart-quiz" onclick="restartQuiz()">もう一度遊ぶ</a></div>
</div>
</div>
<!-- 先に問題ファイルを読み込む -->
<script src="quizData.js"></script>
<!-- 次にメインのスクリプトを読み込む -->
<script src="script.js"></script>
</body>
</html>
body {
font-family: Arial, sans-serif;
background-color: #f9f9f9;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
}
.container {
max-width: 800px;
width: 100%;
margin: 0 auto;
text-align: center;
}
/* 全問題数表示エリア */
.quiz-info {
font-size: 1.2rem;
color: white;
margin-bottom: 20px;
max-width: 100px;
margin-left: auto;
margin-right: auto;
padding: 5px;
border: 2px solid #333;
border-radius: 5px;
background-color: dimgrey;
}
.quiz-container,
.result-section,
.final-section {
display: none;
margin: 20px auto;
}
.question-number {
font-weight: bold;
margin-bottom: 10px;
}
.question {
font-weight: bold;
margin-bottom: 10px;
padding: 15px;
border: 2px solid black;
border-radius: 10px;
box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.5);
background-color: #ffffff;
}
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 10px;
}
.choice {
padding: 15px;
background-color: #ffffff;
text-align: center;
cursor: pointer;
border: 2px solid black;
border-radius: 10px;
box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.5);
transition: background-color 0.3s;
}
.choice:hover {
background-color: #d0d0d0;
}
.result-section {
padding: 20px;
border: 2px solid black;
border-radius: 10px;
box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.5);
background-color: #f1f1f1;
}
#answer-result {
font-size: 2rem;
margin-bottom: 10px;
}
.explanation {
margin-top: 10px;
}
.correct {
color: green;
}
.wrong {
color: red;
}
#hint-content {
display: none;
padding: 10px;
background-color: #f9f9f9;
border: 1px solid #ddd;
margin-top: 10px;
}
#hint-button {
background-color: #e0e0e0;
padding: 10px 20px;
border-radius: 10px;
border: 2px solid black;
cursor: pointer;
text-align: center;
margin-top: 20px;
}
#next-question {
margin-top: 20px;
padding: 10px;
background-color: #e0e0e0;
text-align: center;
cursor: pointer;
border: 2px solid black;
border-radius: 10px;
box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.5);
transition: background-color 0.3s;
max-width: 300px;
margin-left: auto;
margin-right: auto;
}
#next-question:hover {
background-color: #d0d0d0;
}
.final-section {
padding: 20px;
border: 2px solid black;
border-radius: 10px;
box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.5);
background-color: #f9f9f9;
margin: 20px auto;
max-width: 800px;
}
#restart-quiz {
margin-top: 20px;
padding: 10px;
background-color: #e0e0e0;
text-align: center;
cursor: pointer;
border: 2px solid black;
border-radius: 10px;
box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.5);
transition: background-color 0.3s;
max-width: 300px;
margin-left: auto;
margin-right: auto;
}
#restart-quiz:hover {
background-color: #d0d0d0;
}
const quizData = [
{
question: "ドラえもんの体の色は何色ですか?",
choices: [{ text: "青" }, { text: "赤" }, { text: "緑" }, { text: "黄色" }],
correct: 0,
hint: "彼の体は海のような色です。",
},
{
question: "ドラえもんの好きな食べ物は何ですか?",
choices: [
{ text: "ピザ" },
{ text: "どら焼き" },
{ text: "カレー" },
{ text: "ケーキ" },
],
correct: 1,
hint: "名前に似た食べ物です。",
},
{
question: "ドラえもんはどこから来ましたか?",
choices: [
{ text: "未来" },
{ text: "過去" },
{ text: "月" },
{ text: "海底" },
],
correct: 0,
hint: "22世紀から来ています。",
},
];
let currentQuiz = quizData; // 全問題をそのまま使用
let currentQuestion = 0;
let score = 0;
const totalQuestions = quizData.length;
document.getElementById("total-questions").textContent = totalQuestions;
// 初期化
function initQuiz() {
currentQuestion = 0; // 問題番号の初期化
score = 0; // スコアの初期化
loadQuestion();
}
// 問題と選択肢を表示
function loadQuestion() {
document.getElementById("quiz-container").style.display = "block";
document.getElementById("answer-section").style.display = "none";
document.getElementById("final-result").style.display = "none";
// 問題番号を表示
document.getElementById("question-number").textContent = `第 ${
currentQuestion + 1
} 問`;
const questionData = currentQuiz[currentQuestion];
document.getElementById("question").textContent = questionData.question;
const choicesContainer = document.getElementById("choices-container");
choicesContainer.innerHTML = "";
// 選択肢に番号を付ける
questionData.choices.forEach((choice, index) => {
const choiceDiv = document.createElement("div");
choiceDiv.classList.add("choice");
choiceDiv.textContent = `${index + 1}. ${choice.text}`; // 番号付きの選択肢
choiceDiv.onclick = () => checkAnswer(index, questionData);
choicesContainer.appendChild(choiceDiv);
});
// ヒントの初期化(閉じる)
document.getElementById("hint-content").style.display = "none";
}
// 答えを確認
function checkAnswer(selected, questionData) {
document.getElementById("quiz-container").style.display = "none";
document.getElementById("answer-section").style.display = "block";
const resultText = document.getElementById("answer-result");
const choicesContainer = document.getElementById("choices-container");
choicesContainer.innerHTML = ""; // クリアして選択肢を再描画
// 正解・不正解のメッセージ表示
if (selected === questionData.correct) {
resultText.innerHTML = "<span class='correct'>正解!</span>";
score++;
} else {
resultText.innerHTML = "<span class='wrong'>不正解です。</span>";
}
// 最終問題かどうかのチェック
if (currentQuestion === currentQuiz.length - 1) {
document.getElementById("next-question").textContent = "結果を見る";
} else {
document.getElementById("next-question").textContent = "次の問題へ";
}
}
// ヒントを開閉できるアコーディオン
function toggleHint() {
const hintContent = document.getElementById("hint-content");
hintContent.textContent = currentQuiz[currentQuestion].hint; // ヒントの内容を表示
if (hintContent.style.display === "none") {
hintContent.style.display = "block";
} else {
hintContent.style.display = "none";
}
}
// 次の問題へ
function nextQuestion() {
currentQuestion++;
if (currentQuestion < currentQuiz.length) {
loadQuestion();
document.getElementById("container").scrollIntoView({ behavior: "smooth" }); // containerにスクロール
} else {
showResult();
}
}
// 結果を表示
function showResult() {
document.getElementById("answer-section").style.display = "none";
document.getElementById("final-result").style.display = "block";
const percentage = (score / currentQuiz.length) * 100;
document.getElementById("score").textContent = `正解数: ${score}/${
currentQuiz.length
} (${percentage.toFixed(2)}%)`;
}
// もう一度遊ぶ
function restartQuiz() {
currentQuestion = 0;
score = 0;
initQuiz();
}
// クイズ開始
initQuiz();
Also see: Tab Triggers