Responsive Accordion Menu using HTML, CSS & JavaScript with modern UI

Responsive Accordion Menu using HTML, CSS & JavaScript with modern UI

Hello developers, today in this blog you'll learn to create a Responsive Accordion Menu using HTML, CSS & JavaScript with modern UI.

Accordion Menu mainly consists of text articles with the drop-down feature. Accordion takes a small area that displays only the question or heading to the user, if the user wants to see the answer or subdivisions then they need to open it by clicking or by hovering on the accordion.


In this Responsive Accordion Menu or Frequently Asked Questions, there are five questions or headers. Here the heading only will appear and the content of that heading will be hidden. The content can be viewed by clicking or hovering on the question. 

The content of one header at one time means that, if the content of the second header is open then the content of the first header will automatically get closed. Icons are used, to indicate to the user that the answer is currently hidden or expanded.

JavaScript code has been used for every click on the accordion menu, which helps in revealing and hiding the content.

The source code of this Responsive Accordion Menu is given below, if you want the source code of this program, you can copy it. You can use this Responsive Accordion Menu code on your projects.


Responsive Accordion Menu using HTML, CSS & JavaScript with modern UI [Source Code]

To create this website, make three files: an HTML file, a CSS file & a JavaScript file. First, create an HTML file with the name of index.html and remember, you've to form a file with a .html extension.


<!DOCTYPE html> <html> <head> <title>Accordion menu || Learning Robo</title> <link rel="stylesheet" href="style.css"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <h1>Frequently Asked Questions</h1> <div class="accordion"> <div class="accordion-item"> <div class="accordion-item-header"> What is Web Development? </div> <div class="accordion-item-body"> <div class="accordion-item-body-content"> Web Development broadly refers to the tasks associated with developing functional websites and applications for the Internet. The web development process includes web design, web content development, client-side/server-side scripting and network security configuration, among other tasks. </div> </div> </div> <div class="accordion-item"> <div class="accordion-item-header"> What is HTML? </div> <div class="accordion-item-body"> <div class="accordion-item-body-content"> HTML, stands for HyperText Markup Language, is the dominant markup language for creating websites and anything that can be viewed in a web browser. </div> </div> </div> <div class="accordion-item"> <div class="accordion-item-header"> What is Python exactly? </div> <div class="accordion-item-body"> <div class="accordion-item-body-content"> Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. ... Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. </div> </div> </div> <div class="accordion-item"> <div class="accordion-item-header"> What is HTTP? </div> <div class="accordion-item-body"> <div class="accordion-item-body-content"> HTTP, stands for HyperText Transfer Protocol, is the underlying protocol used by the World Wide Web and this protocol defines how messages are formatted and transmitted, and what actions Web servers and browsers should take in response to various commands. </div> </div> </div> <div class="accordion-item"> <div class="accordion-item-header"> What does JavaScript do? </div> <div class="accordion-item-body"> <div class="accordion-item-body-content"> JavaScript is a scripting language that enables you to create dynamically updating content, control multimedia, animate images, and pretty much everything else. </div> </div> </div> </div> <div class="credit">Made with <span style="color:tomato">&#10084;</span> by <a href="https://www.learningrobo.com/">Learning Robo</a></div> <script src="script.js"></script> </body> </html>

CSS provides style to an HTML page. To make the page attractive and page responsive create a CSS file with the name style.css and remember that you've got need to make a file with a .css extension.


@import url('https://fonts.googleapis.com/css?family=Montserrat');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  font-family: 'Montserrat', sans-serif;
  background-color: #FBAB7E;
background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%);
  color: #fff;
}
h1 {
  text-align: center;
  margin: 2rem 0;
  font-size: 2.5rem;
  color:#202124
}

.accordion {
  width: 90%;
  max-width: 1000px;
  margin: 2rem auto;
}
.accordion-item {
  background-color: #202124;
  color: #ddd;
  margin: 1rem 0;
  border-radius: 0.8rem;
  box-shadow: 0 2px 5px 0 rgba(0,0,0,0.25);
}
.accordion-item-header {
  padding: 0.5rem 3rem 0.5rem 1rem;
  min-height: 3.5rem;
  line-height: 1.25rem;
  font-weight: bold;
  display: flex;
  align-items: center;
  position: relative;
  cursor: pointer;
}
.accordion-item-header::after {
  content: "\002B";
  font-size: 2rem;
  position: absolute;
  right: 1rem;
}
.accordion-item-header.active::after {
  content: "\2212";
}
.accordion-item-body {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease-out;
}
.accordion-item-body-content {
  padding: 1rem;
  line-height: 1.5rem;
  border-top: 1px solid;
  border-image: linear-gradient(to right, transparent, orange, transparent) 1;
}

@media(max-width:767px) {
  html {
    font-size: 14px;
  }
}


.credit{
    text-align: center;
    color: #fff;
    font-weight: 900;
    font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
}

.credit a{
    text-decoration: none;
    color:#202124;
    font-weight: bold;
}


JavaScript makes the page work functionally. At last, create a JavaScript file with the name of script.js, and remember that you’ve got to make a file with a .js extension.

const accordionItemHeaders = document.querySelectorAll(".accordion-item-header"); accordionItemHeaders.forEach(accordionItemHeader => { accordionItemHeader.addEventListener("click", event => { const currentlyActiveAccordionItemHeader = document.querySelector(".accordion-item-header.active"); if(currentlyActiveAccordionItemHeader && currentlyActiveAccordionItemHeader!==accordionItemHeader) { currentlyActiveAccordionItemHeader.classList.toggle("active"); currentlyActiveAccordionItemHeader.nextElementSibling.style.maxHeight = 0; } accordionItemHeader.classList.toggle("active"); const accordionItemBody = accordionItemHeader.nextElementSibling; if(accordionItemHeader.classList.contains("active")) { accordionItemBody.style.maxHeight = accordionItemBody.scrollHeight + "px"; } else { accordionItemBody.style.maxHeight = 0; } }); });


We hope you would like this Responsive Accordion Menu using HTML, CSS & JavaScript with modern UI.


Thank you for reading our blog. If you face any problem in creating this Responsive Accordion Menu using HTML, CSS & JavaScript with modern UI, then contact us or comment us. We’ll try to provide a solution to your problem as soon as possible.

 

 

 

Thank you
Learning robo team

إرسال تعليق

Thank you
Learning robo team

Post a Comment (0)

أحدث أقدم
Learning Robo says...
code copied
Welcome