Learn CSS from the Best Tutors
Search in
To add and remove CSS classes based on an accordion element's state, you can use JavaScript or jQuery to handle the interactions. Below is an example using JavaScript to toggle classes when an accordion is opened or closed.
Assuming you have HTML markup for your accordion:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Accordion Example</title>
<style>
.accordion {
display: none;
padding: 10px;
margin: 5px;
border: 1px solid #ddd;
background-color: #f9f9f9;
}
.accordion.active {
display: block;
}
.accordion-title {
cursor: pointer;
padding: 10px;
background-color: #ddd;
}
</style>
</head>
<body>
<div class="accordion-container">
<div class="accordion-title" onclick="toggleAccordion('accordion1')">Accordion 1</div>
<div id="accordion1" class="accordion">
<!-- Accordion 1 Content -->
Content for Accordion 1
</div>
<div class="accordion-title" onclick="toggleAccordion('accordion2')">Accordion 2</div>
<div id="accordion2" class="accordion">
<!-- Accordion 2 Content -->
Content for Accordion 2
</div>
<!-- Add more accordion sections as needed -->
</div>
<script>
function toggleAccordion(accordionId) {
var accordion = document.getElementById(accordionId);
accordion.classList.toggle('active');
}
</script>
</body>
</html>
```
In this example:
- The CSS styles define the initial appearance of the accordion elements.
- The JavaScript function `toggleAccordion` takes an `accordionId` as a parameter and toggles the 'active' class on the corresponding accordion element. This class controls the visibility of the accordion content.
You can adapt this example to your specific accordion implementation and styling. If you're using a more complex JavaScript framework like React or Angular, the approach may be different, and you might use state management to handle accordion states.
Related Questions
Why is HTML is used in Web Application?
Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com
Ask a QuestionRecommended Articles
Why Should you Learn HTML
Since the world today is ruled by the Internet, everyone desires to build a website for either business or personal interests. HTML or Hyper-text Mark-up Language is a globally standardized language which used to format web pages. By using HTML, the appearance of text, links, images and almost every part of a web page could...
Skills Required to Become a Front End Web...
Today, no business can exist without having its own website to interact with its customer base. Having a website is no more restricted to the big MNCs. With the advancement in technology and global business, even the small enterprises are spending on having their own websites and development teams. The person works on web...
8 Hottest IT Careers of 2014!
Whether it was the Internet Era of 90s or the Big Data Era of today, Information Technology (IT) has given birth to several lucrative career options for many. Though there will not be a “significant" increase in demand for IT professionals in 2014 as compared to 2013, a “steady” demand for IT professionals is rest assured...
Make a Career as a BPO Professional
Business Process outsourcing (BPO) services can be considered as a kind of outsourcing which involves subletting of specific functions associated with any business to a third party service provider. BPO is usually administered as a cost-saving procedure for functions which an organization needs but does not rely upon to...
Looking for CSS Training?
Learn from the Best Tutors on UrbanPro
Are you a Tutor or Training Institute?
Join UrbanPro Today to find students near youThe best tutors for CSS Classes are on UrbanPro
The best Tutors for CSS Classes are on UrbanPro