Achieving a full-height accordion with each pane taking up the entirety of the viewport using Bootstrap 4

https://i.sstatic.net/LqhXn.png I found this image which perfectly explains what I'm trying to achieve.

This is my current code:

<div class="accordion" id="myAccordion">
          <div class="card">
            <div class="card-header" id="section1" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
              <h2 class="mb-0">
                <i class="fa fa-search" aria-hidden="true"></i> Section 1
              </h2>
            </div>
        
            <div id="collapseOne" class="collapse" aria-labelledby="section1" data-parent="#myAccordion">
              <div class="card-body">
                Some content here
              </div>
            </div>
          </div>

          <div class="card">
            <div class="card-header" id="section2" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
              <h2 class="mb-0">
                 <i class="fa fa-briefcase" aria-hidden="true"></i> Section 2
              </h2>
            </div>
            <div id="collapseTwo" class="collapse" aria-labelledby="section2" data-parent="#myAccordion">
              <div class="card-body">
                some other content here
              </div>
            </div>
          </div>
            </div>
          </div>
        </div>

I have checked similar questions but none have provided a solution. I want the card-body of section1 to take full height based on the user's viewpoint, regardless of the content inside it, and push section2's card-header all the way to the bottom of the accordion. Any help or further references would be appreciated.

[This is the current result ->] https://i.sstatic.net/THCb9.png

Answer №1

One innovative approach is to dynamically adjust the height of an accordion based on the viewport size. By setting the accordion to 100% viewport height and using flexbox in a column direction, the opened section can expand vertically with flex-grow:1;.

This technique eliminates the need to hard code specific heights, providing a more flexible design.

An ideal scenario would involve Pure CSS if Bootstrap allowed for additional customization options within the collapse plugin. Currently, when toggling the collapsible sections, only the .show class is applied to the specific collapsible element rather than its parent .card.


To address this limitation, JavaScript utilizing Bootstrap Collapse Events can be implemented:

$(function() {
    $('.accordion-vh-100 .collapse').on('show.bs.collapse', function () {
        $(this).parents('.card').addClass('show');
    });
    
    $('.accordion-vh-100 .collapse').on('hide.bs.collapse', function () {
        $(this).parents('.card').removeClass('show');
    });
});

By adding or removing the .show class on the parent .card element, it becomes possible to identify which card is currently open.

A new CSS class named .accordion-vh-100 is introduced to enable the accordion to occupy 100% of the viewport height. Additionally, the structure of .card-header is modified to an anchor tag for improved usability:

<div class="accordion accordion-vh-100" id="myAccordion">
    <div class="card"&g...

To style the new .accordion-vh-100 class accordingly, the following CSS rules are applied:

.accordion-vh-100 {
    height: 100vh;
    display: flex;
    flex-flow: column nowrap;
}

Finally, the expanded .card is set to grow proportionally by targeting the .show class within the .accordion-vh-100:

.accordion-vh-100 .card.show {
    flex-grow: 1;
}

View the demo here: https://jsfiddle.net/davidliang2008/b1duyLs8/24/

Answer №2

Take a look at this code snippet:

    <div class="accordion" id="myAccordion">
        <div class="card">
            <div class="card-header" id="section1" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                <h2 class="mb-0">
                    <i class="fa fa-search" aria-hidden="true"></i> Section 1
                </h2>
            </div>

            <div id="collapseOne" class="collapse" aria-labelledby="section1" data-parent="#myAccordion">
                <div class="card-body" style="height: 85vh;">
                    Some content here
                </div>
            </div>
        </div>

        <div class="card">
            <div class="card-header" id="section2" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                <h2 class="mb-0">
                    <i class="fa fa-briefcase" aria-hidden="true"></i> Section 2
                </h2>
            </div>
            <div id="collapseTwo" class="collapse" aria-labelledby="section2" data-parent="#myAccordion">
                <div class="card-body" style="height: 85vh;">
                    some other content here
                </div>
            </div>
        </div>
    </div>

We have utilized style="height: 85vh;" on

<div class="card-body">
to increase its height to 85% of the viewport height.

If you plan to add more cards, you may need to reduce this height in order to display additional card headers when one of the cards is expanded.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Access and expand a bootstrap accordion with a hyperlink

