Maintaining Element Position in Responsive Design when Window is Resized Using Absolute Positioning Inside Relative

I have 2 div elements.

The first div is larger and has the position:relative property.

The second div contains a set of boxes inside the first div and has the position:absolute property.

As I resize the window to make it responsive, the height of the second div grows and overflows the first div.

To ensure that the height of the first div remains larger than the second one, I am using jQuery. It loops through the children of the first div and sets the biggestHeight accordingly.

However, the height of the first div does not increase dynamically when I resize the window. I need to refresh the page to see the effect.

I would like the height of the first div to increase dynamically as I resize the window until $(window).width() >= 576px.

I am considering if something like $(window).resize() could help in this case.

jQuery:

$(document).ready(function(){

    var biggestHeight = "0";
    // Loop through elements children to find & set the biggest height
    $(".thumbnail-portfolio *").each(function(){
     // If this element's height is bigger than the biggestHeight
     if ($(this).height() > biggestHeight) {
       // Set the biggestHeight to this Height
       biggestHeight = $(this).height();
     }  });

    // Set the portfolio container height. 
    $(".thumbnail-portfolio").height(biggestHeight+200);
    $(".img-size-portfolio").height(biggestHeight+200);

});

Here is the complete code: [1]: https://codepen.io/darthvadercodes/pen/erdGZM

Update 1:

Thanks to @deebs answer, the problem has been solved. I have updated the codepen code with the solution I was looking for.

Thank you everyone for your time.

Solution:

