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 class="contents">
<div><img src="https://cdn.pixabay.com/photo/2017/11/12/13/37/forest-2942477__340.jpg"><p>タイトルが入ります。1</p></div>
<div><img src="https://cdn.pixabay.com/photo/2013/11/28/10/03/river-219972__340.jpg"><p>タイトルが入ります。2</p></div>
<div><img src="https://cdn.pixabay.com/photo/2018/05/18/10/25/trees-3410836__340.jpg"><p>タイトルが入ります。3</p></div>
<div><img src="https://cdn.pixabay.com/photo/2013/10/02/23/03/mountains-190055__340.jpg"><p>タイトルが入ります。4</p></div>
<div><img src="https://cdn.pixabay.com/photo/2016/11/08/04/49/jungle-1807476__340.jpg"><p>タイトルが入ります。5</p></div>
<div><img src="https://cdn.pixabay.com/photo/2017/12/15/13/51/polynesia-3021072__340.jpg"><p>タイトルが入ります。6</p></div>
<div><img src="https://cdn.pixabay.com/photo/2016/03/09/09/43/person-1245959__340.jpg"><p>タイトルが入ります。7</p></div>
<div><img src="https://cdn.pixabay.com/photo/2016/11/19/18/57/godafoss-1840758__340.jpg"><p>タイトルが入ります。8</p></div>
<div><img src="https://cdn.pixabay.com/photo/2012/08/06/00/53/bridge-53769__340.jpg"><p>タイトルが入ります。9</p></div>
<div><img src="https://cdn.pixabay.com/photo/2013/07/25/01/31/forest-166733__340.jpg"><p>タイトルが入ります。10</p></div>
<div><img src="https://cdn.pixabay.com/photo/2015/01/28/23/34/mountains-615428__340.jpg"><p>タイトルが入ります。11</p></div>
</div>
<!-- ページネーション -->
<div class="pagination">
<p class="total_counter"></p>
<p class="prev">前へ</p>
<ul class="page_counter"></ul>
<p class="next">次へ</p>
</div>
.contents {
display: flex;
flex-wrap: wrap;
}
.contents > div {
width: 33.3333%;
box-sizing: border-box;
padding: 10px;
}
.contents img {
width: 100%;
height: 300px;
object-fit: cover;
}
.next.disable,
.prev.disable {
pointer-events: none;
background: #ddd;
}
.pagination {
display: flex;
justify-content: center;
margin-top: 20px;
}
.pagination p {
border: 1px solid #ddd;
padding: 5px 10px;
margin: 0 5px;
background: #606060;
color: #fff
}
.next:hover, .prev:hover, .page_counter li:hover {
cursor: pointer;
}
.page_counter {
display: flex;
list-style: none;
padding-left: 0;
margin: 0 10px;
}
.page_counter li {
border: 1px solid #606060;
padding: 5px 10px;
}
.page_counter li.current {
background: #606060;
color: #fff;
}
.page_counter li + li {
margin-left: 5px;
}
// 表示要素 DOM操作用定数
const contents = document.querySelector('.contents');
const redraw_elements = document.querySelectorAll('.contents > div');
// ページネーション DOM操作用の定数
const total_el = document.querySelector('.total_counter');
const page_counter = document.querySelector('.page_counter');
const prev_btn = document.querySelector('.prev');
const next_btn = document.querySelector('.next');
const count = 3;
// グローバル変数
let current_step;
let index_start;
let index_end;
// ページ数を算出
function split_page(current_step_update)
{
total_step = Math.ceil(redraw_elements.length / count);
if( current_step_update === undefined || current_step === 1) {
current_step = 1;
next_btn_disable(); prev_btn_active();
} else if( current_step_update === total_step ) {
next_btn_active(); prev_btn_disable();
} else {
current_step = current_step_update;
next_btn_disable(); prev_btn_disable();
console.log(current_step);
}
total_el.textContent = current_step + '/' + total_step;
redraw(redraw_elements.length, total_step, current_step, count);
}
// DOMの描画
function redraw(total, total_step, current_step, count)
{
// 現在の表示indexを割り出す
index_start = current_step * count - count;
index_end = current_step * count - 1;
let index_array = [];
for (let i = index_start; i < index_end + 1; i++) {
index_array.push(i);
}
// 一時削除
while( contents.lastChild ) {
contents.lastChild.remove();
}
// 再描画
redraw_elements.forEach((element, index) => {
if(index_array.indexOf(index) != -1) {
contents.appendChild(element);
}
});
}
// ページカウンターの作成
function create_page_counter()
{
for (let i = 1; i < Math.ceil(redraw_elements.length / count) + 1; i++) {
let count_list = document.createElement('li');
count_list.setAttribute('data-counter-id', i);
count_list.classList.add('page_number');
count_list.textContent = i;
page_counter.appendChild(count_list);
}
}
// イベント処理
next_btn.addEventListener('click', () => {
split_page(current_step += 1);
});
prev_btn.addEventListener('click', () => {
split_page(current_step -= 1);
});
// class付与・削除関数
prev_btn_active = () => {
prev_btn.classList.add('disable');
}
prev_btn_disable = () => {
prev_btn.classList.remove('disable');
}
next_btn_disable = () => {
next_btn.classList.remove('disable');
}
next_btn_active = () => {
next_btn.classList.add('disable');
}
// DOMの構築が完了したタイミングでページネーション発火
window.addEventListener('DOMContentLoaded', () => {
split_page();
create_page_counter();
document.querySelectorAll('.page_number').forEach((element, index) => {
element.addEventListener('click', function(e) {
current_step = Number(element.getAttribute('data-counter-id'));
split_page(current_step);
})
});
})
Also see: Tab Triggers