Adjust the size of a nested div with varying height

Hey there! I'm facing an issue with a main div that contains 2 other divs. I need the height of the lower div to change dynamically based on the resize of the main div.

Here's the code I have so far:

<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title></title>
    <style type="text/css">
        #upper{
            background:#F00;
            border: black solid 2px;
            overflow-y:scroll;
            height: 50px;
        }
         #lower{
            background:#AAF;
            border: red solid 2px;
            overflow-y:scroll;
        }
        #maindiv{
            height: 100%;
            border: green solid 2px
        }
        html,body{margin: 0px;}
    </style>
</head>
<body id="bodyId" > 
<div id="maindiv">

    <div id="upper">test<br/>
    test<br/>
    test<br/>
    test<br/>
    test<br/>
    v
    test<br/>
    test<br/></div>

    <div id="lower" onresize="resize('lower')">test2</div>
</div>
    <script type="text/javascript">
        function resize(arg)
        {
            var frame= document.getElementById(arg);
            var heights= document.getElementById(arg).parentNode.offsetHeight ;
            alert(frame);
            frame.style.height = heights -50 + "px";


        }



    </script> 
</body>
</html>

I'm having trouble getting it to work. My goal is to pass the id of the lower div (lower) to a function, retrieve the id and height of the parent div (maindiv), and set the height of the lower div accordingly.

Any insights or suggestions would be greatly appreciated!

Thanks in advance for your assistance.

Sincerely, TheVagabond

Answer №1

If you want to ensure your website looks great on all screen sizes, using media queries in your CSS is a smart approach.

For beginners, a helpful resource to learn about media queries is W3 schools.

Consider this simple example with a parent div and two child divs:

<div id="parentContainer">
  <div id="d1"> </div>
  <div id="d2"></div>
</div>

You can style these elements as follows:

#parentContainer {
  border:1px solid red; 
  height:100%; 
  width:300px; 
}

#d1, #d2 {
  width:300px; 
  height:300px; 
}

#d1 {
  background-color:green; 
}

#d2 {
  background-color:orange;
}

Now, by using media queries, you can customize the styling based on screen size changes:

@media screen and (max-width:400px) {
  #parentContainer {
    height:100%; 
  }

  #d1, #d2 {
    height:50%; 
  }
}

This code snippet adjusts the heights of the divs when the screen width is 400px or less. This creates a dynamic and responsive layout for your website.

By implementing media queries effectively, your design will adapt smoothly to various screen sizes.

Answer №2

<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title></title>
    <style type="text/css">
        #upper{
            background:#F00;
            border: black solid 2px;
            overflow-y:scroll;
            height: 50px;
        }
         #lower{
            background:#AAF;
            border: red solid 2px;
            overflow-y:scroll;
        }
        #maindiv{
            height: 100%;
            border: green solid 2px
        }
        html,body{margin: 0px;}
    </style>
</head>
<body id="bodyId" onresize="resize('lower')"> 
<div id="maindiv">

    <div id="upper">test<br/>
    test<br/>
    test<br/>
    test<br/>
    test<br/>
    v
    test<br/>
    test<br/></div>

    <div id="lower">test2</div>
</div>
    <script type="text/javascript">
        function resize(arg)
        {
            var frame= document.getElementById(arg);
            var heights= frame.parentNode.offsetHeight;
            frame.style.height = heights -70 + "px";


        }



    </script> 
</body>
</html>

@Lena correctly identified the issue with the body, but the script needed some corrections. Thanks for pointing that out.

@F. Bar, I will explore using media queries for this, although determining the starting height for the lower div might be a challenge. Still, appreciate the suggestion to consider media queries, it's a new concept for me.

Answer №3

Ensure to retrieve the parentElement rather than the parentNode. Below is a code snippet including a button for resizing testing purposes. You can invoke a function whenever the height of the upper div changes on an event.

function adjustSize(arg)
{
  var frame= document.getElementById(arg);
  var heights= document.getElementById(arg).parentElement.clientHeight;
  frame.style.height = heights -50 + "px";
}
#upper{
  background:#cca;
  border: black solid 2px;
  overflow-y:scroll;
  height: 50px;
  box-sizing: border-box;
}
#lower{
  background:#9c9;
  border: red solid 2px;
  overflow-y:scroll;
  box-sizing: border-box;
}
#maindiv{
  display: block;
  height: 100%;
  border: green solid 2px;
  box-sizing: border-box;
}
html, body
{
    height: 100%;
    width: 100%;
    margin: 0px;
    
}
<div id="maindiv">

    <div id="upper">test<br/>
    test<br/>
    test<br/>
    test<br/>
    test<br/>
    v
    test<br/>
    test<br/></div>

    <div id="lower">test2</div>
    <button onclick="adjustSize('lower')">resize</button>
</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

Aligning the stars with CSS

One of the components I have deals with a star rating system, and I thought it would be cool to use Font Awesome icons for half stars. Everything is working well except for the CSS styling aspect. While I managed to position some of the stars correctly by ...

