Update the position of a div when an element becomes visible

Currently, I have 3 titles each with a button underneath to reveal more information about the subject.

To toggle the visibility of the content, I have implemented a script that shows or hides the corresponding div when the button is clicked. On smaller devices, the div appears below the subject, as intended, as shown here:

Example on small devices

However, on desktop devices where the subjects are displayed side by side, the shown div pushes the other subjects down, which is not the desired behavior.

I would like the displayed div to appear below all 3 subjects, rather than affecting the layout as shown here:

Incorrect display

Desired layout

Any suggestions on how to achieve this?

Additionally, is there a way to ensure that only one div is visible at a time? Meaning, when a new div is opened, the previous one should automatically close.

HTML:

 <!-- TOP 3 SERVICES -->
<div class="container">
    <div class="row">
        <div class="col-md-4 col-12 text-center mt-5">
            <img src="images/index/tijd.png" alt="time" class="mb-5 img-fluid">
            <h2>Always on time</h2>
            <button class="btn mybtn mt-2" onclick="showInfo('tijd')">See more</button>
        </div>

        <div class="service-detail mt-5 service-detail" id="tijd">
            <div class="col-12 col-md-8 offset-md-2 text-center">
                <h1 class="mt-5"><img src="images/index/tijd.png" alt="" class="mr-5">Always on time</h1>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
                    labore
                    et dolore magna aliqua consectetur adipiscing elit.
                    <br>
                    <br>
                    Duis aute irure dolor in reprehenderit in voluptate consectetur adipiscing elit.</p>
                <button class="btn mybtn mt-2 col-12 col-md-4" onclick="showInfo('tijd')">See less</button>
            </div>
        </div>

        <div class="col-md-4 col-12 text-center mt-5">
            <img src="images/index/trust.png" alt="trust" class="mb-5 img-fluid">
            <h2>Trust</h2>
            <button class="btn mybtn mt-2" onclick="showInfo('trust')">See more</button>
        </div>

        <div class="service-detail mt-5 service-detail" id="trust">
            <div class="col-12 col-md-8 offset-md-2 text-center">
                <h1 class="mt-5"><img src="images/index/trust.png" alt="" class="mr-5">Trust</h1>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
                    labore
                    et dolore magna aliqua consectetur adipiscing elit.
                    <br>
                    <br>
                    Duis aute irure dolor in reprehenderit in voluptate consectetur adipiscing elit.</p>
                <button class="btn mybtn mt-2 col-12 col-md-4" onclick="showInfo('trust')">See less</button>
            </div>
        </div>

        <div class="col-md-4 col-12 text-center mt-5 mb-5">
            <img src="images/index/24.png" alt="24/7" class="mb-5 img-fluid">
            <h2>24/7 support</h2>
            <button class="btn mybtn mt-2" onclick="showInfo('24')">See more</button>
        </div>
    </div>

    <div class="service-detail mt-5 service-detail" id="24">
        <div class="col-12 col-md-8 offset-md-2 text-center">
            <h1 class="pt-5"><img src="images/index/24.png" alt="" class="mr-5">24/7 support</h1>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
                labore
                et dolore magna aliqua consectetur adipiscing elit.
                <br>
                <br>
                Duis aute irure dolor in reprehenderit in voluptate consectetur adipiscing elit.</p>
            <button class="btn mybtn mt-2 col-12 col-md-4" onclick="showInfo('24')">See less</button>
        </div>
    </div>
</div>

JavaScript:

function showInfo(id) {
  var element = document.getElementById(id);
  if (element.style.display === "block") {
    element.style.display = "none";
  } else {
    element.style.display = "block";
  }
}

Answer №2

You are utilizing the bootstrap grid system improperly. The division within the div tags was not done correctly. I have now placed all instances of the "service detail" class within 'col-md-4'

Here is the corrected code:-

