Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ 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

Auto Save

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

              
                <html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>FAQ</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <style>
            @import url('https://fonts.googleapis.com/css2?family=Anek+Odia:wght@300;400;600&family=Bad+Script&display=swap');
            </style>
    </head>
    <body>
        <div id='bannerLinks'>
            <a target='_blank'
                href='https://github.com/alleycaaat/'>
                AC on GitHub
            </a>
            <a target='_blank'
                href='https://codepen.io/alleycaaat'>
                AC on CodePen
            </a>
        </div>
        <main class="container">
            <h1>Frequently Asked Questions</h1>
            <article class="q" aria-expanded="false">
                <div class="title">
                    <h2>What’s the difference between a hippo and a zippo?</h2>
                    <button class="qb" aria-label="toggle answer">
                        <img
                            src="https://i.ibb.co/KKmX2q3/chevron.png"
                            alt="chevron">
                    </button>
                </div>
                <div class="answer">
                    <p>One is really heavy, the other is a little lighter.</p>
                </div>
            </article>

            <article class="q" aria-expanded="false">
                <div class="title">
                    <h2>Which country's capital has the fastest-growing
                        population?</h2>
                    <button class="qb" aria-label="toggle answer">
                        <img
                            src="https://i.ibb.co/KKmX2q3/chevron.png"
                            alt="chevron">
                    </button>
                </div>
                <div class="answer">
                    <p>Ireland. Every day it's Dublin.</p>
                </div>
            </article>

            <article class="q" aria-expanded="false">
                <div class="title">
                    <h2>What do you get when you mix alcohol and literature?</h2>
                    <button class="qb" aria-label="toggle answer">
                        <img
                            src="https://i.ibb.co/KKmX2q3/chevron.png"
                            alt="chevron">
                    </button>
                </div>
                <div class="answer">
                    <p>Tequila mockingbird</p>
                </div>
            </article>
            <script src="./index.js"></script>
        </main>

        <footer>
            <div class="credit">
                <p>Created by <a href="https://achulslander.com/"
                        target="_blank">AC Hulslander</a>
                </p>
                <p>
                    <a
                        href="https://blog.achulslander.com/how-to-make-an-accordion-faq-with-html-scss-and-js"
                        target="_blank">Learn how to make this accordion FAQ on
                        my blog</a>
                </p>
                <p class="smol">
                    <a target="_blank"
                        href="https://icons8.com/icon/100574/double-down">Double
                        Down</a> icon by <a target="_blank"
                        href="https://icons8.com">Icons8</a>
                </p>
            </div>
        </footer>
    </body>
</html>
              
            
!

CSS

              
                $boxshadow: 0 14px 12px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
$purple: #732091;
//***********************
//*** this styling isn't included in the tutorial
//*** scroll down for the tutorial styles
//***********************
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
body {
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: #FFF;
    font-family: 'Anek Odia', sans-serif;
}
h1,
h2 {
    text-align: center;
    color: $purple;
    font-family: 'Bad Script', cursive;
}
.credit {
    display: flex;
    flex-direction: column;
    margin-top: 20px;
    width: 70vw;
    padding: 7px 19px;
    border: 2px solid $purple;
    background-color: lavender;
    border-radius: 15px;
    box-shadow: $boxshadow;
    p {
        color: #000;
        font-weight: 400;
        text-align: center;
        line-height: 21px;
    }
    .smol {
      font-size: 13px;
    }
    a {
        cursor: pointer;
        text-decoration: none;
        color: $purple;
        padding: 1px 2px;
        border-radius: 3px;
        &:hover {
            text-decoration: underline;
        }
    }
}

//***********************
//*** tutorial styles start here
//***********************
div {
    display: flex;
    margin-left: auto;
    margin-right: auto;
    justify-content: center;
    align-items: center;
}
.container {
    flex-direction: column;
    width: 80vw;
    border-radius: 5px;
    padding: 15px;
    margin: 50px auto auto auto;
}
.q {
    width: 100%;
    flex-direction: column;
    background-color: lavender;
    color: $purple;
    border-radius: 5px;
    padding-top: 3px;
    margin: 5px 5px 17px 5px;
    font-size: 25px;
    font-weight: 600;
    box-shadow: $boxshadow;
    .title {
      width: 100%;
    }
    h2 {
        padding: 7px;
        font-size: 23px;
        margin: auto;
    }
}
button {
    cursor: pointer;
    background-color: rgba(255,255,255,0);
    border: 0px;
    border-radius: 7px;
    padding: 1px 7px;
    color: $purple;
    font-size: 25px;
    font-weight: 600;
    margin-right: 10px;
    text-align: center;
    transition: transform .5s ease-in-out; 
    img {
        height: 25px;
        width: auto;
    }
}
.answer {
    display: none;
    padding: 13px;
    background-color: lavender;
    font-weight: 300;
    font-size: 18px;
    border-radius: 5px;
}
.clicked {
    .answer {
        display: block;
  }
}
 .rotate {
    transform: rotate(180deg);
}

//***********************
//*** also not included in the tutorial
//***********************
@media screen and (min-width: 550px) {
    .credit {
      width: 50vw;
    }
}
@media screen and (max-width: 800px) {
   .q h2 {
    font-size: 18px;
  }
}
@media (hover: hover) {
    button:hover {
        transition: transform .5s ease-in-out;
        transform: scale(1.3);
    }
  .rotate {
    &:hover {
      transform: scale(1.3) rotate(180deg);
    }
  }
}

#bannerLinks {
    width: 220px;
    height: 220px;
    position: fixed;
    display: block;
    top: 0;
    left: 0;
    margin: 0;
    transform: rotate(-45deg);
    a {
        display: block;
        background: rgb(74, 0, 105);
        color: white;
        text-align: center;
        line-height: 21px;
        height: 21px;
        font-size: 15px;
        text-decoration: none;
        margin: 3px;
        font-family: Tahoma, Geneva, Verdana, sans-serif;
        &:nth-child(2) {
            background-color: #838383;
        }
        &:hover {
            text-transform: uppercase;
        }
    }
}
              
            
!

JS

              
                const question=document.querySelectorAll('.q');

question.forEach(function (quest) {
  const btn=quest.querySelector('button');
  let arrow=quest.querySelector('.qb');
 
  btn.addEventListener('click', function() {

    quest.classList.toggle('clicked');
    arrow.classList.toggle('rotate');

    let ariaValue=quest.getAttribute('aria-expanded');
    ariaValue = (ariaValue === 'false') ? quest.setAttribute('aria-expanded', 'true') : quest.setAttribute('aria-expanded', 'false');
  });

});
              
            
!
999px

Console