Adjust the height of the floating div to completely occupy the space below it

I have multiple DIVs within my content container, some floating to the right and others to the left on screens larger than 400px.

See working example here: https://jsfiddle.net/cmbpt1hd/

I need the height of area-three to automatically adjust so it fills all the space below it on screens wider than 400px.

Please consider the following:

  1. There may be several DIVs above area-three that float to the right.
  2. There may also be numerous DIVs inside the content div that float to the left.

A CSS-based solution is preferred.

My Code

#content {
  box-sizing: border-box;
  width: 100%;
}

.area-one {
  box-sizing: border-box;
  float: right;
  width: 30%;
  background-color: #ffa2a2;
  padding: 8px;
}

.area-two {
  box-sizing: border-box;
  float: left;
  width: 70%;
  background-color: #a4dca4;
  padding: 8px;
}

.area-three {
  box-sizing: border-box;
  float: right;
  width: 30%;
  background-color: #8282ff;
  padding: 8px;
}

#footer {
  clear: both;
  width: 100%;
  background-color: #ff8282;
}

@media only screen and (max-width: 400px) {
  .area-one,
  .area-two,
  .area-three {
    float: none;
    width: 100%;
  }
}
<div id="content">

  <div class="area-one">
    Area-one content goes here.
  </div>

  <div class="area-two">
    Area-two content goes here.
  </div>

  <div class="area-three">
    Area-three content goes here.
  </div>

</div>

<div id="footer">
  Footer content goes here.
</div>

Desired Outcome

https://i.sstatic.net/TZ2JD.jpg

Answer №1

jQuery solution:

function adjustHeight() {
  if ($(window).width() >= 400) {
    var area1 = $('.area-one').height();
    var area2 = $('.area-two').height();

    var height = area2 - area1;
    $('.area-three').css('minHeight', height);
  } else {
    $('.area-three').removeAttr('style');
  }
}

adjustHeight();
$(window).on('resize', function() {
  adjustHeight();
});
#content {
  box-sizing: border-box;
  width: 100%;
}

.area-one {
  box-sizing: border-box;
  float: right;
  width: 30%;
  background-color: #ffa2a2;
  padding: 8px;
}

.area-two {
  box-sizing: border-box;
  float: left;
  width: 70%;
  background-color: #a4dca4;
  padding: 8px;
}

.area-three {
  box-sizing: border-box;
  float: right;
  width: 30%;
  background-color: #8282ff;
  padding: 8px;
}

#footer {
  clear: both;
  width: 100%;
  background-color: #ff8282;
}

@media only screen and (max-width : 400px) {
  .area-one,
  .area-two,
  .area-three {
    float: none;
    width: 100%;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content">

  <div class="area-one">
    Area-one, Area-one.
  </div>

  <div class="area-two">
    Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two,
    Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two.
  </div>

  <div class="area-three">
    Area-three, Area-three.
  </div>

</div>

<div id="footer">
  Footer, Footer, Footer.
</div>

JSFiddle

Answer №2

Consider using flex-box and segmenting your HTML into left-side and right-side divs wrapped in their individual containers:

By wrapping your floating divs, you eliminate the need to specify width and float for each one, allowing for easier addition of new divs.

Flex-box is particularly useful for filling empty space or evenly distributing divs.

#content {
  box-sizing: border-box;
  width: 100%;
  display:flex;
}
.area {
  box-sizing:border-box;
  float:left;
  clear:left;
  width:100%;
  padding: 8px;
  flex-grow:1;
}
.area-one {
  background-color: #ffa2a2;
}
.area-two {
  background-color: #a4dca4;
  flex-grow:0;
}
.area-three {
  background-color: #8282ff;
}
#footer {
  clear: both;
  width: 100%;
  background-color: #ff8282;
}

.float-left {
  width:70%;
}

.float-right {
  width:30%;
}

.float-box {
  display:flex;
  float:left;
  flex-direction:column;
}

@media only screen and (max-width: 400px) {
  .float-left, .float-right {
    float: none;
    width: 100%;
    display: block;
  }
  #content {
    display:block;
  }
}
<div id="content">
  <div class="float-box float-left">
    <div class="area area-one">
      Area-one, Area-one. 
    </div>
  </div>

  <div class="float-box float-right">
    <div class="area area-two">
      Area-two, Area-two.
    </div>

    <div class="area area-three">
      Area-three, Area-three.
    </div>
  </div>

</div>

<div id="footer">
  Footer, Footer, Footer.
</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

What is the method to select a hyperlink that includes a variable in the "href" attribute and click on it?

