Struggling to create an accordion using pure CSS
Despite numerous attempts, I have been unsuccessful in achieving the desired outcome.
Struggling to create an accordion using pure CSS
Despite numerous attempts, I have been unsuccessful in achieving the desired outcome.
It's impossible to create anything without utilizing HTML. Bootstrap is essentially a tool that enhances the functionality of HTML, CSS, and JS. HTML dictates the structure of elements, while CSS determines the appearance and behavior (JavaScript may also be necessary for certain functionalities).
An accordion, for instance, necessitates all three components (Refer to this question/answer.)
Since the other question is inaccessible, here is the gist of my response for non-2k reputation users:
There isn't a specific HTML tag dedicated to creating accordions.
You have to construct these "accordions" on your own. The simplest method would be to follow this W3Schools tutorial.
It involves three key elements:
You'll require a button and another div for the "dropdown" content. W3Schools refers to it as a panel. Assign unique classes to both elements to simplify future tasks.
While most CSS attributes are focused on aesthetics, there are two critical rules you must include:
For the button
width: 100%;
The button should occupy the entire width to prevent adjacent elements from disrupting the "dropdown" effect when the panel is opened.
For the panel
display: none;
Initially, the panel should remain hidden until triggered by clicking the button.
You need to set up an event listener (find more about events here in the MDN documentation) to respond to clicks on buttons with associated panels.
If all buttons with panels share a common class, you can iterate through them effortlessly. Within the event listener, toggle the active attribute and access the relevant panel.
let panel = this.nextElementSibling;
To adjust the visual state based on user actions:
if (panel.style.display === "none") {
panel.style.display = "block"; // Make it visible
}
else {
panel.style.display = "none"; // Hide it again
}
});
Additionally, you could modify the value of the aria-hidden
attribute if needed (though omit it when employing display:none
).
if (panel.style.aria-hidden === "true") {
panel.ariaHidden = "false"; // Make it visible for assistive technologies
}
else {
panel.ariaHidden = "true"; // Conceal content from assistive technologies
}
});
Here's a condensed version of the W3Schools tutorial code:
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
});
}
.accordion {
width: 100%;
}
.panel {
display: none;
}
<button class="accordion">Section 1</button>
<div class="panel">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<button class="accordion">Section 2</button>
<div class="panel">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
I'm currently facing an issue with adding a jQuery slider to my website. It seems that the slider is not displaying images that are saved on my computer, only images from external websites. Below is the code snippet where I have included an image fil ...
I recently developed a custom column filtering plugin for datatables, but I've encountered a minor issue. Within each column footer, I have added text inputs, and now I am trying to capture their indexes on keyup event to use them for filtering. In ...
My understanding of Redux is still quite hazy as I delve into various techniques and methods. I am curious to learn about other methods similar to redux-Thunk and redux-saga. Each utilizes distinct functions such as CreateSlice. I am interested to know w ...
Hey there! I'm currently facing a bit of a challenge while trying to set up a JSON array using PHP and integrate it into Highcharts. Currently, I am creating the array in this manner: $stack[] = array($commname => $countit); $stack = json_encode( ...
When working with Script#, I often utilize mouse events using the ElementEvent argument. However, one thing seems to be missing - the WheelDelta property for handling the onmousewheel event. Can anyone provide guidance on how to access this property in t ...
https://i.stack.imgur.com/rBL50.png Whenever my page loads, there is a card with a vertical list of images. Due to the loading time of the images, the text appears first and gets squished as shown in the provided screenshot. Is there a way for the text to ...
Is there a website or webpage that can be used to input a user's IP address and receive their country or location as plain text? Below is a code snippet demonstrating how I attempted to retrieve the user's IP address: I inserted the following c ...
Lately, I've been diving into Angular 2 and I have to admit, it's an impressive framework for building single-page applications. But here's the thing - how would one go about integrating Angular with a traditional website (maybe using codei ...
I have developed a click-to-show feature that opens and closes a div from left to right. You can view the demo here on jsfiddle.net In the demo, you will find a green-colored div. Upon clicking this div, another div opens from left to right. An interesti ...
My goal is to have my timer application reset to the original values specified in const initState when I press the reset button. However, currently when I hit the reset button, it just halts the timer without actually resetting the values. I've experi ...
I have a unique single application that requires the use of bootstrap, ui bootstrap, angularjs, and jquery. The functionality of the button below is working correctly, but I am experiencing issues with the styling. You can check out a sample of alerts here ...
Here is my HTML code: <i [class]="item.icon" [pTooltip]="item.item" tooltipPosition="bottom" ></i> The value inside item.item is 'ATTACHMENT' and I am unable to modify it from the ts file. Is there a way ...
As a beginner in angularJS, I am trying to display all customers from MySQL. Here is the code I have written in the controller and service: app.controller('CustomersController', function ($scope, customersService, $http) { init(); function ini ...
Currently facing a puzzling issue with an 'undefined' exception. Developing a tool to track distance traveled between two locations, leveraging Google Maps distance matrix. Upon selecting a vehicle, entering an origin and destination, and clickin ...
I apologize for my lack of experience in this matter. Can someone please guide me on how to utilize a loop to load images? For instance, could you show me how to rewrite the code below using a loop to automate the process? function loadImages() { let ...
Utilizing NestJS and Angular 2, I have noticed that both frameworks have a similar approach when it comes to working with Interceptors. I am currently looking for the best practice to identify specific requests in order to perform additional tasks. When d ...
Can anyone help me with this code snippet? <input name="GG" type="file" value="< ?php echo $data['image'] ?>"> I was trying to set a default value in the form edit, but it seems to not be working. Does anyone know how to set a defau ...
I've been searching and experimenting with various documents in order to make the $where clause work for me in MongoDB, but it just seems impossible. Here is the data object I'm working with: var UserProfiles = [{ userProfileID: "3f8c553c-3633- ...
I am in the process of developing an ePos system that allows users to add items to their basket by simply clicking on them, and then calculates the total price without needing to refresh the entire page. I initially attempted to use $_SESSION and store th ...
Currently, I am developing an application that makes requests for user profile photos from Microsoft Graph. The process seems to be working smoothly as the request returns a 200 status code and a significant amount of data is retrieved. However, when I att ...