Adjust the size of the div element based on the size of its parent container

My goal is to develop a widget that adjusts its size based on the container's dimensions. The widget can be expanded to display extra information. There are three rows, and I want the following behavior:

  • The first row should adjust its size according to the content
  • The second row should behave similarly to the first one
  • The third row needs to fill the available vertical space, allowing its content to scroll when expanded. When collapsed, the widget should vertically center itself within the parent container

I apologize for using colors, but they serve as a visual aid. Here is my current setup:

https://i.sstatic.net/XJC0x.png

In the example above, the widget (white background) is collapsed and centered within the parent container (red background). Upon clicking "open," the first and second rows (first containing content, second just a button) should retain their original sizes, while the third row expands to occupy the available space. Here is what is happening currently:

https://i.sstatic.net/OHwN8.png

The widget (white background) correctly expands to fit the parent, but the expanded section (black background) overflows. What I aim to achieve instead is this (I used white background to fill the parent): https://i.sstatic.net/IUpkS.png

Below is the code snippet (ignore padding and colors which were added for clarity, along with the @click handler):

<div id="searchWidget">
        <div class="row no-row-padding">
            <div class="col-12" style="background-color: blue;">
                <p style="color: white">First row of content</p>
            </div>
        </div>
        <div class="row">
            <div class="col-12">
                <hr class="solid">
            </div>
        </div>  

        <div class="row no-row-padding">
            <button class="btn btn-outline-primary actionButton" type="button" @click="showAdvancedOptions">Open</button>
        </div>  

        <div class="row" id="advancedOptions" style="background-color: black; margin-left: 10px; margin-right: 10px">
            <div class="col-12" style="background-color: purple">
                <div class="collapse" ref="advancedOptionsCollapse" style="background-color: yellow">
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                    <p>Hello World</p>
                </div>
            </div>
        </div>                        
    </div>

Here is the corresponding CSS:

#searchWidget {
    padding: 16px;
    max-height: 100%;
    width: 100%;

    background-color: white;
    display: flex;
    flex-flow: column;
}

I have experimented with various flex properties but struggle to find a combination that expands and contracts the third row accordingly. I am using Bootstrap 4.0.0 along with Vue.js. Any assistance would be greatly appreciated.

Answer №1

By adding overflow:hidden to the parent element (searchWidget) and overflow:auto to the third row, it should function correctly.

<div id="searchWidget" class="vh-100 d-flex flex-column overflow-hidden justify-content-center">
    <div class="row no-row-padding">
        <div class="col-12" style="background-color: blue;">
            <p style="color: white">First row of content</p>
        </div>
    </div>
    <div class="row">
        <div class="col-12">
            <hr class="solid">
        </div>
    </div>
    <div class="row no-row-padding">
        <button class="btn btn-outline-primary actionButton" type="button" @click="showAdvancedOptions()">Open</button>
    </div>
    <div class="row overflow-auto" id="advancedOptions" style="background-color: black; margin-left: 10px; margin-right: 10px">
        <div class="col-12" style="background-color: purple">
            <div :class="collapseClasses" ref="advancedOptionsCollapse" style="background-color: yellow">
                <p>Hello World</p>
                <p>Hello World</p>
                ...
            </div>
        </div>
    </div>
</div>

Answer №2

After some troubleshooting, I finally cracked the code by utilizing flexbox:

#searchWidget {
    max-height: 100%;
    width: 100%;
    display: flex;
    flex-flow: column;
}

#advancedOptions {
    flex: 1;
    display: flex;
    flex-direction: column;
    white-space: nowrap;
    flex-basis: auto;
    overflow: scroll;
}

This ingenious solution allows the container to adjust its size when minimized, and occupies the available area while enabling scrolling for the third row when maximized.

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

The primary section is not extending completely to the bottom, resulting in the footer not being aligned correctly at the bottom of the

Despite the numerous similar questions on Stack Overflow, I am struggling to get my footer positioned correctly on my page. The <footer> stubbornly remains 800px above the bottom behind the <main>. Even with various height options such as 100%, ...

What is the best way to locate a specific button in jQuery?