<div class="container">
<div class="row">
    <div class="col-md-4 col-12 text-center mt-5">
        <img src="images/index/tijd.png" alt="time" class="mb-5 img-fluid">
        <h2>Always on time</h2>
        <button class="btn mybtn mt-2" onclick="showInfo('tijd')">See more</button>
    </div>

    <div class="col-md-4">
    <div class="service-detail mt-5 service-detail" id="tijd">
        <div class="col-12 col-md-8 offset-md-2 text-center">
            <h1 class="mt-5"><img src="images/index/tijd.png" alt="" class="mr-5">Always on time</h1>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
                labore
                et dolore magna aliqua consectetur adipiscing elit.
                <br>
                <br>
                Duis aute irure dolor in reprehenderit in voluptate consectetur adipiscing elit.</p>
            <button class="btn mybtn mt-2 col-12 col-md-4" onclick="showInfo('tijd')">See less</button>
        </div>
    </div>
</div>
    <div class="col-md-4 col-12 text-center mt-5">
        <img src="images/index/trust.png" alt="trust" class="mb-5 img-fluid">
        <h2>Trust</h2>
        <button class="btn mybtn mt-2" onclick="showInfo('trust')">See more</button>
    </div>
    <div class="col-md-4">
    <div class="service-detail mt-5 service-detail" id="trust">
        <div class="col-12 col-md-8 offset-md-2 text-center">
            <h1 class="mt-5"><img src="images/index/trust.png" alt="" class="mr-5">Trust</h1>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
                labore
                et dolore magna aliqua consectetur adipiscing elit.
                <br>
                <br>
                Duis aute irure dolor in reprehenderit in voluptate consectetur adipiscing elit.</p>
            <button class="btn mybtn mt-2 col-12 col-md-4" onclick="showInfo('trust')">See less</button>
        </div>
    </div>
    </div>
    <div class="col-md-4 col-12 text-center mt-5 mb-5">
        <img src="images/index/24.png" alt="24/7" class="mb-5 img-fluid">
        <h2>24/7 support</h2>
        <button class="btn mybtn mt-2" onclick="showInfo('24')">See more</button>
    </div>
</div>
<div class="col-md-4">
<div class="service-detail mt-5 service-detail" id="24">
    <div class="col-12 col-md-8 offset-md-2 text-center">
        <h1 class="pt-5"><img src="images/index/24.png" alt="" class="mr-5">24/7 support</h1>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
            labore
            et dolore magna aliqua consectetur adipiscing elit.
            <br>
            <br>
            Duis aute irure dolor in reprehenderit in voluptate consectetur adipiscing elit.</p>
        <button class="btn mybtn mt-2 col-12 col-md-4" onclick="showInfo('24')">See less</button>
    </div>
</div>
</div>

Answer №3

If you're looking to create a visually appealing and functional layout without the need for any additional JavaScript, then the following code is what you need. It makes use of native Bootstrap 4 classes to achieve the desired result. There might be a few minor adjustments required, but overall, it should meet your expectations:

<!-- TOP 3 SERVICES -->
<div class="container">
    <div class="row" id="myAccordion" data-children=".item">
        <div class="col-12 col-md-4 text-center mt-5 order-1 order-md-1">
            <img src="images/index/tijd.png" alt="time" class="mb-5 img-fluid">
            <h2>Always on time</h2>
            <button class="btn mybtn mt-2 mb-2 mb-md-5" data-toggle="collapse" data-parent="#myAccordion" data-target="#panel1" aria-expanded="false" aria-controls="panel1">See more</button>
        </div><!-- col -->

        <div class="item order-2 order-md-4"><!-- order-2 moves this div to 2nd position on small screens i.e. underneath the previous -->
            <div class="col-12 col-md-8 offset-md-2 text-center collapse service-detail mt-5" id="panel1"><!-- for toggle functionality this column needs to be wrapped in a div with the .item class -->
                <h1 class="mt-5"><img src="images/index/tijd.png" alt="" class="mr-5">Always on time</h1>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua consectetur adipiscing elit.
                    <br>
                    <br> Duis aute irure dolor in reprehenderit in voluptate consectetur adipiscing elit.</p>
                <!--                <button class="btn mybtn mt-2 col-12 col-md-4" onclick="showInfo('tijd')">See less</button>-->
            </div><!-- col -->
        </div><!-- item -->

        <div class="col-12 col-md-4 text-center mt-5 mt-md-5 order-3 order-md-2"><!-- order-3 is what makes it show in the desired order on smaller screens; order-md-2 means it's gonna come in second place on medium screens -->
            <img src="images/index/trust.png" alt="trust" class="mb-5 img-fluid">
            <h2>Trust</h2>
            <button class="btn mybtn mt-2 mb-2 mb-md-5" data-toggle="collapse" data-parent="#myAccordion" data-target="#panel2" aria-expanded="false" aria-controls="panel2">See more</button>
        </div><!-- col -->

        <div class="item order-4 order-md-5"><!-- order-4 moves this div to 4th position on small screens i.e. underneath the previous -->
            <div class="col-12 col-md-8 offset-md-2 text-center collapse service-detail mt-5" id="panel2"><!-- for toggle functionality this column needs to be wrapped in a div with the .item class -->
                <h1 class="mt-5"><img src="images/index/trust.png" alt="" class="mr-5">Trust</h1>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua consectetur adipiscing elit.
                    <br>
                    <br> Duis aute irure dolor in reprehenderit in voluptate consectetur adipiscing elit.</p>
                <!--                <button class="btn mybtn mt-2 col-12 col-md-4" onclick="showInfo('trust')">See less</button>-->
            </div><!-- col -->
        </div><!-- item -->

        <div class="col-12 col-md-4 text-center mt-5 order-5 order-md-3"><!-- order-5 is what makes it show in the desired order on smaller screens; order-md-3 means it's gonna come in 3rd place on medium screens -->
            <img src="images/index/24.png" alt="24/7" class="mb-5 img-fluid">
            <h2>24/7 support</h2>
            <button class="btn mybtn mt-2 mb-2 mb-md-5" data-toggle="collapse" data-parent="#myAccordion" data-target="#panel3" aria-expanded="false" aria-controls="panel3">See more</button>
        </div><!-- col -->

        <div class="item order-6"><!-- order-6 is still necessary here because otherwise this div would show on top of all others -->
            <div class="col-12 col-md-8 offset-md-2 text-center collapse service-detail mt-5" id="panel3"><!-- for toggle functionality this column needs to be wrapped in a div with the .item class -->
                <h1 class="pt-5"><img src="images/index/24.png" alt="" class="mr-5">24/7 support</h1>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua consectetur adipiscing elit.
                    <br>
                    <br> Duis aute irure dolor in reprehenderit in voluptate consectetur adipiscing elit.</p>
                <!--                <button class="btn mybtn mt-2 col-12 col-md-4" onclick="showInfo('24')">See less</button>-->
            </div><!-- col -->
        </div><!-- item -->

    </div><!-- .row #myAccordion -->
</div><!-- container -->

If you wish to further customize this layout, refer to the documentation available at this link:

https://getbootstrap.com/docs/4.0/components/collapse/

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

How to create a calendar selection input using PHP?

Before I start writing the code myself, I am searching to see if there is already a solution available or if someone has previously outsourced the code. Below is an example of my date selection: https://i.sstatic.net/KqIDH.png I want to create a feature ...

Numerous instances of Codemirror