[https://codepen.io/darthvadercodes/pen/erdGZM ]

Answer №1

Indeed, to ensure the function works correctly as intended, it is essential to trigger it not only on page load but also on re-size. It would be beneficial to encapsulate this functionality in its own separate function and call it both on page load and page re-size events, as shown below:

function adjustHeights() {
   var highestHeight = "0";
   
   $(".thumbnail-portfolio *").each(function(){
    if ($(this).height() > highestHeight) {
      highestHeight = $(this).height();
    }
   });

   $(".thumbnail-portfolio").height(highestHeight+200);
   $(".img-size-portfolio").height(highestHeight+200);
}   

$(document).ready(function(){
   adjustHeights();
   
   $(window).resize(function() {
      adjustHeights();
   }); 
});

PS: Avoid adding 200 every time when setting the height to prevent exponential growth.

In line with deebs' suggestion, it might be advisable in the long term to rethink the structure of the page so that reliance on absolute positioning and manual height control can be minimized.

Answer №2

It might be beneficial to consider changing the layout of your design. Instead of setting a fixed height value of 960px for an image, you could remove that and utilize the "background-image" property on the div with the class "container" which contains the boxes. This way, the actual image can be resized appropriately without affecting the div itself.

Answer №3

To handle resizing events in JavaScript/jQuery, consider implementing the onResize event. For more information, check out https://api.jquery.com/resize/

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

Unable to assign multiple values to a bootstrap select from an array

Currently, I am dealing with a string: 93, 94 which represents the references of the values in a select option. My attempt to assign these values using: let values = "93, 94"; $('#property-categories').selectpicker('val', values); ha ...

The page is displaying a horizontal scrolling bar when utilizing bootstrap framework

Currently, I am in the process of designing a webpage that utilizes Bootstrap. To structure my page layout, I am utilizing the grid system and the col-lg-x class. Specifically, I have split the page into two sections with the code below: <div class=&ap ...

"Utilizing Bootstrap's form-check feature places the checkbox alongside the text

While attempting to create a checkbox using the Bootstrap form-check class, I encountered an issue with my code. Here is the snippet: <form> <div class="form-section"> <div>Title</div> <div class="form-check"> ...

The webmethod is not being triggered by Ajax

I'm currently working on troubleshooting the loadResources function by placing it behind a button call. The function is being called, however, the ajax always fails (resulting in an alert saying "loading of Engineers failed!") and I am unable to pinpo ...

Building four frames with HTML

I am looking to have four frames set up in an HTML document. However, with the current code provided below, only three frames are being generated. <!DOCTYPE html> <html> <FRAMESET cols="100%" rows="10%,90%" > <FRAMESET rows="100%,"& ...

I am working on an HTML form that is designed vertically, but I am unsure of how to arrange two text fields side by side on the same line

I'm struggling with formatting my HTML form to have two text fields on the same line instead of stacked vertically. In the example below, I want the Size, Width, and Height fields to be aligned horizontally rather than one below the other. <form c ...

Challenges between Django and VSCode

As I dive into practicing website development with Django, I've encountered a problem while trying to save my work in VSCode and updating my local browser. It seems that only certain changes are being reflected. For example, when I delete code in my C ...

Using JavaScript to Generate Formatting Tags Based on User Selections from a Multiselect Dropdown

When a user selects formatting options (such as bold, italic, underline) from a multiselect dropdown, I need to generate corresponding formatting tags. For example, if the user selects bold and italic, I should create a tag like <b><i></i&g ...

The CSS transition feature does not seem to be functioning properly when it comes to displaying

In my Next.js application, I am using a card component with a "read more" link that should expand the card when clicked. To achieve this behavior, I integrated the "react-show-more" library from https://github.com/One-com/react-show-more. I tried to add ...

What is the proper way to send an AJAX request with the data type set to

I am currently working on creating my own POST request. Below is the function I have written: function sendPost(o) { var h = new XMLHttpRequest(); h.onreadystatechange = requestComplete; function requestComplete() { if (h.readyState = ...

What is the best way to show a message of success once the user has been redirected to the homepage?

Currently, I have a registration form utilizing AJAX and PHP for validation. Error messages can be displayed on the registration page if the user does not correctly fill out the form. Upon successful registration, the user is redirected back to the home pa ...

AngularJS allows users to seamlessly retain any entered form data when redirected, enabling users to pick up right where they left off when returning to the form

I am currently working on a user data collection project that involves filling out multiple forms. Each form has its own dedicated HTML page for personal details, educational details, and more. After entering personal details and clicking next, the data ...

Tips on connecting data within a jQuery element to a table of data

I am currently developing a program that involves searching the source code to list out element names and their corresponding IDs. Instead of displaying this information in alert popups, I would like to present it neatly within a data table. <script> ...

Troubleshooting the issue: Unable to shift a div to the left in a Rails app using jQuery and CSS when a mouseover event occurs

Hey there, I'm working on a div that contains a map. My goal is to have the width of the div change from 80% to 50% when a user hovers over it. This will create space on the right side for an image to appear. I've been looking up examples of jqu ...

Unraveling the Json Object with php and angularJs

When transmitting data from AngularJS as JSON, it arrives in the following format: {data: "stdClass Object↵(↵[0] => B↵[vin] => vin123…[value] => Gambia↵[country_name] => Gambia↵)↵", status: 200, headers: function, config: Object} ...

Sending data from Angular to the DOM

Is there a way to dynamically pass a property to an HTML tag using Angular? <div class="my-class" data-start="10"> I am trying to figure out how to pass the value of data-start dynamically. This is for an Angular 5 application. Any advice would be ...

Scrollable tbody in a responsive Bootstrap 4 table

Utilizing Bootstrap 4, I have implemented a responsive table that I want to have a scrollable body. The table is structured as follows: <div class="table-responsive"> <table class="table table-striped table-hover custom-table text-nowrap"> ...

Verify if Javascript is enabled

Can I determine if the browser supports or has Javascript enabled? If not supported, my goal is to direct the user to a more user-friendly error page. My current tech stack includes jQuery and PHP Zend Framework. ...

Is there a way to adjust the placement of text in an HTML document?

Just starting out with web development and created some HTML code: <html> <body> {errors} <p>Input your numbers:</p> <form method="post" action="."> <p><inpu ...

Utilizing $.Deferred() in a loop of nested ajax requests

I have spent countless hours searching for solutions to my problem, but I am still hopeful that someone out there has a solution. The issue at hand is being able to receive a notification once function a() has finished its execution. The challenge lies in ...