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="contents">
<h1 class="ttl">スクロール位置によって要素を固定する<span class="code">" position:sticky; "</span></h1>
<div class="inner flexbox">
<main>
<section>
<h2 id="ttl1" class="sticky">見出し1</h2>
<p>見出し固定のサンプルコード。ここではJavaScriptを使用せずにCSSのみでスクロールに応じた見出し固定を実現しています。<code>position: sticky;</code>を指定した要素の位置までスクロールすると固定されます。</p>
<p><img src="https://placehold.jp/640x480.png" alt="" /></a></p>
<p>あのイーハトヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモーリオ市、郊外のぎらぎら光る草の波。あのイーハトヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモーリオ市、郊外のぎらぎら光る草の波。あのイーハトヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモーリオ市、郊外のぎらぎら光る草の波。</p>
<p>見出し1はここを過ぎると固定が解除されます。</p>
</section>
<section>
<h2 id="ttl2" class="sticky">見出し2</h2>
<p>見出し2はここから固定されます。</p>
<p>親要素に<code>overflow:hidden;</code>があるとうまく動かないみたい。Internet ExplorerやEdgeでは使えないので使いどころが悩む所。</p>
<p><img src="https://placehold.jp/640x480.png" alt="" /></a></p>
<p>あのイーハトヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモーリオ市、郊外のぎらぎら光る草の波。あのイーハトヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモーリオ市、郊外のぎらぎら光る草の波。あのイーハトヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモーリオ市、郊外のぎらぎら光る草の波。</p>
<p>見出し2はここを過ぎると固定が解除されます。</p>
</section>
<section>
<h2 id="ttl3" class="sticky">見出し3</h2>
<p>見出し3はここから固定されます。</p>
<p>テーブルなんかも<code>posisition:sticky;</code>を使えばExcelのように見出し部分を固定することだって可能。ただし、FirefoxとOperaだとバグでうまく動作しないようです。<code>border-collapse:collapse;</code>との相性が悪いらしいので、テーブルで使用する際は<code>border-collapse:separate;</code>にしておきましょう。</p>
<table>
<thead class="thead-sticky">
<tr>
<th></th>
<th>音声通話プラン</th>
<th>データSMSプラン</th>
<th>データ専用プラン</th>
<th>音声通話プラン</th>
<th>データSMSプラン</th>
<th>データ専用プラン</th>
<th>音声通話プラン</th>
<th>データSMSプラン</th>
<th>データ専用プラン</th>
</tr>
</thead>
<tbody>
<tr>
<th class="th-sticky">3GB</th>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
</tr>
<tr>
<th class="th-sticky">7GB</th>
<td>1,600円</td>
<td>1,750円</td>
<td>1,600円</td>
<td>1,600円</td>
<td>1,750円</td>
<td>1,600円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
</tr>
<tr>
<th class="th-sticky">13GB</th>
<td>3,500円</td>
<td>2,950円</td>
<td>2,800円</td>
<td>3,500円</td>
<td>2,950円</td>
<td>2,800円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
</tr>
<tr>
<th class="th-sticky">3GB</th>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
</tr>
<tr>
<th class="th-sticky">7GB</th>
<td>1,600円</td>
<td>1,750円</td>
<td>1,600円</td>
<td>1,600円</td>
<td>1,750円</td>
<td>1,600円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
</tr>
<tr>
<th class="th-sticky">13GB</th>
<td>3,500円</td>
<td>2,950円</td>
<td>2,800円</td>
<td>3,500円</td>
<td>2,950円</td>
<td>2,800円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
</tr>
<tr>
<th class="th-sticky">3GB</th>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
</tr>
<tr>
<th class="th-sticky">7GB</th>
<td>1,600円</td>
<td>1,750円</td>
<td>1,600円</td>
<td>1,600円</td>
<td>1,750円</td>
<td>1,600円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
</tr>
<tr>
<th class="th-sticky">13GB</th>
<td>3,500円</td>
<td>2,950円</td>
<td>2,800円</td>
<td>3,500円</td>
<td>2,950円</td>
<td>2,800円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
</tr>
<tr>
<th class="th-sticky">3GB</th>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
</tr>
<tr>
<th class="th-sticky">7GB</th>
<td>2,300円</td>
<td>1,750円</td>
<td>1,600円</td>
<td>2,300円</td>
<td>1,750円</td>
<td>1,600円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
</tr>
<tr>
<th class="th-sticky">13GB</th>
<td>3,500円</td>
<td>2,950円</td>
<td>2,800円</td>
<td>3,500円</td>
<td>2,950円</td>
<td>2,800円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
</tr>
<tr>
<th class="th-sticky">3GB</th>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
</tr>
<tr>
<th class="th-sticky">7GB</th>
<td>2,300円</td>
<td>1,750円</td>
<td>1,600円</td>
<td>2,300円</td>
<td>1,750円</td>
<td>1,600円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
</tr>
<tr>
<th class="th-sticky">13GB</th>
<td>3,500円</td>
<td>2,950円</td>
<td>2,800円</td>
<td>3,500円</td>
<td>2,950円</td>
<td>2,800円</td>
<td>1,600円</td>
<td>1,050円</td>
<td>900円</td>
</tr>
</tbody>
</table>
<p>あのイーハトヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモーリオ市、郊外のぎらぎら光る草の波。あのイーハトヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモーリオ市、郊外のぎらぎら光る草の波。あのイーハトヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモーリオ市、郊外のぎらぎら光る草の波。</p>
<p class="yohaku">そんなにスクロールしても何もないよ。フッターまで見ちゃうタイプなんですね。</p>
</section>
<!-- /#contents -->
</main>
<aside>
<div class="bnr">
<div class="sticky"><a href="#bnr"><img src="https://placehold.jp/300x250.png" alt="" /></a></div>
</div>
<div class="toc sticky">
<h2>目次</h2>
<ul class="tocList">
<li class="list"><a href="#ttl1">見出し1</a></li>
<li class="list"><a href="#ttl2">見出し2</a></li>
<li class="list"><a href="#ttl3">見出し3</a></li>
</ul>
<!-- /.toc --></div>
</aside>
<!-- /.inner.flexbox --></div>
</div>
// Google Font
@import url('https://fonts.googleapis.com/css?family=Poppins');
// mixin
$red: #f22;
$webfont: 'Poppins', sans-serif;
body {
background-color:#f2f2f2;
}
#contents {
max-width: 980px;
margin:0 auto;
background-color: #fff;
h1 {
padding: 40px 10px;
font-size: 1.5rem;
line-height: 1.5;
text-align: center;
}
.code {
display: inline-block;
margin-left: 5px;
color: $red;
font-weight: bold;
font-family: $webfont;
vertical-align: middle;
}
.flexbox {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
list-style: none;
-webkit-box-orient: horizontal;
-webkit-box-direction: normal;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
-webkit-box-pack: justify;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
main, aside {
box-pack: justify;
-webkit-box-pack: justify;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
}
}
img {
max-width:100%;
height: auto;
}
/* ↓スクロールに応じて要素を固定↓ */
.sticky {
position: -webkit-sticky; // Safari用
position:sticky; // 要素をスクロールに応じて固定
top:0; // 縦スクロールに追従
z-index: 100; // z-indexで最前面に持ってくる
}
.thead-sticky {
position: -webkit-sticky; // Safari用
position: sticky; // 要素をスクロールに応じて固定
top: 0; // 縦スクロールに追従
z-index: 3; // z-indexで前面に持ってくる
}
.th-sticky {
position: -webkit-sticky; // Safari用
position: sticky; // 要素をスクロールに応じて固定
left: 0; // 横スクロールに追従
z-index: 2; // z-indexで前面に持ってくる
}
/* ↑スクロールに応じて要素を固定↑ */
h2 {
padding: 1em 1.25em;
line-height: 1.25;
border-top:2px solid $red;
box-shadow:0 1px 4px rgba(0,0,0,0.3);
background-color: #fff;
}
main {
flex:1;
width:48%;
section {
padding-bottom: 30px;
p.yohaku { margin-top: 2000px; }
}
p {
margin:1.25em;
line-height:1.7;
}
code {
display: inline-block;
margin: 5px;
padding: 5px 12px 3px;
background-color: #f2f2f2;
font-family: $webfont;
font-size: 13px;
color:#888;
line-height: 1.25;
vertical-align: middle;
border-radius:3px;
}
table {
width: 100%;
max-width: 680px;
height:300px;
overflow: auto;
white-space: nowrap;
display: inline-block;
overflow-x: auto;
table-layout:fixed;
box-sizing:border-box;
tr {
th, td {
position:relative;
padding:1em;
text-align:center;
vertical-align:middle;
border:1px solid #dedede;
box-sizing:border-box;
}
th {
background-color: #faebeb;
}
}
tbody {
tr:hover {
th { background-color: #fdd8d8; }
td {
background-color: #f2f2f2;
&:hover { background-color: #dedede; }
}
}
}
}
}
aside {
width: 300px;
.bnr {
height: 768px;
}
.toc {
.tocList {
margin-top: 1em;
}
.list {
a {
display: block;
padding: 0.5em 1em;
color: $red;
text-decoration: none;
}
}
}
}
// タブレット以下
@media screen and (max-width: 768px) {
.code {
display: block;
}
.flexbox {
display: block;
}
.sticky:not(h2) {
position:static;
}
main, aside {
width: auto;
}
main {
table {
max-width: 768px;
font-size: 14px;
}
}
aside {
.bnr {
position:static;
height:auto;
text-align: center;
}
.toc {
margin-top: 1.25em;
}
}
}
@media screen and (max-width: 480px) {
main table {
font-size: 12px;
}
}
}
Also see: Tab Triggers