Tips for keeping a div stationary when an adjacent div expands

I am facing an issue with 3 blocks in a row - a large block, a smaller block that can expand, and a third block placed under the first block. When the smaller block expands, the top of the third block moves to match the bottom of the expanding div. However, I need the third block to stay at the bottom of the first block.

I have tried different configurations using Bootstrap columns, but I am struggling to maintain the desired layout on mobile. I have also explored using card-columns but faced limitations in customizing the width and order of appearance on mobile devices.

To illustrate the problem, I have created a fiddle: https://jsfiddle.net/k53rnvpb/1/

$('.expand').click(function() {
  $('.block-2').toggleClass('height-300');
})
.row {
  background: #f8f9fa;
  margin-top: 20px;
}

[class^="col-"] {
  border: solid 1px #6c757d;
  padding: 10px;
}

.block-1 {
  height: 200px;
}

.block-2 {
  height: 150px;
}

.block-3 {
  height: 200px;
}

.height-300 {
  height: 300px;
}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>

<div class="container">
  <div class="row">
    <div class="col-12 col-sm-8 block-1">
      Block 1
    </div>
    <div class="col-12 col-sm-4 block-2">
      Block 2
      <p>I need to be between block 1 and 3 on mobile</p>
      <button class='btn btn-primary expand'>Expand</button>
    </div>
    <div class="col-12 col-sm-8 block-3">
      Block 3
      <p>If block 2 expands i still need to be attached to block 1</p>
    </div>
  </div>
</div>

I am seeking a solution to prevent the bottom block from moving when the other block expands or to find a clean way to position the expanding block between the other two blocks on mobile devices.

I hope the issue is clear, and I appreciate any assistance.

Answer №1

There are a couple of methods to achieve this. One option is to duplicate the div content, but that may not be the most efficient solution. Another solution involves using media queries. It's important to note that while you have given a border to the column, I have applied a border to the content div.

This approach addresses two key issues: 1. Expanding the div will not affect its height. 2. Block 2 will remain centered between block 1 and block 3.

$('.expand').click(function() {
  $('.block-2').toggleClass('height-300');
})
.row {
  background: #f8f9fa;
  margin-top: 20px;
}

[class^="col-"] {
  border: solid 1px #6c757d;
  padding: 10px;
}

.block-1 {
  height: 200px;
  clear: both;
}

.block-2 {
  height: 150px;
  position: absolute;
  border: solid 1px red;
}

@media only screen and (max-width: 767px) {
  .block-2 {
    position: relative;
  }
}

.block-3 {
  height: 200px;
}

