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.
<ul class="include-accordion scroll-control">
<li>
<button class="accordionBtn" type="button">Menu-1</button>
<ul>
<li>List 1-1</li>
<li>List 1-2</li>
<li>List 1-3</li>
</ul>
</li>
<li>
<button class="accordionBtn" type="button">Menu-2</button>
<ul>
<li>List 2-1</li>
<li>List 2-2</li>
<li>List 2-3</li>
</ul>
</li>
<li>
<button class="accordionBtn" type="button">Menu-3</button>
<ul>
<li>List 3-1</li>
<li>List 3-2</li>
<li>List 3-3</li>
</ul>
</li>
</ul>
<ul class="include-accordion scroll-control">
<li>
<button class="accordionBtn" type="button">Menu-1</button>
<ul>
<li>List 1-1</li>
<li>List 1-2</li>
<li>List 1-3</li>
</ul>
</li>
<li>
<button class="accordionBtn" type="button">Menu-2</button>
<ul>
<li>List 2-1</li>
<li>List 2-2</li>
<li>List 2-3</li>
</ul>
</li>
<li>
<button class="accordionBtn" type="button">Menu-3</button>
<ul>
<li>List 3-1</li>
<li>List 3-2</li>
<li>List 3-3</li>
</ul>
</li>
</ul>
ul{
background-color: #35924A;
width: 150px;
padding: 0;
color: #fff;
float: left;
margin-left:30px;
}
li{
list-style: none;
}
ul ul{
height: 0;
padding: 0;
overflow: hidden;
transition: .5s;
border-top: 1px solid #67a863;
background-color: #5EAA6C;
margin:0;
}
ul li li{
border-bottom: 1px dotted #7FBF8B;
padding: 10px 0 10px 10px;
margin-left:15px;
}
ul:nth-of-type(1) li.active li:last-child{
border-bottom:1px solid #67a863;
}
button{
position: relative;
border: none;
width: 100%;
background-color: inherit;
color: #fff;
cursor: pointer;
text-align: left;
padding: 15px 0 15px 20px;
font-size:1em;
}
button:hover{
background-color: #1A5B27;
}
button::before,
button::after{
content:"";
position: absolute;
top: 20px;
width: 1.5px;
height: 8px;
background-color: #fff;
transition: .5s;
}
button::before{
transform: rotate(-45deg);
right: 35px;
}
button::after{
transform: rotate(45deg);
right: 30px;
}
li.active button::before{
transform: rotate(-135deg);
transition:.5s;
}
li.active button::after{
transform: rotate(135deg);
transition:.5s;
}
ul:nth-of-type(2){ background-color: #357D87; }
ul:nth-of-type(2) ul{ background-color: #519FA5; border-top: 1px solid #5D9FA8; }
ul:nth-of-type(2) button:hover{ background-color: #1C4B56; }
ul:nth-of-type(2) li li{ border-bottom: 1px dotted #73BEBF; }
ul:nth-of-type(2) li.active li:last-child{ border-bottom:1px solid #5D9FA8; }
ul.active{ overflow-y: auto; }
// メニューを開く関数
const slideDown = (el) => {
el.style.height = 'auto'; //いったんautoに
let h = el.offsetHeight; //autoにした要素から高さを取得
el.style.height = h + 'px';
el.animate([ //高さ0から取得した高さまでのアニメーション
{ height: 0 },
{ height: h + 'px' }
], {
duration: 300, //アニメーションの時間(ms)
});
};
// メニューを閉じる関数
const slideUp = (el) => {
el.style.height = 0;
};
let activeIndex = null; //開いているアコーディオンのindex
//アコーディオンコンテナ全てで実行
const accordions = document.querySelectorAll('ul.include-accordion');
accordions.forEach((accordion) => {
//アコーディオンボタン全てで実行
const accordionBtns = accordion.querySelectorAll('.accordionBtn');
accordionBtns.forEach((accordionBtn, index) => {
accordionBtn.addEventListener('click', (e) => {
activeIndex = index; //クリックされたボタンを把握
e.target.parentNode.classList.toggle('active'); //ボタンの親要素(=ul>li)にクラスを付与/削除
const content = accordionBtn.nextElementSibling; //ボタンの次の要素(=ul>ul)
if(e.target.parentNode.classList.contains('active')){
slideDown(content); //クラス名がactive(=閉じていた)なら上記で定義した開く関数を実行
}else{
slideUp(content); //クラス名にactiveがない(=開いていた)なら上記で定義した閉じる関数を実行
}
accordionBtns.forEach((accordionBtn, index) => {
if (activeIndex !== index) {
accordionBtn.parentNode.classList.remove('active');
const openedContent = accordionBtn.nextElementSibling;
slideUp(openedContent); //現在開いている他のメニューを閉じる
}
});
//スクロール制御のために上位階層ulのクラス名を変える
let container = accordion.closest('.scroll-control'); //sroll-controlnのクラス名である親要素を取得
if(accordionBtn.parentNode.classList.contains('active') == false && container !== null ){
container.classList.remove('active')
}else if (container !== null){
container.classList.add('active')
}
});
});
});
Also see: Tab Triggers