Scrolling the y-axis with a fixed height limit to prevent overflow

My current dilemma is a seemingly simple one:

<div class="container">
    <div class="a"></div>
    <div class="b"></div>
    <div class="c"></div>
</div>​

Within the container, there are 3 divs: A and B have fixed heights, while C's height should adjust dynamically along with the container. If the content within C exceeds its size, I want it to scroll without affecting the positions of A and B.

Code available at: http://jsfiddle.net/V2c9G/

Despite multiple attempts, I have yet to achieve the desired outcome.

I experimented with:

<div class="container">
    <div class="a"></div>
    <div class="b"></div>
    <div class="xxx" style="overflow-y:scroll">
       <div class="c"></div>
    </div>
</div>​

However, this approach did not yield the expected results as the container div should resize according to the browser window.

A suitable example can be found at (specifically related to layout adjustments when resizing the browser). Yet, this method involves JavaScript for recalculations, which is not an ideal solution.

Any thoughts or suggestions?

Answer №1

Revision 3:

In my revised solution, I have incorporated CSS as a fallback option from the previous edit but added JavaScript functionality to dynamically resize div elements upon page load and window size changes. This combination ensures a visually appealing layout even if JavaScript is disabled. The JS code snippet can be found below, and you can view a live demo on this link.

var resizeDiv = function(){
    document.getElementById('c').style.height = getWindowHeight() - 64 + 'px';
};

//Reusable method
var getWindowHeight = function(){
    if (window.innerHeight) {
        return window.innerHeight;
    }
    if (document.body && document.body.offsetHeight) {
        return document.body.offsetHeight;
    }
    if (document.compatMode=='CSS1Compat' &&
        document.documentElement &&
        document.documentElement.offsetHeight ) {
        return document.documentElement.offsetHeight;
    }

    return 740;//default height value
};

//Initiate resizing on pageload
window.onresize = resizeDiv;
setTimeout(resizeDiv);




To address your specific issue with the third div's height, I recommend setting it to take up the remaining space using absolute or percentage values, ensuring overflow is hidden on the parent container, allowing inner content to dictate scrollbar presence. Check out an updated fiddle demonstrating this aspect here.

Update:

Considering your mention of "Imagine the container is the browser", adjusting overflow to 'scroll' and setting the third div's height to 'auto' ought to display the scrollbar correctly inside the main container. See the modified example in action here.

Further Edit #2:

Based on your feedback regarding the percentage approach, utilizing percentage heights for all sections seems suitable. However, keep in mind the limitations mentioned regarding fixed vs. dynamic content placement. Another alternative involves setting a minimum window size and adjusting the third element's percentage accordingly. Here is a suggested implementation here. For optimal results, consider incorporating event listeners to adjust div sizes dynamically upon window resize.

Note: A blend of percentage and pixel units for sizing properties would simplify these scenarios immensely!

Answer №2

Perhaps what you're looking for is located here: http://jsfiddle.net/QsLFt/. It's unclear how to remove the hidden divs that are causing the scrollbar to disappear, but one possible solution could be setting a fixed width?

Answer №3

To achieve a scrollable overflow in the .container, use overflow-y:scroll

Take a look at this example:

http://jsfiddle.net/v4ZtN/

Edit (after comments):

Css:

.container{
    background-color: red;
    width: 90%;
    margin: 0 auto; 
    height:220px;
}

.a{
   background-color: yellow;
   height: 30px;  
   margin: 2px;    
}

.b{
   background-color: blue;
   height: 30px;
   margin: 2px;    
}

.c{
   background-color: green;
   overflow-y: scroll;    
   height:inherit;
}

Html:

<div class="container">
    <div class="a"></div>
    <div class="b"></div>
    <div class="c"><img src="http://www.boingboing.net/images/_documents_anemone_images_anemone850-1.jpg" alt=""/></div>
</div>

Edit:2 (after comments)

Update the style of .c with the following:

.c{
       background-color: green;
       overflow-y: scroll;    
       height:100%;
    }

Here is the updated fiddle: http://jsfiddle.net/XM4gH/6/

Answer №4

A div element will only display a scroll bar when content is added to it. Check out the demonstration here: jsfiddle

Answer №5

After reviewing your jsFiddle content, it appears that you can easily include the following line in the style definitions for your .container class:

    overflow:hidden;

To observe any scrolling effects, make sure to insert some content into the .c div.

Answer №6

Is this answer suitable for your needs? Click here to view

.container{
    background-color: red;
    width: 90%;
    margin: 0 auto;

    height: 315px;
}
.a{
   background-color: yellow;
   height: 30px;  
   margin: 2px;    
width: 90%;
}


.b{
   background-color: blue;
   height: 30px; 
   margin: 2px;    
width: 90%;
}

.c{
   background-color: green;
   height: 250px; 
   margin: 2px; 
width: 90%;    
   overflow-y: scroll;    
}
​

Answer №7

Understanding your intention from the question is a bit challenging, but if this is what you aim to achieve, I have identified a few issues within your code:

  1. To prevent the green box from protruding beyond the container, it was necessary to conceal overflow on the red box.
  2. In case the content in the green box exceeds its dimensions, a scrollbar is desired. Although the previous height setting (250px) allowed for scrolling, it caused the box to extend outside of the container. The appropriate approach is to determine the remaining space within the container and assign that as the height, which in this case is 132px. By enabling overflow scroll, any content surpassing this designated height will become scrollable.

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