I have the ability to generate and exhibit multiple dynamic codemirror instances, however, I am having trouble referencing them using the code snippet below. I suspect that the problem lies in creating a dynamic function name (not entirely sure how to ac ...

The event listener for the custom cursor in Nuxt.js is failing to work properly when the route

I am currently in the process of constructing a new website for our studio, but am encountering difficulties with getting the custom cursor to function correctly. I implemented a custom cursor using gsap, and it worked perfectly; however, once I navigate t ...

When the enter key is pressed in the input box, the page resets instead of triggering

After extensive research, I have found that none of the available resources have been able to resolve the specific issue I am facing. Objective: My goal is to trigger the function $("#search").click(); when text is entered into the box and th ...

Challenges with border-color and CSS input:focus

The dilemma Currently, I am immersed in a project focusing on constructing a front end using bootstrap4. In order to align with the company's main color scheme, I decided to alter the highlighting color of inputs. To achieve this, I made the followi ...

Identifying various device platforms through CSS styling

After extensive research and testing, I have explored a variety of resources and solutions for implementing device-specific CSS on my webpage. Here are some of the references I've studied: Detect iPhone/iPad purely by css Detect Xoom browser (Andro ...

Using a ThreeJS bitmap texture to extrude a shape created with THREE.Shape

I have successfully created a cube with rounded corners using the THREE.Shape object: var shape = new THREE.Shape(); x = width/2; y = height/2; shape.moveTo(x - radius, y); //*** Top right x = -width/2; y = height/2; shape.lineTo(x + radius, y); if (tr) ...

Querying the api for data using Angular when paginating the table

Currently, I have a table that retrieves data from an API URL, and the data is paginated by default on the server. My goal is to fetch new data when clicking on pages 2, 3, etc., returning the corresponding page's data from the server. I am using an ...

JavaScript fails to effectively validate URLs

I have implemented the following validation for URLs: jQuery.validator.addMethod("urlValidatorJS", function (value, element) { var regExp = (/^HTTP|HTTP|http(s)?:\/\/(www\.)?[A-Za-z0-9]+([\-\.]{1}[A-Za-z0-9]+)*\.[A-Za-z]{ ...

What is the most effective method for displaying error messages in Extjs?

I am currently using the store.synch() method to post data, with validation being done on the server side. I am currently displaying error messages in a message box, but I want to explore alternative ways to show errors without having to use markInvalid( ...

Choosing both large font and low vision CSS stylesheets concurrently

I'm currently working on designing one of my initial websites and have encountered a dilemma. I have three different stylesheets - one for low vision, one for large font, and the default one. I have three buttons to select these stylesheets, but I&apo ...

What is the best way to utilize the `Headers` iterator within a web browser?

Currently, I am attempting to utilize the Headers iterator as per the guidelines outlined in the Iterator documentation. let done = false while ( ! done ) { let result = headers.entries() if ( result.value ) { console.log(`yaay`) } ...

Docusaurus font loading specifically optimized for body text, excluding headings

I've added the following CSS code to my Docusaurus custom stylesheet: @import url("https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,400;0,500;0,600;0,700;1,400;1,500;1,600;1,700&display=swap"); :root { --ifm-color- ...

Attempting to align images in Bootstrap image carousel within a Google Sites webpage

Click here for image of issue I am currently working on adjusting the code to ensure that all six images in this Bootstrap Carousel are centered on my updated Google Sites webpage, as opposed to them appearing at the top like shown in the screenshot. Any ...

Minimize all expanded containers in React

Although I've come across similar questions, none of them have addressed mine directly. I managed to create a collapsible div component that expands itself upon click. However, I am looking for a way to make it so that when one div is expanded, all o ...

The importance of adding blank spaces in email formatting

A Visual Basic 6 application is sending a HTML email. The issue is that when the email is received, all blank spaces are removed. For example, in the code snippet below, the email sent says "this is a test" instead of displaying the actual spaces on line ...

Calculating the total sum of values using a dropdown list as a filter with J

I have successfully implemented a dropdown filter in my jQuery datatable to filter data. However, I am now looking to calculate the sum of all values in the filtered table each time a user selects a value from the dropdown list. How can I achieve this? My ...

The Zip file generated in memory is experiencing corruption

I'm encountering an issue while attempting to serve a zip file generated in memory by Flask to a JavaScript front-end. However, the downloaded file appears corrupted and I am unsure of what mistake I may be making. @app.route('/route') def ...

Using jQuery to pass UI positions as percentages instead of screen positions

Currently, I have implemented the following code to send $_GET values to a PHP page: $.get('/includes/sticky_notes/update_position.php',{ x : ui.position.left, y : ui.position.top, z : zIndex, id : parseInt(ui. ...

Can the keys of an object be retrieved in a specific order?

Is it possible to retrieve keys from a JSON object in the exact same order they were originally received? The use of Object.keys(company).length currently seems to be functioning, but I am seeking reassurance that this method will consistently deliver acc ...