Overflowing Content Spilling from Div, Reaching Beyond Viewport Height

I am struggling with keeping the content inside a div from overflowing, despite using height: 50vh;. This div contains another div that has a background image and an overflow scroll while maintaining a specific aspect ratio.

Is it possible to resize the content inside the top div on browser resize while preserving the aspect ratio?

I would appreciate any guidance on how to achieve this. Thank you!

Here is what I have so far:

Codepen: https://codepen.io/anon/pen/LaLqNZ

    body,
    html {
      padding: 0;
      margin: 0;
    }
    
    #wrap {
      width: 100vw;
      height: 100vh;
      display: inline-block;
      margin: 0;
      padding: 0;
    }
    
    #top {
      width: 100vw;
      height: 50vh;
      display: inline-block;
      background: blue;
      padding: 20px;
      box-sizing: border-box;
    }
    
    #bottom {
      width: 100vw;
      height: 50vh;
      display: inline-block;  
      background: green;  
    }
<div id="wrap">
    <div id="top">
    <div class="one">
    <div class="two">
        <div class="three">
          <div class="four"><img src="https://i.imgur.com/4yBw2n4.jpg"></div>
        </div>
      </div>
      </div>
    </div>
    <div id="bottom">
    <h1>Sample Text</h1>
    </div>
    </div>

Answer №1

Hopefully this meets your expectations

document.addEventListener('DOMContentLoaded', function(){
setTimeout(function() { 
    resize();
}, 25);

});
    
function resize() {
var w = document.getElementById('monitor').width;
document.getElementById('two').setAttribute("style", "width: " + w + "px;")
}
body,
html {
padding: 0;
margin: 0;
}

#wrap {
width: 100vw;
height: 100vh;
display: inline-block;
margin: 0;
padding: 0;
}

#top {
width: 100vw;
height: 50vh;
display: inline-block;
background: blue;
padding: 20px;
box-sizing: border-box;
}

#bottom {
width: 100vw;
height: 50vh;
display: inline-block;
background: green;
}

.one {
max-width: 450px;
height: 100%;
margin: auto;
}

.two {
display: block;
margin: auto;
height: 100%;
position: relative;
}

.three {
position: absolute;
top: 5.3%;
right: 4%;
bottom: 23.5%;
left: 4%;
}

.four {
max-height: 100%;
overflow: scroll;
}

.four img {
max-width: 100%;
display: block;
}

.monitor {
    position: absolute;
    height: 100%;
}

::-webkit-scrollbar { 
    width: 0 !important 
}
<!DOCTYPE html>
<html>
<body onresize="resize()">
<div id="wrap">
    <div id="top">
        <div class="one">
            <div id="two" class="two">
                <img id="monitor" class="monitor" src="https://i.imgur.com/OQL0NR3.png">
                <div class="three">
                    <div class="four"><img src="https://i.imgur.com/4yBw2n4.jpg"></div>
                </div>
            </div>
        </div>
    </div>
    <div id="bottom">
        <h1>Sample Text</h1>
    </div>
</div>;

    </body>

</html>

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

Is it possible to analyze an API call and determine the frequency of a specific field?