How can I determine if the target of a click event is a button element in a document? $(document).click(function(e){ if(e.target.nodeName.toLowerCase() === 'button') { // Do something } Is the code above correct for this purpose ...

What is the process for locating inline CSS styles within HTML body tags?

I'm currently assisting with CSS tasks and have noticed that some developers have included inline CSS in the code. Our preference is to avoid using inline CSS, so I am curious if there is a way to detect the presence of inline CSS within the <body& ...

Display the data count of a filtered list in Vue 2

As a newcomer to Vue, I am facing what seems like a simple and common task but the solution is escaping me. I need to filter some data and then display the count in a component. This is the HTML structure: <main class="container-fluid"> <div ...

New to JavaScript and Bootstrap - eager to learn by using Bootstrap 4 Chart Template, just need help with a small issue in the code

I am eager to dive into Bootstrap 4 and data visualization charts. I want to replicate the visualizations found in the link below. However, after copying and pasting the code into Atom, it's not functioning as expected. I ensured that I copied the HT ...

Apply a chosen style to the element that was clicked on, while removing the style from the rest

Here is an example of my ul li structure: <div id="categoryTree"> <ul> <li id="cat_15"> <a class="hasSubCat" href="javascript:void(0);"><img src="images/icons/folder.gif" border="0" alt="Folder" title=" Folder ">N ...

Validating Password Consistency using Javascript

I'm having trouble with validating the password fields and ensuring they match, even when both input boxes contain the same password. JavaScript var validator = $("#signupform").validate({ rules: { password: { required: true, minle ...

What could be causing the inability of select:-moz-focusring to eliminate the dotted lines in the Firefox browser on this bootstrap navigation

Is there a way to remove the Firefox dotted lines from my bootstrap navigation bar? I've come across suggestions to add the following code snippet to the CSS file: a:-moz-focusring { color: transparent; text-shadow: 0 0 0 #000; ...

Issue with React: Caret icon in dropdown menu shifts position when page is resized to iPad or smaller dimensions

I'm facing an issue with a styled react component that has a custom dropdown menu featuring a caret icon. Whenever I test the responsiveness of the dropdown, the caret icon ends up outside the intended area. To resolve this, I've wrapped the drop ...

Utilize JavaScript to assign a button with identical functionality as another button

After developing a slick slider, I decided to add custom arrows. The standard method from slick involves using prevArrow: and nextArrow:, but I wanted the buttons outside of the slick-slider container. To achieve this, I placed additional prev and next but ...

Properly aligning sub-divs within a parent div using CSS

I'm facing a problem with my CSS styling that I need help with. Here is the screenshot of the issue I am trying to center align the inputs, each placed in its own div, within my form. Below is the HTML markup for the form: HTML Markup Code CSS st ...

Python Script for Scanning a Website for a Specific Tag

Currently, I am exploring the process of creating a website monitoring script (which will eventually be set up as a cron job) that can access a specified URL, verify the presence of a specific tag. If the tag is not found or does not contain the expected i ...

Error: Invalid syntax detected: /blog

I am encountering an issue with jQuery while trying to add a blog section to my existing single-page site. The new blog will be located in a separate /blog directory, causing the menu item for it to differ from the other href="#" tags on the index.html pag ...

How to select a button within a table using Selenium and C#?

I'm struggling with a section of HTML code. My goal is to use the selenium chrome driver in C# to navigate a website and create a ticket. However, I've encountered an issue where I need to select a tab to check some checkboxes, but all the tabs h ...

Learn the process of transferring product details to a new component in Angular when a product is clicked from the product list component

Currently, my focus is on creating a shopping application using Angular and Django. I managed to get the products from the Django API into the angular products list component. However, I'm looking to enhance the user experience by allowing them to cl ...

Guide to Embedding a SQL Table into an HTML Table

I need to set up a table within my current webpage to showcase a list of movies along with their genres. Welcome to my Movies page. The index.php file contains all the necessary database credentials and establishes the connection. <?php session_start( ...

Struggling to properly line up the baselines of navigation list items that are styled as circular elements using CSS

I have transformed my navigation menu into a series of CSS circles with text inside. The issue I am facing is that the text spills out unevenly based on the amount of content in each circle. To address this, I used a JavaScript solution to center-align the ...

Aligning with the parent element and adjusting slightly towards the left side

.parent-div { box-sizing: border-box; min-width: 0px; padding: 1rem; min-height: 1px; margin: 0; border: solid 1px; } .child-div-one { box-sizing: border-box; margin: 0; min-width: 0px; min-height: 400px; display: flex; border: sol ...

"Each time the header of the BS5 accordion is clicked, it van

While using the BS5 accordion, I noticed that it's behaving differently than described in the documentation. Whenever I click on the header, it disappears. <link href="//cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="style ...

Revamp the Side Navigation Feature to Identify the Currently Viewed Section of the Page

My website currently features a side navigation bar that dynamically updates based on the user's scroll position. When the user reaches a specific height, a class is added to a div in the sidebar, turning it orange and indicating which part of the pag ...