What is the best way to conceal Bootstrap accordion content when first loading my single-page application?

I am currently developing a single page application for an ESP32 device. I have chosen to keep all the content in one document as there is not a lot of information to display.

My approach started with using the collapse class from Bootstrap 4.1.2. While it is functional, there seems to be an issue that I cannot seem to resolve.

Since only one content section should be visible at a time, I want to prevent users from hiding it by clicking on the corresponding nav-link. Instead, I want them to only toggle between different content sections using the different nav-links available.

How can I achieve this functionality?

<!doctype html>
<html lang="en">

<head>
    <title>LOGO</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
</head>
<!-- A vertical navbar -->

<body>
    <nav class="navbar navbar-expand-md navbar-dark bg-primary">
        <div class="container justify-content-center">
            <a class="navbar-brand abs " href="#">
                <span class="menu-collapsed">LOGO</span>
            </a>
            <button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#navbarNavDropdown">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse flex-grow text-center" id="navbarNavDropdown">
                <ul class="nav navbar-nav flex-fill mx-auto flex-nowrap">
                    <li class="nav-item">

                        <a class="nav-link" data-toggle="collapse" data-target="#collapseOne, .navbar-collapse.show"
                            aria-expanded="true" aria-controls="collapseOne" href="#">TEST1</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" data-toggle="collapse" data-target="#collapseTwo, .navbar-collapse.show"
                            aria-expanded="false" aria-controls="collapseTwo" href="#">TEST2</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" role="button" data-toggle="collapse" data-target="#collapseThree, .navbar-collapse.show"
                            aria-expanded="false" aria-controls="collapseThree" href="#">TEST3</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">
                            <i class="fa fa-cog"></i>
                            <span>Settings</span>
                        </a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container" id="accordionExample">
        <div class="row">
            <div id="collapseOne" class="collapse" aria-labelledby="headingOne" data-parent="#accordionExample">
                <div class="card-body active">
                    1111111111111111111111111111111111111
                </div>
            </div>
        </div>
        <div class="row">
            <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
                <div class="card-body">
                    222222222222222222222222222222222222
                </div>
            </div>
        </div>
        <div class="row">
            <div id="collapseThree" class="collapse" aria-labelledby="headingThree" data-parent="#accordionExample">
                <div class="card-body">
                    333333333333333333333333333333333333
                </div>
            </div>
        </div>

</body>

</html>

Answer №1

Try removing the 'show' class from #collapseOne - that should fix your problem...

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

<meta charset="utf-8>
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <!-- Bootstrap CSS -->
        <link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="./css/fontawesome.css" rel="stylesheet">

        <nav class="navbar navbar-expand-md navbar-dark bg-primary">
            <div class="container justify-content-center">
                <a class="navbar-brand abs " href="#">
                    <span class="menu-collapsed">LOGO</span>
                </a>
                <button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#navbarNavDropdown">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse flex-grow text-center" id="navbarNavDropdown">
                    <ul class="nav navbar-nav flex-fill mx-auto flex-nowrap">
                        <li class="nav-item">
                            <a class="nav-link" data-toggle="collapse" data-target="#collapseOne, .navbar-collapse.show" aria-expanded="true"
                                aria-controls="collapseOne" href="#">TEST1</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" data-toggle="collapse" data-target="#collapseTwo, .navbar-collapse.show" aria-expanded="false"
                                aria-controls="collapseTwo" href="#">TEST2</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link"  role="button" data-toggle="collapse" data-target="#collapseThree, .navbar-collapse.show" aria-expanded="false"
                                aria-controls="collapseThree" href="#">TEST3</a>
                        </li>
                         <li class="nav-item">
                            <a class="nav-link" href="#">
                                <i class="fa fa-cog"></i>
                                <span>Settings</span>
                            </a>
                        </li>
                    </ul>
                </div>
            </div>

        </nav>

        <div class="container" id="accordionExample">
            <div class="row">
                <div id="collapseOne" class="collapse" aria-labelledby="headingOne" data-parent="#accordionExample">
                    <div class="card-body active">
                       1111111111111111111111111111111111111
                    </div>
                </div>
            </div>
            <div class="row">
                <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
                    <div class="card-body">
                        222222222222222222222222222222222222
                    </div>
                </div>
            </div>
            <div class="row">
                <div id="collapseThree" class="collapse" aria-labelledby="headingThree" data-parent="#accordionExample">
                    <div class="card-body">
                        333333333333333333333333333333333333
                    </div>
                </div>
            </div>


            <script src="./js/jquery-3.3.1.slim.min.js" crossorigin="anonymous"></script>
            <script src="./js/popper.min.js" crossorigin="anonymous"></script>
            <script src="./js/bootstrap.min.js" crossorigin="anonymous"></script>
            <script src="./js/fontawesome.js" crossorigin="anonymous"></script>

