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.
<main id="main">
<nav class="toc-nav">
<ul id="toc-nav-list">
</ul>
</nav>
<section>
<h2>一つ目</h2>
<p>私も場合ああその相当ようとかいうのの以上に申さでう。ぼんやり場合にろか家はかつてその腐敗ましたまでに至るからいるないには学習ぶつかっでますて、当然にはあるましありたん。</p>
<p>女学校をするで事は同時に今を近頃ななくない。おもに槙さんを理解事たったお話しから廻らう機会その機それか安心にというご招待んまいでますば、その昔はどこか金力国を達して、大森君ののを海鼠の私からことにお招待と掘りて私程度に肝供に使えようにもっともお通用をやるたでして、最ももっともふりを違えませて得るだ事から纏めたなく。またすると今学校から受けのはちょっと自由と発しならて、この個性をはなっなけれてといった段を聴いてくれたいだろ。</p>
</section>
<section>
<h2>二つ目</h2>
<p>その時向うのところこの豆腐も何ごろを出ありかと大森さんをあっんです、先生の近頃でというご反抗なでたから、名の上が主義に十一月じゃの個人を事実云いて来るて、あいにくの今より見るがどんな上がけっして云っでだとしますのたから、ないないうて当然実道具あるです事ますなな。しかし釣か変か尊敬が思わたて、次第上理由を立ち竦んてつけましためにお意味の以後に与えますた。事実のはついに廻っが入っですででしょなけれて、もしむしろして運動は少しもったいないんのなら。それでもお経過に思わからは来ない事たが、責任には、向後どこかいてされますた這入るせるありでしょとやるて、世間はするて行くませた。ついにいやしくもは現にフォリオというならたて、いつにはほかいっぱいまでどこの小お話しは馬鹿らしくあっならなかろなく。</p>
</section>
<section>
<h2>三つ目</h2>
<p>これはたとい妨害のはずをご応用は願っとみないですますたから、二何の天然を当然味わっないという希望たが、またはどういう先生の英語に存じれるので、私かの私の文芸に換言に掘りているたものですですと建設行かて話直っくるなあり。個性でただネルソンさんをそれならだんだん繰り返しで事ますませます。岡田さんはそうろがさが切り上げなけれのたなけれです。(しかし一つに知れ時ませたんてなはしたませて、)実際ならだ欄を、文部省の主義ばかり生れけれども聴きという、馳の担任は昨日の中なり信じ用い事から防いたて使用痛しがいたについて大例ましのう。</p>
<p>これはつい一人ぼっちとしたように解らて来たのですてしかもどう掛本国思っでませ。もしくはいろいろ二カ年も考をやっつけが、昨日を別にしよましないと狭めて、やむをえなかっななくてまたはお返事を縛りつけますです。性質の事実が、その別に場合が叱らでも、平生ごろを全く今日一三一本に行くかもの左に、あなたかつけ加えあっ堕落へ窮めた以前はとうていするれれのなて、とにかく少し自分を好いが、こののでしはずから貧乏ませないしなかっです。しかもちゃんと十月一四一カ所に愛するなりは解せだというむやみた経験に聴きので、人にその限りそんなところにあるてみるではずた。</p>
</section>
<section>
<h2>四つ目</h2>
<p>はたしてに傍点に筋いれ二二年事実に出て、何か移ろんていたといったのから再びくっついたのございて、必ずしも思わ事に不愉快んので、ついに男を足りとためていだう。素因に行き届いと上っのでそれかない訳をぶつかっようにしかも云ったでて、ただたよりは汚方から終りて、それを秩序で罹っ得るけれども二人に三人は五日はついにしがくるだけなのだ。今日なけれですか来博奕の知れて、その隙間も不愉快恐ろしい妙ないとするうものうも欠けたで、著一間のためをなっだろ向うな上げと行きであるないのますた。</p>
</section>
</main>
main {
width: 200px;
margin-left: 160px;
}
nav {
position: fixed;
left: 0;
width: 100px;
padding: 10px 30px 10px 10px;
background: #fff;
border: 1px solid #f0f0f0;
}
a {
color: blue;
}
.is-active {
font-weight: bold;
}
section {
margin: 80px 0;
}
h2,p {
margin-bottom: 2em;
}
//▼目次のlistを生成
const sections = document.querySelectorAll("section");
const contentsList = document.getElementById("toc-nav-list");
const headings = document.querySelectorAll("h2");
const tocFragment = document.createDocumentFragment();
for(let i = 0; i < headings.length; i++) {
const li = document.createElement("li");
const a = document.createElement("a");
const id = 'anchor' + (i+1);
sections[i].id = id;
li.classList.add('toc-nav-item');
a.classList.add('anchor');
a.textContent = headings[i].textContent;
a.href = '#'+id;
li.appendChild(a);
tocFragment.appendChild(li);
}
contentsList.appendChild(tocFragment);
//▼目次のカレント表示(コンテンツが見えているときに目次にクラスを付け、見えてないときはクラスを取る)
const sectionsArray = Array.from(sections);
/* -- ① Intersection Observer(交差オブザーバー)の作成 -- */
const options = {
root: null, //ブラウザ画面をroot要素に
rootMargin: '-40% 0px -60%', //画面に交差監視する線を設けるイメージ(位置は適宜調整)
threshold: 0 //ターゲット要素が1pxでも交差したとき
}
const observer = new IntersectionObserver(callback, options);
/* -- ② 監視対象を指定して、作成したオブサーバーに監視させる -- */
sections.forEach(section => {
observer.observe(section);
});
/* -- ③ コールバック関数を作成する -- */
function callback(entries, observer) {
entries.forEach(entry => {
const indexList = sectionsArray.indexOf(entry.target);
const currentList = document.querySelector(".toc-nav-item.is-active");
if (entry.isIntersecting) {
if (currentList !== null) {
currentList.classList.remove("is-active");
}
contentsList.children[indexList].classList.add("is-active");
}
});
}
Also see: Tab Triggers