When the height expands, the div spills over and engulfs the following div

I am facing an issue with my divs on a page that have a "see more details" link. When clicked, the div expands and should fit the height of the parent div seamlessly. However, currently, each div overflows onto the next parent div until I scroll, at which point it adjusts itself. How can I ensure that the div grows without spilling over?

Current Behavior:

Desired Outcome:

$(function() {
  var minHeight = null;
  var expCb = function($el) {
    $el.addClass('expanded');
    $el.find('.expand-details').text('Details -');
  };
  var collapseCb = function($el) {
    $el.removeClass('expanded');
    $el.find('.expand-details').text('Details +');
  };
  $('.details-content a.expand-details').click(function() {
    var currentCb = $(this).parent().hasClass('expanded') ? collapseCb : expCb;
    currentCb($(this).parent());
    updateCardDetailsLockups();
    return false;
  });
});
.details-content {
  max-height: 18px;
  overflow-y: hidden;
}

.details-content.expanded {
  max-height: 2000px;
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent1">
  <div class="child col1">
    <div class="details-content">
      <a href="#" class="details-header expand-details">details +</a> ...
    </div>
  </div>
  <div class="child col2">
    ...
  </div>
</div>

<div class="parent2">
  <div class="child col1">
    <div class="details-content">
      <a href="#" class="details-header expand-details">details +</a> ...
    </div>
  </div>
  <div class="child col2">
    ...
  </div>
</div>

Answer №1

In order to assist you better, additional information is required. It seems like the .col1 and .col2 classes may have the float property set. To resolve this issue, a clearfix needs to be applied to their parent for proper expansion.

.details-content {
  max-height: 18px;
  overflow-y: hidden;
}

.details-content.expanded {
  max-height: 2000px;
  display:block;
}

.parent1 { border: 4px solid orangered; /* for visibility */ }
.parent2 { border: 4px solid teal; /* for visibility */ }
.col1 { float: left; /* assuming this is the case */ }

/* Here is the clearfix, apply it to your container */
.with-clearfix:after {
  content: "";
  display: table;
  clear: both;
}
<div class="parent1 with-clearfix">
    <div class="child col1">
        <div class="details-content expanded">
            <p>With clearfix, the parent container is sized based on its floating children. You can see the red-orange border surrounding the content.</p>
        </div>
    </div>
</div>
<div class="parent2 without-clearfix">
    <div class="child col1">
        <div class="details-content expanded">
            <p>Without clearfix, the teal border is collapsed and the children are spilling out of the parent. A clearfix is needed to size the parent to its children.</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

Obtaining Spotify API access token using JavaScript code on the front end

I've developed a web application that enables users to generate a list of songs by artists related to a selected artist. The goal is to link the user's Spotify account and create a playlist based on this generated song list, which requires obtain ...

Does the color of <hr> tags in Bootstrap 5 look different?

Encountering a problem with Bootstrap 5 version where adding background-color to <hr> is not displaying accurately: Comparison using Bootstrap 4: https://i.sstatic.net/QIY0P.png Comparison using Bootstrap 5: https://i.sstatic.net/Drlow.png Despite ...

Display the HTML code in its formatted text output

I am currently facing an issue while trying to take input from the user using the WYSIWYG text editor plugin. When I attempt to display the text in a label or paragraph, all the HTML tags become visible. For instance, @string str = "<b>sample text&l ...

Sticky footer in Bootstrap causing content to overlap

I'm a beginner when it comes to Bootstrap, and I'm attempting to integrate it with Symfony2. I currently have a sticky top bar in place for navigation purposes. However, I'm facing an issue when I try to add a similar sticky footer at the bo ...

Make sure PTable maintains a horizontal layout on any device with PrimeNG

When I view my Ptable on desktop or iPad, it looks like this: https://i.stack.imgur.com/ONqZV.png However, when I switch to a device like an iPhone X, it changes to this layout: https://i.stack.imgur.com/H2q7j.png I want the horizontal layout to displa ...

I am interested in creating a header that remains static

Currently, I am attempting to create a header that has a freezing effect. My goal is to have a banner at the top with a navigation menu underneath it. When scrolling down, I want the banner to disappear but have the navigation menu remain frozen at the top ...

How can I make a div move to the position of another div and stay with it when the screen is resized?

In my card game project, I am dealing with an issue where cards are not aligning properly with the designated "dropZonePositions" when the screen is resized. Despite creating animations for the card movement that I'm satisfied with, the problem arises ...

What is the proper way to set a margin on a fixed div that has a width

I am looking to create a fixed header that allows the content (body) to scroll underneath it. The challenge arises when the parent element has some margin-right, causing the fixed header to extend beyond the parent's width and occupy 100% of the windo ...

The functionality for determining whether the value isset as 'Yes' or 'No' is not functioning properly in my PHP form

I'm currently working on creating a combined 'Order Calculator / Order Form' using one php form. The calculator part of the form is functioning perfectly and accurately calculates the total for all 5 order options both on the live page and i ...

HTML element DIV positioned above the iframe

I am looking for a way to automatically place a "PLAY" div above each iframe instead of doing it manually. I have successfully done it manually but I'm not sure how to achieve the same result with a script or using CSS. Below is my HTML markup: < ...

Tips for centering an image vertically in a table's cell

I'm looking to vertically center the text, but I'm not sure how to do it. Check out my attempt on the fiddle link below: http://jsfiddle.net/jTW4d/ ...

Instructions for submitting information from a web form using Python without needing to know the form's ID or name

I am currently attempting to automate the submission of data into the online forms found on this webpage: Unfortunately, I am unable to determine the specific names of these forms by inspecting the source code. I have tried using the requests python modul ...

Jquery animation in Wordpress without any need for scrolling

My Wordpress site features some animations that work flawlessly when the site is scrolled. The code looks like this: jQuery(document).ready(function($){ $(window).scroll( function(){ if(!isMobile) { $('.animate_1').each ...

Trouble displaying complete image (ReactJS, CSS)

I am looking to create a visually stunning landing page that covers the entire screen, showcasing a captivating image. However, I have encountered an issue where only a portion of the image is visible due to additional divs being created. Here is what my ...

Modifying the appearance of the search box

I'm looking to customize the appearance of my search box. On Stack Overflow, you'll notice the search box is a perfect rectangle. I want mine to have rounded edges, like an ellipse, rather than being rectangular. How can I achieve this look? Than ...

Creating a basic popup with jQuery: A step-by-step guide

Currently in the process of creating a webpage. Wondering how to create a popup window with an email label and text box when clicking on the mail div? ...

use a loop to add various html meta tags to the header section

In the midst of developing a Shopware plugin, my focus is on adding meta data to the html tag head. Utilizing twig, I extend a template and incorporate code into it. By triggering an event, I pass an array of meta entities mykn_metaFields to my twig templa ...

The process of transmitting messages back and forth between a popup script and a content script

I am in the process of developing a Chrome extension that involves sending data requests from a popup script to a content script (via a background script), analyzing the request on the content script, and then sending back a response (again through the bac ...

Using CodeIgniter, input information into a textbox and verify if there is a corresponding record in the database when submitting

Working on a CodeIgniter project, I am faced with the task of adding information to a form within my view page called meal_add.php. Below is the form structure: <div id="content" class="box"> <form action="<?php echo base_url(); ?>index ...

Can someone please clarify how the nth-of-type selector functions?

I can't understand why the nth-of-type selector needs to be set to 2 instead of 1 for the first sibling of this type in the code. To see the code in action, check out this jsfiddle: https://jsfiddle.net/xj5hvn16/ If anyone could kindly provide an ex ...