How we can create a flexbox based accordion using HTML CSS JS? Solution: See this Responsive Flexbox Accordion With CSS jQuery, Flex Box Accordion.
Previously I have shared some accordion related programs, but this accordion design is based on flexbox layout. Basically, accordion is a kind of collapsible function it contains only heading or title, later it reveals all content on click. Mostly we use accordion for FAQ and specification sections. There use places many contents in less space because accordion opens only a single panel at a time.
Today you will learn to create a FlexBox Accordion. Basically, there is accordion with a title, some dummy text, and 4 accordion or collapsible items. There each item contains an image, a title, and some dummy text when its reveal. On the desktop, you will see 2 items in a row, and on the mobile or small screen, you will see one after one list-wise. And it has a toggle feature, items will open and close on clicks.
So, Today I am sharing Responsive Flexbox Accordion With CSS jQuery. There I have used HTML to create the layout, CSS for styling and animation, jQuery for functioning. There I have created 2 separate sections for desktop and mobile view. This is a cool accordion design with images, you can replace images with icons. You can use this program on your website after making content changes.
If you are thinking now how this flex box accordion actually is, then see the preview given below.
Preview Of Flex Box Accordion
See this video preview to getting an idea of how this responsive accordion design looks like.
Now you can see this program visually, also you can see it live by pressing the button above. If you like this, then get the source code of its.
You May Also Like:
- Responsive Table Detail View
- Material Color Picker
- Folding Select Dropdown
- Multi-Level Accordion Menu
Responsive Flexbox Accordion Source Code
Before sharing source code, let’s talk about it. There I have created two main divs name containers and 2 other additional class names. There I have created two different sections for desktop view and mobile view. Inside these two divs, I have placed the same elements with the same class names. Also in the HTML file, I have linked other files like CSS, JS, and jQuery CDN.
Now using CSS I have placed all the items in the right place, as you can see in the preview. With CSS first I gave basic values like size, position, margin, padding, etc to all the elements. First I gave display: none; values to the mobile version of the element, and in @media query I gave none display to the desktop version. In other words, on a small screen, the mobile version layout will active and on a large screen desktop version will active.
jQuery handling here the toggle item reveal and hide feature in this program. At first, it will hide the content and it detects the click using .click(function ()) command and open/close the items. There I have used if{} else{} statements and declared the conditions. Left all other things you will understand after getting the codes, I can’t explain all in writing.
For creating this program you have to create 3 files. First file for HTML, second for CSS, and third file for JavaScript. Follow the steps to creating this program without any error.
index.html
Create an HTML file named ‘index.html‘ and put the codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
<!DOCTYPE html> <!--Code By Webdevtrick (https://webdevtrick.com)--> <html lang="en" > <head> <meta charset="UTF-8"> <title>Flex Box Accordion | Webdevtrick.com</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Josefin+Sans:400,400i,700&display=swap'> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container desktop"> <header class="title"> <h1> Social Media Marketing Accordion </h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In volutpat elit euismod, convallis ligula in, dictum urna. Nunc et iaculis diam.</p> </header> <section class="tasks"> <div class="tasks-grp-1"> <div class="task-desc-title"> <img src="https://webdevtrick.com/wp-content/uploads/whatsapp.png" alt="WhatsApp Marketing Tool"> <div class="one">WhatsApp Marketing Tool</div> </div> <div class="task-desc-title"> <img src="https://webdevtrick.com/wp-content/uploads/twitter.png" alt="Twitter Marketing Service"> <div class="two">Twitter Marketing Service</div> </div> </div> <p class="desc one-desc-style"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. In volutpat elit euismod, convallis ligula in, dictum urna. Nunc et iaculis diam. </p> <p class="desc two-desc-style"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. In volutpat elit euismod, convallis ligula in, dictum urna. Nunc et iaculis diam. </p> <div class="tasks-grp-2"> <div class="task-desc-title"> <img src="https://webdevtrick.com/wp-content/uploads/instagram.png" alt="Instagram Marketing Tool"> <div class="three">Instagram Marketing Tool</div> </div> <div class="task-desc-title"> <img src="https://webdevtrick.com/wp-content/uploads/facebook.png" alt="Facebook Audience Network"> <div class="four">Facebook Audience Network</div> </div> </div> <p class="desc three-desc-style"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. In volutpat elit euismod, convallis ligula in, dictum urna. Nunc et iaculis diam. </p> <p class="desc four-desc-style"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. In volutpat elit euismod, convallis ligula in, dictum urna. Nunc et iaculis diam. </p> </section> </div> <div class="container mobile"> <header class="title"> <h1> Social Media Marketing Accordion </h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In volutpat elit euismod, convallis ligula in, dictum urna. Nunc et iaculis diam.</p> </header> <section class="tasks"> <div class="task-desc"> <div class="task-desc-title"> <img src="https://webdevtrick.com/wp-content/uploads/whatsapp.png" alt=""> <div class="one">WhatsApp Marketing Tool</div> </div> <p class="desc one-desc-style"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. In volutpat elit euismod, convallis ligula in, dictum urna. Nunc et iaculis diam. </p> </div> <div class="task-desc"> <div class="task-desc-title"> <img src="https://webdevtrick.com/wp-content/uploads/twitter.png" alt=""> <div class="two">Twitter Marketing Service</div> </div> <p class="desc two-desc-style"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. In volutpat elit euismod, convallis ligula in, dictum urna. Nunc et iaculis diam. </p> </div> <div class="task-desc"> <div class="task-desc-title"> <img src="https://webdevtrick.com/wp-content/uploads/instagram.png" alt=""> <div class="three">Instagram Marketing Tool</div> </div> <p class="desc three-desc-style"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. In volutpat elit euismod, convallis ligula in, dictum urna. Nunc et iaculis diam. </p> </div> <div class="task-desc"> <div class="task-desc-title"> <img src="https://webdevtrick.com/wp-content/uploads/facebook.png" alt=""> <div class="four">Facebook Audience Network</div> </div> <p class="desc four-desc-style"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. In volutpat elit euismod, convallis ligula in, dictum urna. Nunc et iaculis diam. </p> </div> </section> </div> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js'></script> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
/* Code By Webdevtrick (https://webdevtrick.com) */ * { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } body { font-size: 100%; font-family: 'Josefin Sans', sans-serif; color: #4A4A4A; display: flex; justify-content: center; background-image: linear-gradient(180deg, #F5F5F5 0%, #FFFFFF 100%); background-size: cover; min-height: 100vh; } body .container { padding: 0 16px; min-width: 750px; max-width: 1000px; height: 100%; } body .container.mobile { display: none; } body .container.desktop { overflow: hidden; } body .container .title h1 { height: 35px; top: calc(50% - 35px/2 - 163px); font-style: normal; font-weight: bold; font-size: 35px; line-height: 35px; text-align: center; margin: 50px auto 21px auto; } body .container .title p { font-style: normal; font-weight: normal; font-size: 20px; line-height: 28px; text-align: center; margin: 0 auto; max-width: 718px; } body .container .tasks { margin-top: 40px; margin-right: auto; margin-left: auto; } body .container .tasks .tasks-grp-1, body .container .tasks .tasks-grp-2 { display: flex; justify-content: space-between; margin: 0 auto; } body .container .tasks .tasks-grp-2 { margin-top: 32px !important; } body .container .tasks .task-desc-title { cursor: pointer; display: flex; width: 310px; } body .container .tasks .task-desc-title:hover .one { color: #96df72; } body .container .tasks .task-desc-title:hover .two { color: #7acbff; } body .container .tasks .task-desc-title:hover .three { color: #f94f79; } body .container .tasks .task-desc-title:hover .four { color: #3798f1; } body .container .tasks .task-desc-title div { align-self: center; margin-left: 10px; } body .container .tasks .desc { width: 600px; text-align: center; margin: 10px auto; } body .container .tasks .desc.one-desc-style { margin-top: 10px; border-top: 3px solid #96df72; background-color: rgba(150, 223, 114, 0.2); border-radius: 5px; padding: 10px; } body .container .tasks .desc.two-desc-style { margin-top: 10px; border-top: 3px solid #7acbff; background-color: rgba(122, 203, 225, 0.2); border-radius: 5px; padding: 10px; } body .container .tasks .desc.three-desc-style { margin-top: 10px; border-top: 3px solid #f94f79; background-color: rgba(249, 79, 121, 0.2); border-radius: 5px; padding: 10px; } body .container .tasks .desc.four-desc-style { margin-top: 10px; border-top: 3px solid #3798f1; background-color: rgba(55, 152, 241, 0.2); border-radius: 5px; padding: 10px; } @media screen and (max-width: 720px) { body .container { max-width: 375px; min-width: 340px; margin: 0 auto; width: 100%; } body .container.mobile { display: block; } body .container.desktop { display: none; } body .container .title h1 { width: 280px; height: 70px; top: calc(50% - 70px/2 - 294.5px); font-style: normal; font-weight: bold; font-size: 35px; line-height: 35px; text-align: center; margin: 39px auto 35px auto; } body .container .title p { font-style: normal; font-weight: normal; font-size: 20px; line-height: 28px; text-align: center; margin: 0 auto; max-width: 343px; } body .container .tasks { padding: 0 16px; display: flex; flex-direction: column; margin-top: 48px; justify-content: center; } body .container .tasks .task-desc { margin-bottom: 47px; } body .container .tasks .task-desc img { max-width: 65px; max-height: 65px; } body .container .tasks .task-desc .desc { width: 300px; } } |
function.js
The final step, create a JavaScript file named ‘function.js‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
// initiates the jquery library $(document).ready(function(){ // hides the paragraph to be displayed via Javascript $('.desc').hide(); //stores the total number of indicated classes(in this case , hidden paragraph titles) in the variable 'numberOfDesc' var numberOfDesc = $('.task-desc-title').length; //initiates the 'while loop' inidicated below var i = 0; //start while loop while(i <= numberOfDesc) { function tasks(i) { $('.task-desc-title').eq(i).click(function () { //this closes any paragraph that might be open. can be seen as default behaviour. $('.desc').slideUp(); // opens paragraph (if it's closed) with equivalent index to clicked '.task-desc-title' if($('.desc').eq(i).is(':hidden')){ $('.desc').eq(i).slideDown("slow"); } else { // hides paragraph if its visible $('.desc').eq(i).slideUp(); } }); } tasks(i); i++; //repeats the task till last declared index } }); |
That’s It. Now you have successfully created Responsive Flexbox Accordion With CSS jQuery, Flex Box Accordion Program. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.