Answer №2

Everything is up and running smoothly now, thanks to utilizing pills with the data-toggle="tab" feature. The only issue I've encountered is that the collapsed menu isn't aligning all items vertically as seen in the initial post. It's just a minor design flaw that I'll address. Appreciate the assistance :)

<!doctype html>
<html lang="en">

<head>
    <title>LOGO</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
</head>
<!-- A vertical navbar -->

<body>

    <nav class="navbar navbar-expand-md navbar-dark bg-primary">
        <div class="container justify-content-center">
            <a class="navbar-brand abs " href="#">
                <span class="menu-collapsed">LOGO</span>
            </a>
            <button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#navbarNavDropdown">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse flex-grow text-center" id="navbarNavDropdown">

                <div class="nav nav-pills  justify-content-center bg-primary" id="pills-tab" role="navigation" style="margin-top:2.5%;">

                    <a class="nav-item nav-link text-light active " id="pills-home-tab" data-toggle="tab" href="#pills-home"
                        role="tab" aria-controls="pills-home" aria-selected="true">
                        <p class="div" data-toggle="collapse" data-target=".navbar-collapse.show" aria-expanded="false"
                            aria-controls="collapseThree" href="#">Test1
                        </p>
                    </a>
                    <a class="nav-item nav-link text-light" id="pills-profile-tab" data-toggle="tab" href="#pills-profile"
                        role="tab" aria-controls="pills-profile" aria-selected="false">
                        <p class="div" data-toggle="collapse" data-target=".navbar-collapse.show" aria-expanded="false"
                            aria-controls="collapseThree" href="#">Test2
                        </p>
                    </a>
                    <a class="nav-item nav-link text-light" id="pillsv-contact-tab" data-toggle="tab" href="#pills-contact"
                        role="tab" aria-controls="pills-contact" aria-selected="false">
                        <p class="div" data-toggle="collapse" data-target=".navbar-collapse.show" aria-expanded="false"
                            aria-controls="collapseThree" href="#">Test3
                        </p>
                    </a>
                </div>
            </div>
    </nav>

    <div class="tab-content" id="pills-tabContent">
        <div class="tab-pane active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">
            111111111111111111111111111111111111111111
        </div>
        <div class="tab-pane" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab">
            222222222222222222222222222222222222222222
        </div>
        <div class="tab-pane" id="pills-contact" role="tabpanel" aria-labelledby="pills-contact-tab">
            333333333333333333333333333333333333333333
        </div>
    </div>

    <footer class="fixed-bottom">
        Facebook, Twitter icons etc.
    </footer>

</body>

</html>

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

Turn off a feature on older buttons

For my project, I am looking to dynamically add specific elements like buttons. However, every time a new button is added, the function call for the old button gets duplicated. var btnElem ='<button type="button"class="doSomething">Button< ...

Troubleshooting the Problem with CSS Margin in HTML Footer

I'm encountering an issue with the footer on my website. The margin: auto; command doesn't seem to be working for the list items in the footer. I want the items in the footer to occupy one-third of the width, but no matter where I place the marg ...

Accordion menu with smooth CSS3 transitions

I created a unique accordion menu to replace the select form control, and I am interested in using CSS3 transitions to give it a smooth expand and contract effect. Feel free to check out my work on jsfiddle: http://jsfiddle.net/hKsCD/4/ In order to achi ...

Troubleshooting problem with CSS overflow and absolute positioning on Android Browser

Our mobile web app was created using a combination of JQuery Mobile, PhoneGap, and ASP.net MVC. It is designed to function smoothly on both iOS and Android devices regardless of the browser being used. We have conducted tests on various devices and everyth ...

