Ensure that the nested div expands to the full height of the parent container div

I need my container div to expand in height to at least the full window height. If there is a lot of content inside the container div, I want it to exceed the window's height. This feature is working properly.

Within the container div, I have placed an image followed by a second nested div below it. My goal is to make this nested div stretch vertically to occupy the remaining space within the container div. Unfortunately, this aspect is not functioning as expected.

To visualize the issue, you can refer to this illustration. Ideally, I want the div to expand like the left side of the example on the right image, rather than how it appears on the right side of the same image.

Below is the code snippet that demonstrates my attempts so far. As shown here, I haven't been successful in making the div under the image expand vertically, despite trying different methods. Setting "Height: 100%" did not yield the desired outcome, especially since I have defined "100%" to represent the default window height to ensure the container div stretches accordingly.

* {
  margin: 0;
  padding: 0;
}

body {
  overflow-y: scroll;
  overflow-x: hidden;
}

body,
html {
  height: 100%;
}

.container {
  display: block;
  overflow: auto;
  min-height: 100%;
  height: 100%;
  height: auto;
}

.bottom {
  width: 100%;
}
<div class="container">

  <img src="image.jpg" width="100%">

  <div class="bottom">
    LOREM IPSUM
  </div>

</div>

Answer №1

After dedicating a significant amount of time to unraveling the puzzle, I am thrilled to announce that I have cracked it!

The moment of enlightenment came when I stumbled upon other inquiries from individuals wondering "how can I achieve this without relying on tables?" It dawned on me that although table layouts might offer an easy solution, there is a more elegant alternative through the utilization of display:table.

An enlightening blog post elaborates on how employing display:table allows for table-like layouts without the cumbersome markup associated with traditional HTML table structures, enabling the creation of semantic code and versatile layouts tailored to different media queries.

I did find myself needing to introduce one modification to the markup: enclosing the image within a <div>. Additionally, managing max/min heights can be perplexing when dealing with table displays: height settings are interpreted as preferred heights based on the constraints of parent and child elements. Consequently, by setting a height of zero on a display:table-row wrapper div, I was able to ensure the row conformed precisely to the dimensions of the content image. Similarly, specifying a height of 100% on the content div allowed it to seamlessly fill the space between the image and the minimum height of the parent container.

Behold!

In essence, here's the condensed essential code:

body {
  overflow-y: scroll;
  overflow-x: hidden;
}

body,
html {
  height: 100%;
}

.container {
  display: table;
  width: 100% height: 100%;
  /* this will be treated as a Minimum! It will stretch to fit content */
}

div.wrapper {
  display: table-row;
  height: 0px;
  /* take as little as possible, while still fitting content */
}

img {
  display: table-cell;
  width: 100%;
  /*scale to fit*/
}

.bottom {
  display: table-cell;
  height: 100%;
  /* take as much as possible */
}
<div class="container">
  <div class="wrapper">
    <img src="http://placekitten.com/600/250" />
  </div>
  <div class="bottom" contentEditable="true">
  </div>

This method functions smoothly in Firefox 25, Chrome 32, Opera 18, and IE10. While compatibility issues arise in IE 8, it remains functional albeit lacking full responsiveness. If you conduct tests on Safari, IE9, or mobile browsers, feel free to share your observations.

Answer №2

Flex boxes are a great feature, but their widespread support is still pending.

An improvement in the specification would be to allow percentage values for child object's min-height when min-height is specified on the parent element. Currently, the parent height value needs to be explicitly set for child height percentages to work properly.

In the meantime, an alternative approach is to use "faux columns" as described here. This technique involves painting the background of the parent element to create the illusion that the child element's background extends to full height.

The linked example demonstrates this using a background image, although a background gradient can also be used with some browser compatibility limitations. Despite these limitations, the functionality remains the same.

Check out an example here:
http://fiddle.jshell.net/y6ZwR/3/

Answer №3

Utilizing flexbox is a solid alternative to using JavaScript for layout purposes. The specific implementation may vary depending on various factors, but you can experiment with different options at .

Polyfills are available, however, the choice of which one to use will depend on the browsers you are aiming to support.

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

BeautifulSoup is throwing an AttributeError because it cannot find the attribute 'h3' in the list object

I am a newcomer to the world of Web-scraping and I am currently working my way through this tutorial ( ) in order to collect movie data. However, I believe I have made a mistake in defining the variable "first_movie". This is the code I have: from reque ...

Ensuring uniformity in picture size while uploading images in Angular by resizing and including white borders

