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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<script src="https://unpkg.com/[email protected]"></script>
</head>
<body>
<div id="root"></div>
</body>
</html>
const en = 'He wants to be independent of his parents.'
const RootComponent = {
data () { return {
origin: en,
text: setData(), //初期設定
p: [], //何番目まで答えたかの位置
showBackward: true, //「戻る」ボタンの表示・非表示
message: '' //メッセージの本文
}},
template: `
<p>{{origin}}</p>
<p>
<span v-for="word in text.words">
{{word+" "}}
</span>
</p>
<div>
<button
v-for="(option, index) in text.opt"
v-show="text.optShow[index]"
@click="input(index)">
{{option}}
</button>
<button v-show="showBackward" @click="backward">戻る</button>
</div>
<div>{{message}}</div>`,
methods: {
input(index) {
this.text.optShow[index] = false; //押されたボタンを非表示に
//マスクされた部分を選択肢から選んだ単語に置き換える
this.text.words[this.text.mask[this.p.length]] = this.text.opt[index];
this.text.record.push(index); //押されたボタンの番号を記録
//選択肢の文字列と本文の文字列が一致すれば正解
if(this.text.optMask[index] == this.text.mask[this.p.length]) {
this.p.push('correct'); //正解なら配列にcorrectを加える
} else {
this.p.push('incorrect'); //不正解なら配列にincorrectを加える
}
//終了時
if(this.p.length == this.text.mask.length) {
this.showBackward = false; //「戻る」ボタンを非表示にする
//配列のすべての要素がcorrectであるかどうかを調べる
if(this.p.every(value => value == 'correct')) {
this.message = 'すべて正解です。';
} else {
this.message = '誤りがあります。';
}
}
},
backward() {
if(this.p.length > 0) {
this.p.pop(); //配列の最後の要素を削除して一つ戻る
//入力された単語を下線に戻す
this.text.words[this.text.mask[this.p.length]] = ' ____ ';
//ボタンを再度表示する
this.text.optShow[this.text.record.pop()] = true;
}
}
}
}
function setData() {
let obj = {
words: en.split(' '), //本文を単語ごとに分割
record: [], //ボタンを押した順番の記録
mask: [], //マスクする単語の位置
opt: [], //選択肢の文字列
optMask: [], //選択肢が表す単語の位置
optShow: [] //ボタンの表示・非表示
}
let n = Array.from(Array(obj.words.length).keys()); //連番の配列
//シャッフル
for(let i = 0; i < 100; i++) {
let a = Math.floor(Math.random() * n.length);
let b = Math.floor(Math.random() * n.length);
[n[a], n[b]] = [n[b], n[a]];
}
//マスクする単語数:1語+全体の25%
let amount = 1 + Math.round(obj.words.length * 0.25);
//マスクする単語数にしたがって設定を行う
for(let i = 0; i < amount; i++) {
obj.mask.push(n[i]); //マスクする単語の位置を追加
obj.opt.push(obj.words[n[i]]); //選択肢の文字列を追加
obj.optMask.push(n[i]); //選択肢が表す単語の位置を追加
obj.optShow.push('true'); //ボタンの表示を追加
}
obj.mask.sort((a, b) => a - b); //昇順に並べ替え
//マスクされた単語を下線に置き換える
for(let i = 0; i < obj.opt.length; i++) {
obj.words[obj.mask[i]] = ' ____ ';
}
return obj;
}
const app = Vue.createApp(RootComponent);
app.mount('#root');
Also see: Tab Triggers