Concealed material warps the column in a CSS multicolumn design

I'm working on creating a 3-column grid where hidden objects can be revealed and expanded within the same column without affecting the layout of other columns. Currently, when the button to show the hidden content is clicked, the content below shifts to the next column. How can I keep it within the same column?

$("#clickable").click(function(){
  $(".hidden_until_button_clicked").toggle();
});
#container {
  width: 100%;
  max-width: 700px;
  margin: 2em auto;
}
.flex-col {
  columns: 3;
  -webkit-columns: 3;
}
.box {
  height: auto;
  display: block;
  margin-bottom: 20px;
  page-break-inside: avoid;
  background-color: red;
}
.hidden_until_button_clicked{
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <div id="container" class="flex-col">
    <div class="box">Box 1</div>
    <div class="box">
      Box 2 <br />
      <button id="clickable">Click me</button>
      <div class="hidden_until_button_clicked">
        <p>This is all the extra content hidden until clicked</p>
      </div>
    
    </div>
    <div class="box">Box 3</div>
    <div class="box">Box 4</div>
    <div class="box">Box 5</div>
    <div class="box">Box 6</div>
    <div class="box">Box 7</div>
    <div class="box">Box 8</div>
  </div>

Answer №1

To display the hidden content after a click, you can use absolute positioning on the div:

$("#clickable").click(function(){
  $(".hidden_until_button_clicked").toggle();
});
#container {
  width: 100%;
  max-width: 700px;
  margin: 2em auto;
}
.flex-col {
  columns: 3;
  -webkit-columns: 3;
}
.box {
  height: auto;
  display: block;
  margin-bottom: 20px;
  page-break-inside: avoid;
  background-color: red;
}
.hidden_until_button_clicked{
  display: none;
  position: absolute;
  background: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <div id="container" class="flex-col">
    <div class="box">Box 1</div>
    <div class="box">
      Box 2 <br />
      <button id="clickable">Click me</button>
      <div class="hidden_until_button_clicked">
        <p>This is all the extra content hidden until clicked</p>
      </div>
    
    </div>
    <div class="box">Box 3</div>
    <div class="box">Box 4</div>
    <div class="box">Box 5</div>
    <div class="box">Box 6</div>
    <div class="box">Box 7</div>
    <div class="box">Box 8</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

JQuery cannot target content that is dynamically inserted through an Ajax request

When I use an ajax call to pass dynamically generated html, such as in the example below: var loadContent = function(){ $.ajax({ url: '/url', method: 'GET' }).success(function (html) { $('.con ...

Why isn't my background image appearing on the screen?

I'm experiencing an issue with my background image not displaying properly. I am currently using bootstrap 5 and have tried several solutions without success. How can I resolve this problem? html { background: url(https://via.placeholder.com/150 ...

Utilizing Django to extract values and dynamically inserting them as placeholders on a separate page

On my initial webpage, there is a submit button that includes an array of necessary information in the value tag. <button type="submit" name="editEntry" value="[{{ entry.id }}, {{ entry.first_name }}, {{ entry.last_name }}, {{ entry.country }}, {{ entr ...

What is the process for enabling a Submit button and updating its value following a successful Ajax response within a Django environment?

My current project involves using Django as the web framework. I am in the process of setting up an AWS lab or AWS account. To avoid multiple submissions, I have disabled the Submit button once the user enters information such as lab name, CIDR block, etc. ...

Calculate the variance between two dates in HTML

I am looking to develop a customized countdown timer for various future dates. Below is the code snippet where I input a date to specify the upcoming event: <body> <form action="countDown.php"> <td> Select date: <input type="date" n ...

Tips on adjusting section height as window size changes?

Working on a website that is structured into sections. Initially, all links are aligned perfectly upon load. However, resizing the window causes misalignment issues. Check out my progress at: karenrubkiewicz.com/karenportfolio1 Appreciate any help! CSS: ...

Unable to load module/controller file from Angular 1 into view/html

var app = angular.module("appModule",[]); app.controller("viewController",function($scope){ $scope.greeting = "Welcome to the future"; }); <html> <head> <script src="Scripts/script.js"></script> <script ...

A simple way to set a button as active when clicked in a form element

Is there a way to activate the input button with an Onclick event? <div class="paForm-right"> <form onsubmit="" name="form1" id="form1" method="post"> <input type="submit" class="paForm-choose" id="paForm-bottom-vk" value="Visi ...

"Utilizing jQuery to asynchronously make an AJAX request with a setTimeout

Question about SetTimeout: Why does setTimeout(fn, 0) sometimes help? After reviewing the jQuery 1.8 source code, WHAT is the reason behind using setTimeout with a delay of 0 ms ? (instead of simply executing the callback immediately ?) Link to the ...

Is it a commonly recommended method to nest fxLayout containers?

Recently, I ventured into using Angular Flex for the first time and I find myself a bit unsure about the nesting of layout containers. The issue arises when dealing with a navigation tag that triggers an angular-material sidenav opening to the left, pushin ...

What is the process for combining three.js renders with additional HTML elements?

As someone who is new to the world of programming, stackoverflow, and everything in general here, I kindly request your patience and understanding. I have experience creating simple scenes using Three.js and applying effects using HTML Canvas independentl ...

Attempting to adjust a script that hides and reveals content in a Django project so that the first div automatically opens when the page loads

I'm currently working on enhancing a blog feed feature. The script I have opens up a div and displays its content when clicked, but my goal is to have the first div in the feed open automatically when the page loads. Being relatively new to django and ...

A guide to simultaneously sending dual variables by implementing Ajax via JavaScript

I am currently facing an issue with a textarea and PHP variables. I have created a script as shown below: $(document).ready(function(){ $('.post').keyup(function(e){ var post = $.trim($('.post').val()); if (post != ...

Integrate a delete option directly into a Django database table for easy removal of raw

I am currently working on a web page that has a form to add data to the database and display it in a table One of the requirements is to implement a button that can delete a specific row by its ID from the database The code snippet below is from index.ht ...

The Carousel is covering up my Mega-Menu and it is not visible on top

I'm facing an issue where my mega menu is hidden behind the carousel, and I need it to show on top. You can see how it looks before adding the carousel and after adding the carousel from Bootstrap. Here are the links to the Mega Menu CSS and Mega menu ...

In JavaScript, use the following code to replace "(" with "(":

Is there a way to dynamically transform the string "Test(5)" into "Test\(5\)" using JQuery or Javascript? I attempted this approach, but it did not work as expected var str = "Test(5)"; str = str.replace("(","\("); str = str.replace(")"," ...

Instructions for using arrow keys to navigate between div elementsHow to use arrow keys for navigating through

Is it possible to navigate between div elements using arrow keys like in Notion's editor? <div> hello word </div> <div>hi</div> <div>notion</div> Given the code above, how can one move the cursor to another ...

Is "getElementById" malfunctioning on Firefox?

Is there a way to retrieve the file name from a file upload and display it in a textarea across different browsers? It seems to work in IE and Chrome, but not in Firefox. Any suggestions on how to solve this issue? Thank you! function takeName( ...

Include a navigation bar in the footer of my WordPress Theme's footer.php file

Looking for help with adding a custom footer to my WordPress theme. I have successfully uploaded my static website here, and now I am in the process of making it dynamic. I have added my primary menu to WordPress, but I am unsure of how to exactly add a fo ...

Utilizing Bootstrap and Javascript to efficiently handle multiple forms

My website uses Bootstrap for layout, and I need to submit a form that is duplicated for different screen sizes - one for large layouts and another for mobile. However, both forms go to the same endpoint, so I have to give them unique IDs. <div class=&q ...