Currently, I am working with pictograms and I am looking to showcase all the pictograms that the user has in both grid list and grid tiles formats. However, the challenge lies in the fact that the aspect ratio of the pictograms varies significantly depen ...

Tips for creating a MaterialUI list that wraps to the next line?

import React from 'react'; import { List, ListItem, } from '@material-ui/core'; import { makeStyles, createStyles, } from '@material-ui/core/styles'; import clsx from 'clsx'; import VideoCard from './VideoCa ...

The JSON data fails to load upon the initial page load

I am having trouble getting JSON data to display in JavaScript. Currently, the data only shows up after I refresh the page. Below is the code I am using: $(document).ready(function () { $.ajax({ url:"http://192.168.0.105/stratagic-json/pr ...

Guide to changing the checkbox value using JavaScript

Describing the Parent Element: <span style="background: yellow; padding: 50px;" onClick="setCheckBox()"> <span> </span> <input type="checkbox" name="f01" value="100"> </span> ...

Refreshing ng-model within a radio input

Currently, I am utilizing a jQuery library for radio/checkbox components. Although I am aware that combining jQuery with Angular is not ideal, the decision to use this particular library was out of my control and cannot be altered. One issue I have encount ...

ui-router: Issue with ui-sref causing font color to remain unchanged

I've been trying to customize the font color of a navigation link using ui-router's ui-sref functionality, but I'm facing an issue. While I can change the background color by adding it to the .active class in my CSS, the font color remains u ...

Turn off automatic HTML correction in Google Chrome

I'm currently developing a NODE.js app that utilizes Nunjucks as its view engine. To ensure the validity of the HTML being generated, I've implemented tests to check if all tags are properly closed. My approach involves spinning up the applicatio ...

A straightforward development and production build to incorporate HTTPS for a static website created with React and Express

Is there a straightforward method to create a static web page and Node backend where the Node server runs in HTTPS during development but not in production? How can the static web page point to https://localhost/foo in dev mode, and simply /foo in producti ...

The curious conduct of wild safari creatures

I have been using JavaScript to split my ePub chapter into pages by wrapping the code with new divs that represent separate pages. I have also created CSS styles for these new divs. Everything works perfectly when the file has a filename extension of &apos ...

HTTP request form

I'm currently working on a form that utilizes XMLHttpRequest, and I've encountered an issue: Upon form submission, if the response is 0 (string), the message displayed in the #output section is "Something went wrong..." (which is correct); Howe ...

Encase every picture within a div and add a numerical label in front of it

I'm working on a function that will wrap each image in a div and add a span label for each image. Here is the code I have written so far: $('#carousel img').each(function(index) { $(this).wrap('<div class="image"></div>& ...

Struggling to understand how to assign a specific color to individual divs within a loop

Featured on the main page of my website are 3 posts, each accompanied by a div link below. I want each of these 3 divs to have a distinct color. Any new posts within this category will appear here as they are published, but only the latest three will be vi ...

Using a media query in conjunction with an image map

Can anyone help me figure out why this media query isn't working as expected? I am attempting to remove the top 150px of an image map if the browser width is larger than 1300 pixels. The image map itself is responsive and expands with the browser widt ...

How can a dropdown menu appear above other content on a webpage?

My latest work task has me scratching my head. I've been asked to tweak these category dropdown menus at the top of the page so that when they slide down, the rest of the page shifts along with them to accommodate the change. It seems like a rather od ...

Ways to determine the number of overlapping elements and adjust their widths

My layout conundrum involves 3 buttons within a flexbox placed in a single table cell. I am trying to adjust the width of each button so that they collectively fill the horizontal space as much as possible. The issue arises when the buttons overlap and the ...

What is the best way to switch between search results shown in an li using AngularJS?

Looking for a way to toggle a list that appears when a user searches in my app. I want the search results to hide when the search bar is closed. How can I achieve this? I think Angular might be the solution, but I'm stuck on how to implement it. I tri ...

Issues with click events in the navigation menu

How can I make my menu close when clicking on other parts of my website, instead of opening? I know that I should use a click event for this, but when I implemented a click event, my menu encountered 2 unwanted problems: 1- Whenever I clicked on a menu i ...

How to make a routerLink clickable within an anchor tag in Angular 6

I am facing a peculiar issue with a group of hyperlinks. html <div class="col-12 navlinks"> <div class="text-center"> <div class="justify-content-center"> <a class="col-auto d-inline-block whitelink" rou ...

Playing with background hues

Can anyone help me with this issue? I've been attempting to change the background color using jQuery upon document load, but it doesn't seem to be working. Any thoughts on what I might be doing wrong? http://jsfiddle.net/KczZd/2/ HTML: <d ...