アコーディオン:FAQ形式|Accordion: FAQ Style
2025
8/18
Q: What is this accordion?
A: This is a simple Q&A style accordion that adopts clean Material Design principles. It displays questions as buttons and provides clear answers when expanded.
HTML
<div class="container">
<div class="accordion">
<div class="btn-box">
<button data-default-text="Q: What is this accordion?" data-open-text="Close">Q: What is this accordion?</button>
</div>
<div class="more">
<ul>
<li>A: This is a simple Q&A style accordion that adopts clean Material Design principles. It displays questions as buttons and provides clear answers when expanded.</li>
</ul>
</div>
</div>
</div>
CSS
@keyframes materialFadeIn {
0% {
opacity: 0;
transform: translateY(-10px);
}
100% {
opacity: 1;
transform: translateY(0);
}
}
.accordion {
margin: 25px 0;
position: relative;
}
.btn-box button {
width: 100%;
padding: 20px 24px;
background: #f5f5f5;
border: none;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
color: #333;
cursor: pointer;
transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
text-align: left;
position: relative;
overflow: hidden;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.btn-box button:hover {
background: #e0e0e0;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}
.btn-box button:active {
background: #d5d5d5;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}
.more {
max-height: 0;
overflow: hidden;
transition: max-height 0.3s cubic-bezier(0.4, 0, 0.2, 1);
background: #fff;
border-radius: 0 0 8px 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin-top: -4px;
position: relative;
z-index: 1;
}
.more.appear {
max-height: 500px;
animation: materialFadeIn 0.3s ease;
}
.accordion:has(.more.appear) .btn-box button::after {
transform: translateY(-50%) rotate(180deg);
}
.more ul {
list-style: none;
padding: 16px 24px;
margin: 0;
}
.more li {
padding: 16px 0;
color: #333;
font-size: 15px;
line-height: 1.6;
font-weight: 400;
transition: all 0.2s ease;
}
.more li:hover {
color: #000;
}
.more li:last-child {
border-bottom: none;
}
/* レスポンシブ対応 */
@media (max-width: 768px) {
.btn-box button {
font-size: 14px;
padding: 16px 20px;
}
.more ul {
padding: 12px 20px;
}
.more.appear {
max-height: 400px;
}
}
JavaScript
document.addEventListener('DOMContentLoaded', function() {
const buttons = document.querySelectorAll('.btn-box button');
buttons.forEach(button => {
button.addEventListener('click', function() {
const more = this.parentElement.nextElementSibling;
const isOpen = more.classList.contains('appear');
// 他のアコーディオンを閉じる
document.querySelectorAll('.more').forEach(item => {
item.classList.remove('appear');
});
// クリックされたアコーディオンの開閉
if (!isOpen) {
more.classList.add('appear');
} else {
more.classList.remove('appear');
}
});
});
});