.height-300 {
  height: 300px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="row">
    <div class="col-12 col-md-8 block-1">
      Block 1
    </div>
    <div class="col-12 col-md-4" style="padding: 0;">
      <div class=" block-2">
        Block 2
        <p>I need to be between block 1 and 3 on mobile</p>
        <button class='btn btn-primary expand'>Expand</button>
      </div>
    </div>
    <div class="col-12 col-md-8 block-3">
      Block 3
      <p>If block 2 expands I still need to be attached to block 1</p>
    </div>
  </div>
</div>

https://jsfiddle.net/g21eqyxn/11/

Answer №2

I made a tweak by adding a nested <div class="expander"> within the second expanding DIV and adjusted the height expansion to this inner div. I trust it now functions as per your requirement.

Towards the end of the css, a media query has been included. Ensure to tailor it accurately to suit your small screen resolution.

Kindly note that this may not work seamlessly in the snippet as bootstrap is not integrated. Please test it in your own code.

$('.expand').click(function(){
$('.expander').toggleClass('height-300');
})
.row {
  background: #f8f9fa;
  margin-top: 20px;
}

[class^="col-"],
.expander{
  border: solid 1px #6c757d;
  padding: 10px;
}
.block-1{
    height: 200px;
}
.block-2{
    height: 150px;
    border: none;
    padding: 0;
}
.block-3{
    height:200px;
}
.height-300{
    height: 300px;
}

/*Fine tune this media query based on your desired mobile resolution*/
@media screen and (max-width: 600px){
  .block-2{
    height: auto;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
    <div class="row">
        <div class="col-12 col-md-8 block-1">
            Block 1
        </div>
        <div class="col-12 col-md-4 block-2">
          <div class="expander">
          Block 2
            <p>I need to be between block 1 and 3 on mobile</p>
            <button class='btn btn-primary expand'>Expand</button>
          </div>
        </div>
        <div class="col-12 col-md-8 block-3">
            Block 3
            <p>If block 2 expands i still need to be attached to block 1</p>
        </div>
    </div>
</div>

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

Output information to the specified divID using Response.Write

Currently, I have knowledge of javascript and I am currently expanding my skills in ASP.NET C#. My goal is to achieve the following task using javascript: document.getElementById('divID-1').innerHTML= '<p>Wrong Password< ...

Injecting script into a webpage and initiating an ajax request prior to receiving a response

Trying to accomplish something a bit complex that I believe is feasible, or perhaps someone could offer a suggestion on how to achieve it. At website A, there is a database containing booking data. At website B, the goal is to display information about w ...

The initial row's Id will be used as the Id for all subsequent rows within a JSON object

After dragging a tr in a table and confirming by clicking a button, I need the order list to be updated in the database. To achieve this, I created a JSON object where I intend to save the ids and their corresponding new orders. The issue I am facing is t ...

Transfer PHP's uniqid() function to real-time using JavaScript with jQuery

Seeking a compact JavaScript(jQuery) code snippet to achieve this: date("r",hexdec(substr(uniqid(),0,8))); Your assistance is highly appreciated. Thank you. ...

What is causing my Javascript media query for the sidebar to not function properly?

I am working on implementing a sidebar that slides out 250px using JavaScript on the desktop view. However, I want the sidebar to take up 100% width when viewed on mobile devices. Despite my attempts to use Media Queries in JavaScript to achieve this, I am ...

Failure to receive results from $.getJSON request

I am currently working on developing a web page that allows users to search and retrieve Wikipedia pages based on their search results. Below is the relevant HTML code for the search feature: <form class='search-form' onsubmit='return f ...

Ensure that the top of a div stays in place as the user scrolls, and spans the entire width of the div

My goal is to create a sticky or fixed position header inside a div with a list, but I'm facing an issue where it only sticks to the width of the text. If I expand the width to 100%, it takes up the size of the entire page: import styled from 'st ...

What happens when the `.push` command runs the "Undefined" function in jQuery - is it possible to execute a number instead?

My script is quite simple - I'm trying to use the push method to add the result of a function called dNumber into the variable named pattern. However, every time I try this, the result is always "undefined". Oddly enough, when I try pushing just plain ...

Incorporate additional text to the downloads hyperlink using jquery

Is it possible to achieve this using jQuery since I am unable to directly modify the HTML markup? I am looking to append download.php?file= to a URL without replacing the entire href attribute. Here's an example of what I'm trying to achieve: & ...

Converting a webpage into UTF-8 format

Can you explain the concept of encoding a webpage to me? When someone mentions that a page is encoded in UTF-8 or another format like EUC-KR, what does that signify? Does it pertain to encoding done on the server-side or client-side? Is it related to th ...

Keep track of the toggled state and prevent any flickering when the page is reloaded, all without the

After extensive searching, I couldn't find exactly what I needed. Therefore, I decided to utilize cookies to remember the toggled state of a div whether it's hidden or visible. However, I encountered an issue where there is flicker happening as t ...

Invokeing JQ's on() function to trigger a click event on a concealed Google Infobox.Js

I'm currently using the Infobox.js plugin from Google API V3 to show custom info windows. However, I've encountered an issue where the infobox.js code hides each marker's info window and brings it to the front using z-index. Is there a way ...

Switching a div class in Angular 6 with a button click: A step-by-step guide

In this scenario, I have a div with the class name old. There is also a button that, when clicked, should toggle the div's class name between old and new. I am looking for a solution using Angular 6. Below is the code snippet: I am relatively new to ...

To avoid any sudden movements on the page when adding elements to the DOM using jQuery, is there a way to prevent the page from

I have a challenge where I am moving a DIV from a hidden iFrame to the top of a page using jQuery. Here is my current code: $(document).ready(function($) { $('#precontainer').clone().insertBefore(parent.document.querySelectorAll(".maincontainer" ...

Creating a contact form in WordPress that triggers a separate PHP file

I've been working on integrating a small contact form into the sidebar of my WordPress site. After moving the code from its original HTML file to the appropriate WordPress PHP files, everything seems to be functioning correctly except for the contact ...

You can use the following code to retrieve the value of the checked radio button with the name "nameOfradio", but make sure that the radio button

I am attempting to extract the value from a radio button using the following code: document.querySelector('input[name=nameOfradio]:checked').value; However, I would also like to retrieve a value even if none of the radio buttons are checked. Fo ...

Can one jQuery script be used for multiple ajax 'like' buttons?

Hey there! I'm working on an Ajax 'like' button that utilizes jQuery. This button will be utilized multiple times on a single page. I'm looking for a way to streamline the process and avoid including the jQuery script multiple times. Is ...

Visual button spacing

There is no CSS styling applied to the page. When two image buttons are placed in separate lines of code like this: <asp:ImageButton ID="btnVoteUp" Height="16px" Width="16px" runat="server" ImageUrl="images/thumbs_up.gif" CausesValidation="false" Co ...

Incorporating conditional statements within a loop

I'm racking my brains over this issue, can you lend a hand? Currently, I am extracting data from a website. The .MyElement containers on the site store either gif or jpg URLs that I need to retrieve. In my node.js app, I am utilizing a Cheerio-base ...

When I attempt to press the shift + tab keys together, Shiftkey is activated

Shiftkey occurs when attempting to press the shift + tab keys simultaneously $("#buttonZZ").on("keydown",function (eve) { if (eve.keyCode == 9 && eve.shiftKey) { eve.preventDefault(); $("#cancelbtn").focus(); } if (eve. ...