Dynamically adjusting the size of two block elements

I'm facing a challenge that seems insurmountable at the moment.

Picture this: there's a container block with fixed height and width. Inside it, two blocks reside - one orange and one blue.

The goal is for the orange block to adjust its height based on the height of the blue block. The blue block sits at the bottom of the container block using absolute positioning.

How can I achieve this using only CSS? (and minimal HTML)


Check out this little demo showcasing my issue: http://jsfiddle.net/uK64u/2/


Here's one of several attempts:

HTML:

<div id="container">
    <div id="orange"></div>
    <div id="blue">Lorem ipsum dolor sit amet.</div>
</div>

CSS:

#container
{
    width: 400px;
    height: 700px;
}

#orange
{
    position: absolute;
    height: 100%;
}

#blue
{
    position: absolute;
    height: auto;
    bottom: 0;
}

This attempt falls short as the blue block overlaps the orange one instead of adjusting its height.

Answer №1

To achieve this effect, you can set the orange box to have a position:relative and the text inside the blue background to have a position:absolute. Take a look at the FIDDLE

HTML

<div class="box-outer">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. </p>
</div>

CSS

.box-outer {
    border:1px solid #222;
    background-color:orange;
    width:200px;
    height:300px;
    position: relative;
 }
 p {
    background-color:blue;
    padding:10px;
    margin-bottom:0px;
    vertical-align:baseline;
    position: absolute;
    bottom: 0;
    left: 0;
  }

I've made some tweaks to my fiddle for clarity!

Answer №2

Alright, let's explore an approach to achieve the desired behavior. While it may remind you of older table-based layouts, this HTML structure could help:

<div class="container">
    <div class="orange">
        <!-- Content -->
    </div>
    <div class="blue">
        <div class="content">
            <!-- Content -->
        </div>
    </div>
</div>

Combine this with the following CSS styling:

.container {
    width: 400px;
    height: 700px;
    display:table;
}
.orange {
    background:#AAA;
    height:100%;
    display:table-row;
}
.blue {
    background:#CCC;
    display:table-row;
}
.blue .content {
    max-height:500px;
    overflow:scroll;
}

This setup should achieve the desired effect. Feel free to experiment with the code by adding content to the bottom element - you'll notice that the top element adjusts its size accordingly. You can test this functionality in action through this JSFiddle.

I've also added a height restriction to the content within the bottom element along with scroll bars for overflow. However, feel free to modify or remove these features based on your specific requirements.

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

Removing a CSS class using JQuery

Within my website layout, I have a div that is dynamically included using PHP. This div is inserted in two different locations, each inside its own parent div. <div id="parent_div"> <div id="contact_details_div" class="contact_details_div sam ...

Substitute a piece of text within an element while also preserving the formatting of a new line

My goal is to modify the text within the div tag "a/b" to "b/c" while preserving the line break that I have inserted in the HTML using the break tag. Currently, the text is being replaced, but I need the second line to appear below the first line. $(".t ...

Ways to incorporate physics into CSS animations

Creating a loading screen with CSS and aiming for realistic behavior can be a challenge. Utilizing the animation-timing-function: cubic-bezier(1, 0, 1, 1) property gives a decent result, but perfecting it requires understanding how the parameters in cubic- ...

Tips for effectively implementing word count functionality in javascript

After developing a function to randomly generate a paragraph, I'm seeking advice on how to display the frequency of each word in the paragraph when a button is clicked. Should I implement a counter variable for this purpose? <html> <head> ...

What is the best way to apply CSS to a mat-row element only when it is being hovered over?

I have a table with rows where a specific condition triggers a light red background color for each row. On hover, the background changes to light gray for all rows. However, I need the special rows (already colored light red) to have a deeper shade of red ...

Issue with React Native Flex Not Filling Width Equally

My Custom Component, a Simple Counter, Has a Small Style Error. Despite Trying Multiple Solutions, I Couldn't Figure Out the Problem. Can Someone Please Tell Me What's Wrong and How to Fix It? Mistake I Have Added 3 Touchable Opacity Components ...

Have you tried incorporating Nostramap Javascript into your vue.js projects?

Instructions on Using Nostramap API to Call a Map. Refer to the following example: ...

Can the color of an ion-radio be adjusted when it is not selected?

I am experiencing an issue where I need to change the default color (grey) to a specific color of my choice, such as the primary color. I have attempted the following methods: <ion-col sizeXs="12" sizeMd="6" class="radio-box"> <ion-item line ...

The input box fails to show larger values on the user's page

Show me the biggest number that the user enters in an input box and then display it back to them. I need some improvements to my code, ideally making it possible with just one input box instead of two. <!DOCTYPE html> <html la ...

While using Tcpdf, I encounter an issue where the textarea value I pass is not being printed in the desired format

Currently, I am experimenting with TCPDF to create PDF files from HTML. However, I have encountered an issue where passing a variable containing text results in the text being printed on separate pages for each line. Is there a way to ensure that the tex ...

Having trouble getting the hover effect to work when selecting a different section of the SVG

In my SVG checkbox design, I have a circle element surrounding a polyline element (which acts as the checkmark). The boundaries of the icon extend beyond the circle, causing hover styles to trigger even when hovering outside the circle. I want to change st ...

How can I integrate classes into Bootstrap popover content?

By utilizing the provided html, I am able to display a popover html button. However, the issue arises when attempting to add classes to the button, as it causes the popover button to malfunction and appear on the screen. Is there a solution that allows for ...

What is the best way to ensure a consistent time interval between invoking two functions in JavaScript?

Is there a way to create a code that will call one function, let's say toStart(), and then immediately call another function, toStop(), exactly two seconds after the first function is initiated? I want this process to continue until a specific button, ...

In CAKEPHP, emphasize a row in a Product table to indicate a comparison condition, without the need for hovering

I've been experimenting with different approaches to make this comparison work. Despite trying various methods, only the "gumboots" string check seems to do the trick. This string represents a product name in the table and proves that PHP is not neede ...

User verification and sign-ins - Bootstrap - Tabbed Navigation

My primary concern is security, but I also seek advice on the most efficient method: Initially, I created a website that loaded a new html/php file for each tab. However, I recently switched to using Bootstrap and nav-tabs to streamline the website and en ...

Tips for combining values with Reactive Forms

Is there a way to merge two values into a single label using Reactive Forms without utilizing ngModel binding? <label id="identificationCode" name="identificationCode" formControlName="lb ...

When the content within a dialog element exceeds the boundaries of the dialog, it will not be displayed

Issue Struggling with creating a dialog element that includes an x button for closing the <dialog> in the upper-right corner. My initial idea was to utilize absolute positioning and transforms to achieve this functionality, but encountered a setback ...

When the browser window is resized, the footer starts to cover the content

To prevent the divs from overlapping when the browser window is resized vertically, take a look at the code snippet below: <html> <body> <style> #box { display: flex; flex-direction ...

I am interested in displaying or hiding a table depending on a specific condition in MVC 4

Looking at this MVC 4 Razor code snippet: <h4>You have @Model.Count() items up for sale currently. @Html.ActionLink("Add a new listing by clicking here", "Create")</h4> <br /> <table style="visibility: hidden"> .... I am seeking ...

Ways to conceal a child div element without using any specific ID reference

I encountered an issue where I need to conceal all divs within a parent div except the first one. The challenge is that these divs do not possess any unique identifiers. Is there a way to achieve this task using CSS or pure JavaScript? <div role="list ...