Here is a concept I want to make happen: For my project, I am utilizing Bootstrap v3.4.1 and have set up two columns next to each other. My goal is to link the elements in the hotspot banner (left column) to trigger and open the corresponding Accordions ( ...

What is the best way to conceal a row when a particular field is devoid of content?

Is it possible to hide an entire table row if a field is empty? For example: https://i.sstatic.net/tMW7L.png If a certain field is empty, I want the entire row to be hidden, either using JavaScript or jQuery. I have tried some methods but none of them fu ...

Can we incorporate the radix-ui/colors variables into a CSS module file?

For my personal project, I am incorporating Radix components alongside radix-ui/colors. However, when attempting to import color variables into the CSS Module, I encountered an error. It seems that using the :root selector in the _app file is necessary, as ...

Styling a parent element when it is in focus using CSS3

Is there a way to apply a style to the parent element when an input is in focus? input:focus { background-color:yellow; } For example, if we have the following HTML: <div class="foo"> <input type="text /> Hello </div> I ...

Issue encountered when attempting to remove items from Redux store with onClick event

My goal is to delete a specific object from an array in my store. I have a delete item function that successfully removes objects, but I am struggling to make it work with a button that is rendered with each object using map. Here is my component: import ...

How can I apply an underline effect when hovering over navigation links in a responsive menu?

I've successfully added an underline effect on hover for the navigation menu. However, I encountered an issue with the responsiveness of the menu. When viewed on screens smaller than 600px, the underline effect covers the entire width of the block ins ...

Utilize the cakePHP HTML input helper to assign specific values from 2 arrays

I am working on creating 2 arrays from a model for an edit page. One array contains values already selected by the user, while the second array contains available values that the user can choose from. My goal is to populate a multi-select input box with ...

Can a text file be loaded into the title of a Bootstrap tooltip?

Looking to dynamically load a text file into bootstrap tooltip titles. Here is a snippet of working code with hardcoded values: <div> <a id="A" type="button" class="btn btn-outline-secondary btn-lg" data-toggle=& ...

Issue with custom checkbox not changing when bootstrap collapse occurs

I've been searching for a solution and experimenting with different methods, but I just can't seem to make it work. Below is a CodePen example that showcases a custom checkbox with a toggle function to show/hide a div when checked, along with a ...

I am facing difficulty creating a dropdown menu with nested options that only appear when the user hovers over the main selection

Here are the parent options I have and the code I have attempted to build. I would like to include sub-options such as phones, radios, speakers under the electronics parent option and many more. <select id="category" name="category" class="dropDown"& ...

Dropdown menu similar to the one utilized in Bootstrap

I am curious to understand how the drop-down menu system in Bootstrap3's tutorial page operates. It seems that it collapses all subtopics under a main topic by default, allowing users to either click or scroll to expand further. Additionally, the menu ...

The chosen options will automatically populate the text-boxes that require dynamic summation

I am encountering an issue with a select option that should dynamically populate specific values in text-boxes (different prices) based on the selected option. The goal is to sum up the values in the text-boxes and display the total in another text-box. Ho ...

What could be causing the arrows on my card carousel to malfunction?

I'm facing some issues with my card carousel functionality. I'm in the process of learning JavaScript and I believe that's where the problem lies, but I'm unsure how to resolve it. Every time I click on the button/arrow for the carousel ...

Having trouble with the dropdown multiselect feature in AngularJS?

I'm striving to develop a straightforward multi-select dropdown utilizing angular JS, bootstrap, and JS. This dropdown should display days (mon, tue...sun), with options for select all and unselect all. My goal is to create a controller that will de- ...

Combining Bootstrap Vue: utilizing class names alongside HTML tags

(Bootstrap-Vue 2.0, Vue.js 2.5) Is it possible to combine traditional CSS Bootstrap 4 classes with Bootstrap-Vue? For example, can I use the following code snippet: <section id="introduction"> <b-container class="h-100"> & ...

Changing a button to text with PHP upon clicking

I am looking to create a button that, upon being clicked, will show a simple "X" on the website in the same location as the button itself. Essentially, the button will vanish and be replaced by an "X". The current code for my button is straightforward: &l ...

What are the best practices for preloading images, JavaScript, and CSS files?

Back in the late 90's, I was really passionate about creating websites from scratch. However, as I dove back into web development recently, I realized how much the landscape has changed since then. As more of a designer than a developer, I opted to us ...

Issue with Clicking Mouse to Display Content not Operational

I need a way to make a div element hidden until the user clicks on it, at which point it should be displayed. HTML <div class="coupon-wrapper"> <a href="/{{ i.couponStoreURL }}/#{{ i.id }}"> <span id="couponCode" style="">{{ ...

Initial button click fails to trigger onclick event

After researching and reading various articles on the issue, I have tried several solutions but nothing seems to work. The problem occurs when the button is clicked for the first time - nothing happens. It is only after clicking the button a second time th ...

"Utilizing the React library to implement an HTML component with a styled radio input that

My radio button issue: I have two radio buttons, one is checked by default. When trying to switch the selection by clicking on the unchecked option, I have to click twice. How can I resolve this problem? <label>YES</label> <input type={&a ...