Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="accordion">
  <dl class="accordion-box">
    <dt class="question"><span></span>Question1</dt><!-- /.question -->
    <dd class="answer">Answer1 Answer1 Answer1 Answer1 Answer1 Answer1 Answer1 Answer1 Answer1 Answer1</dd><!-- /.answer -->
  </dl><!-- /.accordion-box -->
    <dl class="accordion-box">
    <dt class="question"><span></span>Question2</dt><!-- /.question -->
    <dd class="answer">Answer2 Answer2 Answer2 Answer2 Answer2 Answer2 Answer2 Answer2 Answer2 Answer2</dd><!-- /.answer -->
  </dl><!-- /.accordion-box -->
    <dl class="accordion-box">
    <dt class="question"><span></span>Question3</dt><!-- /.question -->
    <dd class="answer">Answer3 Answer3 Answer3 Answer3 Answer3 Answer3 Answer3 Answer3 Answer3 Answer3</dd><!-- /.answer -->
  </dl><!-- /.accordion-box -->
  <dl class="accordion-box">
    <dt class="question"><span></span>Question4</dt><!-- /.question -->
    <dd class="answer">Answer4 Answer4 Answer4 Answer4 Answer4 Answer4 Answer4 Answer4 Answer4 Answer4</dd><!-- /.answer -->
  </dl><!-- /.accordion-box -->
</div>

<!-- jQueryの読み込み -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
              
            
!

CSS

              
                *,
*::after,
*::before {
  box-sizing: border-box;
}
  
body {
  background: #e6dbc7;
}
  
dl,
dt,
dd {
  margin: 0;
  padding: 0;
}

/*------------------------------

ここからアコーディオンのCSS

------------------------------*/

/* アコーディオンメニューの大きさ */
.accordion {
  width: 500px;
  max-width: 100%;
}

/* アコーディオンメニューの間隔 */
.accordion-box:not(:first-child) {
  margin-top: 20px;
}
  
/* 質問部分 */
.question {
  background: #52a261;
  padding: 20px 20px 20px 80px;
  font-size: 18px;
  color: #fff;
  font-weight: 700;
  position: relative;
}
  
/* Qマーク */
.question::before {
  content: 'Q';
  font-size: 40px;
  color: #1d1d74;
  position: absolute;
  left: 20px;
  top: 50%;
  transform: translateY(-50%);
}
  
/* プラスマークアイコン */
.question span::before,
.question span::after {
  content: '';
  display: block;
  width: 30px;
  height: 3px;
  background: #fff;
  border-radius: 10px;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  transition: .5s;
}

/* プラスマークアイコンの横棒の位置調整など*/
.question span::before {
  right: 30px;
}
  
/* プラスマークアイコンの縦棒の位置調整など*/
.question span::after {
  right: 30px;
  transform: translateY(-50%) rotate(90deg);
}

/* 回答部分が開いている時は縦棒を横にする*/
.question span.open::after {
  transform: translateY(-50%) rotate(0);
}

/* 回答部分は最初非表示 */
.answer {
  display: none;/* 非表示にしておく */
  padding: 20px 20px 20px 80px;
  font-size: 18px;
  background: #fff;
  position: relative;
}

/* Aマーク */
.answer::before {
  content: 'A';
  font-size: 40px;
  font-weight: 700;
  color: #1d1d74;
  position: absolute;
  left: 20px;
  top: 50%;
  transform: translateY(-50%);
}
              
            
!

JS

              
                $(function() {
  $('.question').click(function() {
    //クリックされた質問の子要素のspan以外からはopenというクラスを外す
    $('.question').not(this).children('span').removeClass('open');
    //クリックされた質問部分に対する回答以外は全て閉じる
    $('.question').not(this).next().slideUp(400);
    
    //クリックされた質問の子要素のspanにopenクラスが付与されいなければ付与し、付与されていれば外す
    $(this).children('span').toggleClass('open');
    //クリックされた質問に対する回答を表示する
    $(this).next().slideToggle(400);
  });
});
              
            
!
999px

Console