Is it possible to continuously increase the value of a CSS property with each click?

I am trying to implement a feature where the value of an element decreases each time a different element is clicked. Currently, I have the following code: $("#trigger_heritage").click(function () { $(".heritage_content ul").css("margin-left", + -880); ...

Ensure each list item is directly aligned when swiping on a mobile device

Looking to make a simple horizontal list swipeable on mobile devices with tabs that snap from tab to tab, activating the content for the active tab immediately. The desired effect is to swipe directly from one tab to the next without having to click separ ...

What is the method for opening the image gallery in a Progressive Web App (PWA)?

I am struggling to figure out how to access the image gallery from a Progressive Web App (PWA). The PWA allows me to take pictures and upload them on a desktop, but when it comes to a mobile device, I can only access the camera to take photos. I have tried ...

Select a hyperlink to access the corresponding tab

I've been playing around with bootstrap and AngularJS. I placed nav-tabs inside a modal-window and have it open when clicking on a link. However, I want the appropriate tab to open directly upon click. For example, if I click on "travel", I want the t ...

Is there a translation issue affecting Chrome version 44?

Recently, Chrome 44 (44.0.2403.89 m) was released and I've encountered some issues with the translate3d feature (on both Mac and Windows versions). This problem is impacting plugins such as fullPage.js and consequently affecting numerous pages at th ...

Building a Responsive Page with Bootstrap 4

I'm currently working on a design page with 2 boxes on the left side and 3 boxes on the right. It looks great on desktop, but I want the boxes to display individually on mobile devices and I'm having trouble figuring it out. Here is my code: < ...

Dynamic fields added using JavaScript are not recognized by PHP

I have a MySQL database where orders are stored along with various activities. My PHP/HTML page retrieves these activities when an order is clicked and allows users to modify them using a form. Upon submission, another PHP file updates the database by loop ...

Issues with CSS compatibility have been identified on Internet Explorer version 7

Currently, I am in the process of working on a website. You can access it through this link. The good news is that the site functions well on FireFox, Chrome, and IE 8. However, the bad news is that it does not display properly on IE 7. There are alignme ...

Unfold an HTML element and reveal a sneak peek of its content with the aid of JQuery

I need a way to organize and display a set of 5 lines of text vertically. Specifically, I want them arranged like this: 1) Line one 2) Line two 3) Line three 4) Line four 5) Line five However, I have a special requirement where only the first ...

Angular 6: Issue with displaying data on the user interface

Hello! I am attempting to fetch and display a single data entry by ID from an API. Here is the current setup: API GET Method: app.get('/movies/:id', (req, res) => { const id = req.params.id; request('https://api.themoviedb.org/ ...

The color of the button in HTML5 will remain constant and not shift

I have several buttons that function like radio buttons - only one can be selected at a time: <button id="btn-bronze" name="btn-bronze" type="button" class="blue-selected">Bronze</button> <button id="btn-silver" name="btn-silver" type="butt ...

Utilizing the map function to display elements from a sub array

I'm struggling to print out the comments using the map function in this code: class Postcomment2 extends React.Component { uploadcomment = () => { return this.props.post.comments.map((comment) => { return <div>{comment["comment"]}< ...

Is feature recognition possible in a jQuery plugin?

Can I integrate a tool similar to Modernizr into my plugin? I want to be able to test for specific CSS3 properties without starting from scratch. ...

I encountered an issue where I was unable to customize the styling of the

I'm facing an issue where I cannot override the style using `className` on a `Button`. Interestingly, the same style works correctly on a `TextField`. However, when I use the `style` prop, it allows me to successfully override the `Button` style. What ...

Displaying HTML elements conditionally in React depending on boolean values

Developing a component that limits the display of text to the first 40 characters, but can show the full description when a Chevron click event is triggered. The goal is to have the content expand in a similar fashion as it currently does with the Accord ...

What is the process for allowing right-click to open in a new tab, while left-clicking redirects to a new page in the current tab?

In my project, I have implemented a multi-page form for users to input information. The left side menu contains items that allow users to switch between different pages while filling out the form. Currently, clicking on any of these items redirects the use ...