Currently, I am in the process of creating acceptance tests utilizing Selenium and WebdriverIO. However, I have encountered a problem where I am unable to successfully click on a specific link. client.click('a[href=#admin/'+ transactionId + &apo ...

What are some solutions for troubleshooting a responsive image that functions correctly in Firefox but not in Chrome and Safari?

My row contains several img elements with height-capped sizes, displaying properly on Firefox but stretching on Chrome and Safari. Firefox Chrome Safari HTML: <div class="container-fluid text-center" style="padding-top: 15px"> <h6 class="ca ...

After making the request, $.getJSON initially comes back as undefined but eventually delivers

I found myself in a sticky situation. I was working on coding a Wikipedia search tool for a personal project, but encountered a small glitch. When a user types a word into the search bar, it gets stored in the data parameter of $.getJSON. The response then ...

Can a function be improved by including a sourceLocation property?

Building upon the discussion in this thread, I have a related query. When examining the properties of a function, we can see it has a name and a length among other things. Is there a way to add a new property to all functions by manipulating the construct ...

Tips for reducing the amount of white space on a card featuring a pie chart in a mobile layout

Is there a way to adjust the size of the pie chart or reduce the white space at the bottom? In the mobile view after inspecting with Chrome dev tools, it seems like the chart is not displaying as desired. Here is the screenshot <div ...

Transform shorthand CSS font properties into their longhand equivalents

When dealing with a font CSS string like the examples below: font:italic bold 12px/30px Georgia, serif; or font:12px verdana; I aim to convert it into its longhand format which would be: font-style: italic; font-weight: bold; My initial attempt can b ...

HTML Tooltip with Bootstrap 4

After creating a website using Bootstrap 4 and jQuery, I encountered an issue with HTML-styled tooltips. Instead of displaying the tooltips with the correct styling, they appear to be populated using the jQuery text() function, showing the HTML as plain te ...

Looking for a method to incorporate an onclick function into a column for renderCell within the MUI Datagrid. Any suggestions?

I'm currently developing a react application and utilizing the MUI Datagrid to present some data. I have incorporated a rendercell to insert an edit icon which should trigger a pop-up modal when clicked. Below is the column setup. export const specifi ...

From what source does the angular framework retrieve its 'data' information?

--- Consider this code snippet from a specific file --- angular.module('storyCtrl', ['storyService']) .controller('StoryController', function(Story, socketio) { var vm = this; Story.getStory() .success(func ...

Combine PHP, jQuery, and AJAX to retrieve multiple values simultaneously

I have been using jQuery AJAX for my projects. When I make an AJAX call to a PHP page, it normally returns a single value to the success function of AJAX. However, I am now looking to retrieve multiple data individually. How can I achieve this? This is th ...

Passing a JavaScript function as an argument

In the process of developing a JavaScript application, I am tasked with creating a handler that can accept a function as a parameter: Here is an example function: function myFunction() { alert("hello world"); } This is the handler implementation: func ...

Repetitive points detected in three.js typography

I converted a ttf font to a js font for three.js using Facetype.js, but I am encountering multiple duplicate point errors such as: THREE.Shape: Duplicate point 41.52:10.928 THREE.ShapeUtils: Unable to triangulate polygon! in triangulate() What steps ...

Bootstrap fixed top navbar with a double color scheme

I would like to replicate the following image: using a navbar-fixed-top with Bootstrap. Currently, I have added the following custom CSS: navbar-fixed-top.css .navbar { background-color: #8CB5A0; background-image: none; } Here is the Bootstrap ...

Angucomplete Alternative solves the challenge of accessing remote URLs

I have been using the Angucomplete Alt directive for creating an autocomplete feature. It has been working well so far, but now I want to customize a specific request to be sent to my server. <div angucomplete-alt id="input-name" ...

When attempting to preflight cross domain Ajax requests with a custom header using jQuery, the requests are consistently being terminated

I've been attempting to execute a cross domain POST request with custom headers, but the preflight is consistently being cancelled. I'm uncertain whether it's the browser or jQuery causing this cancellation as indicated in Chrome's "Ins ...

Error in MEAN Stack: Unable to access the property 'companyTitle' because it is undefined

I have established a MongoDB collection named joblist in my database. Additionally, I have developed a DB schema known as jobList.js. var mongoose = require('mongoose'); const joblistSchema = mongoose.Schema({ companyTitle: String, jobT ...

What is the best way to ensure that the maximum price is mandatory when entering a minimum price?

Essentially, the process works like this: if a person inputs a minimum price but forgets to input a maximum price, then presses search, it will not perform the search and a message will appear next to the max price field reminding them to enter a maximum ...

Tips for choosing a nested element within a div using CSS

I'm looking to target a specific child element within a div using CSS. Here's the HTML code I have: <div class='body'> <text>This is a text element</text> </div> Instead of adding a class or an ID, I want to ...

What is the best way to clear the values of multiple chosen-select boxes in one go?

I am encountering an issue while using a chosen-select plugin. I have multiple select boxes created with JavaScript code. My goal is to reset all select boxes when I change the class to "default-chosen", but instead of displaying my message "not chosen," i ...

Why isn't the router returning JSON data?

Why am I not receiving a response from this code? const express = require('express') const mongoose = require('mongoose') const authRouter = require('./authRouter') //importing the router const PORT = process.env.PORT || 3000 ...