Accordion Expand Collapse Animation CSS

Accordion Expand Collapse Animation CSS
Code Snippet:Pure CSS Expand Collapse List
Author: el_guapo
Published: January 19, 2024
Last Updated: January 22, 2024
Downloads: 9,734
License: MIT
Edit Code online: View on CodePen
Read More

This pure CSS code snippet helps you to create an accordion expand collapse animation with plus/minus icons. It uses Font Awesome for icons and CSS transition property for smooth expand/collapse animation.

This accordion list menu is useful to display longer content or FAQs on a webpage. Basically, it comes with a simple and clean design, anyhow you can further style it according to your needs.

How to Create Accordion Expand Collapse Animation CSS

1. First of all, load the Font Awesome CSS for icons by adding the following CDN link into the head tag of your HTML document. The icons CSS is optional, you can skip this step if you don’t want to show plus icon at the end of accordion title.

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">

2. After that, create the HTML structure for the accordion as follows:

<ul class="m-d expand-list">
	<li data-md-content="200">
		<label name="tab" for="tab1" tabindex="-1" class="tab_lab" role="tab">Product Description</label>
		<input type="checkbox" checked class="tab" id="tab1" tabindex="0" />
		<span class="open-close-icon">
			<i class="fas fa-plus"></i>
			<i class="fas fa-minus"></i>
		</span>
		<div class="content">
            Welcome to Brackets, a modern open-source code editor that understands web design. It's a lightweight,
            yet powerful, code editor that blends visual tools into the editor so you get the right amount of help
            when you want it.
        </div>
    </li>
    <li data-md-content="300">
			<label name="tab" for="tab2" tabindex="-1" class="tab_lab" role="tab">Specifications</label>
			<input type="checkbox" class="tab" id="tab2" tabindex="0" />
			<span class="open-close-icon"><i class="fas fa-plus"></i><i class="fas fa-minus"></i></span>
			<div class="content">
				<em>Brackets is a different type of editor.</em>
            Brackets has some unique features like Quick Edit, Live Preview and others that you may not find in other
            editors. Brackets is written in JavaScript, HTML and CSS. That means that most of you using Brackets
            have the skills necessary to modify and extend the editor. In fact, we use Brackets every day to build
            Brackets. To learn more about how to use the key features, read on.
        </div>
    </li>
    <li data-md-content="600">
			<label name="tab" for="tab3" tabindex="-1" class="tab_lab" role="tab">Shipping &amp; Returns</label>
			<input type="checkbox" class="tab" id="tab3" tabindex="0" />
			<span class="open-close-icon"><i class="fas fa-plus"></i><i class="fas fa-minus"></i></span>
      <div class="content">
				<h3>Projects in Brackets</h3>
        <p>
            In order to edit your own code using Brackets, you can just open the folder containing your files.
            Brackets treats the currently open folder as a "project"; features like Code Hints, Live Preview and
            Quick Edit only use files within the currently open folder.
        </p>
        <samp>
            Once you're ready to get out of this sample project and edit your own code, you can use the dropdown
            in the left sidebar to switch folders. Right now, the dropdown says "Getting Started" - that's the
            folder containing the file you're looking at right now. Click on the dropdown and choose "Open Folder…"
            to open your own folder.
            You can also use the dropdown later to switch back to folders you've opened previously, including this
            sample project.
        </samp>
			</div>
    </li>
</ul>

3. Finally, use the following CSS to style and functionalize the accordion:

.m-d.expand-list{
        margin: 0;
        padding: 0;
    }
    .m-d.expand-list > li{
        list-style-type: none;
        padding: 15px 0;
        border-bottom: 1px solid #212121;
        position: relative;
/*         max-width: 80%; */
    }
    .m-d label[class^="tab"]:hover{
        cursor: pointer;
    }
    .m-d input{
        -webkit-appearance: none;
        -moz-appearance: none;
        appearance: none;
    }
    .m-d input[class^="tab"]{
        width: 100%;
        height: 40px;
        position: absolute;
        left: 0;
        top: 0; 
    }
		.m-d input[class^="tab"]:hover{
			cursor: pointer;
		}
    .m-d label[class^="tab"]{
        font-weight: bold;
    }
    .m-d .content{
        height: auto;
        max-height: 0;
        max-width: 300px;
/*        background: yellow;*/
        overflow: hidden;
        transform: translateY(20px);
        transition: all 180ms ease-in-out 0ms; 
    }
    .m-d li[data-md-content="100"] input[class^="tab"]:checked ~ .content{
        max-height: 100px;
        transition: all 150ms ease-in-out 0ms;
    }
    .m-d li[data-md-content="200"] input[class^="tab"]:checked ~ .content{
        max-height: 200px;
        transition: all 200ms ease-in-out 0ms;
    }
    .m-d li[data-md-content="300"] input[class^="tab"]:checked ~ .content{
        max-height: 300px;
        transition: all 250ms ease-in-out 0ms;
    }
    .m-d li[data-md-content="400"] input[class^="tab"]:checked ~ .content{
        max-height: 400px;
        transition: all 250ms ease-in-out 0ms;
    }
    .m-d li[data-md-content="500"] input[class^="tab"]:checked ~ .content{
        max-height: 500px;
        transition: all 250ms ease-in-out 0ms;
    }
    .m-d li[data-md-content="600"] input[class^="tab"]:checked ~ .content{
        max-height: 600px;
        transition: all 250ms ease-in-out 0ms;
    }
    .m-d li[data-md-content="700"] input[class^="tab"]:checked ~ .content{
        max-height: 700px;
        transition: all 300ms ease-in-out 0ms;
    }
    .m-d li[data-md-content="800"] input[class^="tab"]:checked ~ .content{
        max-height: 800px;
        transition: all 300ms ease-in-out 0ms;
    }
    .m-d li[data-md-content="900"] input[class^="tab"]:checked ~ .content{
        max-height: 900px;
        transition: all 300ms ease-in-out 0ms;
    }
    .m-d li[data-md-content="1000"] input[class^="tab"]:checked ~ .content{
        max-height: 1000px;
        transition: all 350ms ease-in-out 0ms;
    }
		.m-d li[data-md-content=""] input[class^="tab"]:checked ~ .content{
        max-height: 1000px;
        transition: all 250ms ease-in-out 0ms;
    }
    .m-d input[class^="tab"]:checked ~ .content{
        margin-bottom: 20px;
    }
    
    .m-d .open-close-icon{
        display: inline-block;
        position: absolute;
        right: 20px;
        transform: translatey(2px);
    }
    .m-d .open-close-icon i{
        position: absolute;
        left: 0;
    }
    .m-d .open-close-icon .fa-minus{
        transform:rotate(-90deg);
        transition: transform 150ms ease-in-out 0ms;
    }
    .m-d input[class^="tab"]:checked ~ .open-close-icon .fa-minus{
        transform: rotate(0deg);
        transition: transform 150ms ease-in-out 0ms;
    }
    .m-d .open-close-icon .fa-plus{
        opacity: 1;
        transform:rotate(-90deg);
        transition: opacity 50ms linear 0ms, transform 150ms ease-in-out 0ms;
    }
    .m-d input[class^="tab"]:checked ~ .open-close-icon .fa-plus{
        opacity: 0;
        transform: rotate(0deg);
        transition: opacity 50ms linear 0ms, transform 150ms ease-in-out 0ms; 
    }
    
*{
	line-height: 1.4;
	font-family: "harmonia sans", roboto, arial;
}

That’s all! hopefully, you have successfully created accordion expand collapse animation. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X