Code for switching between accordion panels with a simple button press

I took some code snippets from a website and developed a shopping cart accordion module. The complete code is available on my CodePen profile, you can access it through this link: http://codepen.io/applecool/pen/YXaJLa <div class="summary"> <but ...

Filter JSON data deeply for specific values

I am attempting to filter JSON data based on user checkbox selections in JavaScript. The challenge I'm facing is implementing multi-level filtering. The data has two dimensions that need to be filtered: first by OS selection and then by a selected que ...

Jumbling a word by shuffling its letters into a random order

The objective of the program is to take the word you input into a box, split it into an array of letters, and then shuffle them. Following that, it should capitalize the first letter and lowercase the rest before displaying the result in the same box. I a ...

Preventing JSON data from being altered in AngularJS by creating a duplicate copy

It seems like there might be an issue with my implementation of AngularJS. I am trying to create a copy of a JSON object on page load, generate a form using the original data, and then compare the two JSON objects when the submit button is pressed to deter ...

What is the best way to keep horizontal divs aligned in a row with margins between them within a wrapper?

I have arranged three divs in a row with a 10px margin between them within a responsive layout. Currently, the divs have margin-left: 10px; margin-right: 10px; to create spacing. However, I aim to align them perfectly within the wrapper without any extra ...

Design all buttons, except for the two specific buttons

.button , button{ border:solid black 2px !important; } Using this code, I have added a border to all buttons. But now, I need to exclude the buttons "searchsubmit" and "single_add_to_cart_button" from having a border. How can I achieve that? ...

What is causing this code to run immediately upon the addition of Promise logic?

The coding scenario written below was supposed to have two 4-second delays. However, when the code is run, it executes immediately. It seems that there might be a lack of understanding on my part regarding some basic concept, or perhaps there's a hidd ...

Adjusting the document root in an Apache site's directory

I have been working on my portfolio website and I want to showcase some past projects. There is one particular website that I developed in the past which is no longer live, but I have an archived version of it. I would like to be able to view this old site ...

Discovering duplicate values in a JSON object using JavaScript (JQuery)

Below is a JSON object that contains information about various materials: [ { "idMaterial": "Alloy 450 (15Cr6Ni1.5Cu)_S45000", "tipoMaterial": "Alloy 450 (15Cr6Ni1.5Cu)", "uns": "S45000", "temperatura": "NL", "p ...

Styling ReactJS Components with CSS Styling

I am a complete beginner to React and I seem to be facing an issue with rendering CSS properly. The react class, App, that I have should display a Card with a progress tracker on it. var App = React.createClass({ render: function () { var pushNotific ...

Is there a way to deactivate the checkbox in an AngularJS input field?

<div ng-repeat="x in spaceutilization"> <input type="checkbox" name="{{x.filenumber}}" id="{{x.id}}" class = "pdffiles" value="101SP{{x.initials}}.dwg" /><label for="{{x.id}}"><button type = "button" class = "btn btn-primary btn-sm h ...

How do I use onclick to hide a <div> and reveal the content beneath it?

Is there a way for me to hide this <div> when the user clicks outside of it? I want the content behind it to be visible once the <div> is hidden. <html> <style> .overlay { position: fixed; top: 0; left: 0; height: ...

Is it possible to save the video title as a variable using YTDL-core?

Is there a way to store the video title as a global variable in JavaScript? ytdl.getBasicInfo(videoURL, function(err, info) { console.log(info.title) }); I have been trying various methods but I am unable to successfully define a variable to hold the v ...

What is the method for displaying the html required attribute in Django forms?

I am encountering an issue with a form field in Django. The email field is defined with the required attribute, but when rendered in HTML, it is missing the required attribute and is using type "text" instead of "email". The max_length attribute is, howeve ...

How can I create dynamic tabs using Tailwind CSS?

I am looking to create an animated tab using React and Tailwind CSS. Here is the code I have so far: import React from 'react' import clsx from 'clsx' export const Modal = () => { const [theme, setTheme] = React.useState<' ...

Rotate images with animation using Javascript

Seeking assistance with a swinging motion using two simple functions in Javascript. As a newcomer to the language, I am looking for guidance to achieve my goal. First function: function up() { imgObj.style.left = parseInt(imgObj.style.transform = 'r ...

Shifting a div element around the webpage and letting it fall into place if it intersects with another div using plain JavaScript

Check out this jsFiddle link. Upon opening it, you will encounter a moveable div. However, I am looking to enhance this functionality by allowing the div to disappear if moved to the 'trash' area. Essentially, when you place the moveable div in t ...

Add the appropriate ordinal suffixes ('-st', '-nd', '-rd', '-th') to each item based on its index

Imagine having an array filled with various option values, such as: var options = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grapefruit", "honeydew", "kiwi", "lemon", "mango", "nectarine", "orange", "pear", "quince", "raspberry", "strawbe ...

What is the process to set a Woocommerce checkout field as optional when a checkbox is marked?

I need assistance with customizing the checkout page on Wordpress Woocommerce. Specifically, I want to make the field 'billing_name' not required if the checkbox 'buy_on_company' is checked. Currently, I have successfully hidden ' ...

What is causing my ReactJS web application to not recognize the cookies being sent by the backend server?

I have a web application with a frontend built in ReactJS and a backend built in HapiJS. The backend is running on http://localhost:3000 and the frontend on http://localhost:1234. My goal is to implement authentication using cookies. I am using Axios in m ...