What is the best way to transfer an integer from my main application to a separate JavaScript file?

Currently, I am developing a complex code using React Bootstrap and focusing on creating a Dropdown list that fetches data from the backend database. <Dropdown> <Dropdown.Toggle variant="success" id="dropdown-basic"></D ...

What causes <input> elements to vertically center when using the same technique for centering <span> elements? (The line-height of <input> does not match its parent's height)

Important: The height of <div> is set to 50px, and the line-height of <span> is also 50px When 'line-height' is not applied to the <span>, all elements align at the top of the parent. However, when 'line-height: 50px&apo ...

Unable to activate animation within a nested ngRepeat loop

I'm struggling to understand how to initiate animations within a nested ngRepeat using Angular. The CSS class ".test" is supposed to be animated. However, when I apply ".test" on the inner ngRepeat, it doesn't seem to work (Plunker): <div ng ...

Netlify deployment is failing to display the CSS styling on a website built with Hugo

Currently in the process of creating my own website using the "Call Me Sam" hugo theme. Everything looks great locally but once deployed on Netlify, the CSS styling seems to be completely disregarded. If you want to check out my repository: https://github ...

The div is empty when trying to display Google Maps

When I set the height of my Google map div in pixels, it displays correctly. However, when I try to set it as a percentage, specifically 100%, the map does not show up. I have tried setting the height of all parent divs as well, based on suggestions from S ...

Ways to align text and horizontal rule perfectly within a div container?

I need to add some text to my <div> container and make sure it is centered. .hr { background: green; height: 50px; margin: 65px 0; width: 3000px; } <div class="hr"></div> This is an illustration of my project: https://i.sstatic.n ...

How can I locate an element within the body of an if statement?

I am working on a simple JavaScript (jQuery library) if statement to check for the presence of a div element with the class 'video' on the page. If it finds the element, the variable 'arrows' is set to true, otherwise it is set to false ...

Implementing dynamic styling with Alpine JS when hovering

I am working with 2 color pickers, one for background and one for text. These are connected to the styles of a button <button :style="`background-color: ${bg_colour}; color: ${text_colour};`">TEST BUTTON</button> Everything is funct ...

Ways to implement a conditional statement to display a div using JavaScript

Looking for a way to utilize Javascript conditions to toggle the visibility of a div in an HTML5 document? Check out this code snippet below: #demo { width: 500px; height: 500px; background-color: lightblue; display: none; } To set the f ...

What is the method to alter the color of an SVG source within an image tag?

I am currently working on a component that involves changing the color of an SVG icon from black to white when a color prop is given. export class CategoryIconComponent extends React.Component { static displayName = 'CategoryIcon'; state = ...

Display a portion of the existing URL as a clickable link on the webpage using JavaScript or PHP

If I have a website with the URL and I would like to showcase the image at https://example.com/image.jpg on my site (), what can I do? I attempted the following approach, but it only displays the URL of the image. <p id="image"></p> <scri ...

Numerous Controllers in AngularApp failing to function properly

One issue I'm encountering is that only one out of the two controllers within my app seems to be registered: var app = angular.module('MyModule', []); app.controller('FruitCtrl', function($scope) { $scope.fruits = ['appl ...

Filter a data array using a two-dimensional filter array that may change dynamically

Currently, I am facing a challenge where I need to filter a data array of objects using a two-dimensional filter array of objects. Here is a glimpse of my data array: dataArray = [{ Filter_GroupSize: "1" Filter_Time: "5-30" title: "Tools Test ...

The art of uploading images with HTML and CSS

Looking for guidance on how to convert this image bubble so that users can upload their images during account creation. When the user hovers over the image bubble, it should display "Upload profile image" and prompt them to upload an image upon clicking. A ...

I designed my radio buttons to ensure they cannot be selected multiple times

I have developed a basic React custom controlled radio button: export const Radio: FC<RadioProps> = ({ label, checked, onChange, disabled, }) => { const id = useId(); return ( <div> <label htmlFor={id} ...

Two objects precisely located in the same spot yet not visible simultaneously

There are two elements here. One is a transparent button, and the other is an image positioned behind it. The background property didn't work for me, so I attempted different variations without success: .top-container > button { background-ima ...

Why is async await giving back a promise object instead of my intended value?

During an event triggered by a select element, I am trying to call a function named init(); Within the init() function, another function called getManifestJsonFilePath() is invoked. Inside the getManifestJsonFilePath() function, I am attempting to search ...

Troubleshooting issue with Django development server causing HTML5 video element to become non-seekable

My Django app is currently serving a webpage with an HTML5 video element, but I've encountered a strange issue. The video.seekable property is returning a timeRanges object with a length=0, when it should actually be length=1. Unfortunately, this mea ...

Is there a way to deliberately trigger an error using Selenium WebDriverIO?

Is there a way to trigger an error using ChromeDriver with Selenium WebDriverIO? I'm not sure if there's a method like browser.fire('error'). ...