Code: var textArray = new Array(); var allText = results.data._contained.text; for (var i = 0; i < allText.length; i++) { var text1 = allText[i]; var textHtml = "<div id='text_item'>"; textHtml += "& ...

Adjust the minimum and maximum values of the Y-axis based on the scale of the X-axis

Currently, I am faced with the challenge of adjusting the Y-axis to scale with changes in X-scale. I have a long unixtime value Array for initializing a flot chart, along with buttons to modify the scale. However, I am struggling to synchronize the Y-axis ...

React Native Redux Form Submission Not Functioning Correctly (No Output Values Displayed)

Utilizing Redux Form for gathering user information submissions has been a bit tricky. Although I have connected Redux Form with the store and structured the form as per the guidelines, I am facing an issue where no values are being passed through upon cli ...

The display of the AngularJS {{variable}} is not supported within a script tag

<script type="text/template"> <div class="abcd"> <div class="efg" data-price="{{::displayPrice}}"></div> </div> </script> I am facing an issue with a template that contains a div element with the da ...

ever-evolving background-image with dynamic CSS styling

Being new to both PHP and Javascript, please excuse any mistakes in my explanation. I have information stored in a PHP array that I bring to my index page using the function below (located in a separate file called articles.php that is included in my index ...

The resetting of checkboxes in the Boostrap form validation form is not functioning properly

I am currently working on form validation using Bootstrap. Although resetForm(); is functioning properly, I am facing an issue with resetting the checkbox upon form submission. I have attempted methods like $('.form-check-input').val();, $(' ...

Error: Unexpected syntax error occurred due to the use of '===' operator

Click here for image descriptionError: There is a syntax error in line 7 of the file add.php Code: <?php require_once('db.php'); if ( !empty($_POST['name']) &&!empty($_POST['alias']) &&!empty($_POST[&apo ...

The directive is becoming unbound

Currently, I am facing an issue with my code. I have set up a fiddle that demonstrates a directive displaying an alert and also a controller with an alert function. Everything is functioning correctly except for the fact that the binding for the text on th ...

Changing the class when a checkbox is selected using JQuery

I’m working on a bootstrap switcher and I want to change the panel color from grey to green when the checkbox (switch) is checked. I had it working before, but after updating the switcher, it no longer functions properly. Here is the main code for the s ...

Using jQuery Mobile alongside two distinct versions of jQuery

My current task involves inserting both jQuery and custom JavaScript into the DOM of an existing page. The jQuery is inserted right before my script, where I utilize window['my$'] = jQuery.noConflict(true);. This approach worked smoothly after ...

Code to execute function depending on the width of a div

Looking for a script that can run a function depending on the width of a div. I want the ability to trigger the function when the div is resized. For example, if the div's width is 300px, execute function 1; if it's 700px, execute another functi ...

"Exploring the process of transferring an ID from one service to another using the select feature in Angular

I need to store the ID I receive from one service into another using the select element. Here is my approach: <select class="form-control" id="select_fac" [(ngModel)]="rep.idfac"> <option selected disa ...

Is there a way to pick up text where a div element ends using Bootstrap or HTML?

Using Bootstrap, I placed a div on the right and now I want the text outside to seamlessly continue from where the text inside the div ends. You can see the visual representation of what I'm aiming for in this picture. Any suggestions on how to achiev ...

Exploring ways to access and display images from local files using JavaScript or alternative methods

Is there a way to load local images in JavaScript without requiring user input? Situation: Our php/js tool on a webserver allows users to process data, accessing numerous large images stored on the same server. However, the high traffic from loading thes ...

What is the best way to adjust the positioning of a Form Validator error message?

Currently, I am utilizing the jQuery form validator from . It has been quite effective overall, especially with conditional validation. However, there is one pesky error positioning issue that has proven to be quite troublesome. The challenge lies in vali ...

Tips for expanding a Bootstrap component to fill the rest of the available space excluding the navigation bar

I am attempting to ensure that the #output element fills the remaining space on the page, considering the presence of a navbar. Many solutions online recommend using min-height: 100vh, but this does not account for the navbar (borrowed from Bootstrap' ...

What are the steps to designing a unique JSON data format?

When working with a JSON data structure containing 100 objects, the output will resemble the following: [{ "Value": "Sens1_001", "Parent": Null, "Child": { "Value": "Sens2_068", "Parent":"Sens1_001", "Child" : { ...

Ways to verify whether a callback function supplied as a parameter in Javascript has any arguments

My task involves implementing the following: doSomething .then(success) .catch(failure); The success and failure functions are callbacks that will receive their value at runtime (I am developing a module). Therefore, I need to ensure that the fa ...

React BrowserRouter causing issues with "invalid hook calls"

Just starting out with React and I am working on setting up paths using BrowserRouter, Route, and Routes in my code. import React from "react" import "./App.css"; import { BrowserRouter as Router, Route, Routes } from 'react-router ...

Having difficulty uploading an image to Facebook through the graph API

I have a requirement to upload a photo to Facebook using the Javascript SDK, but I am experiencing some difficulties: Firstly, FB.login(function (response) { if (response.authResponse) { va ...