Issue with child div not properly fitting inside parent div

I am working with three different divs in my code.

There is one parent div and two child divs.

The styling for the parent div looks like this:

#parent {
  overflow:auto;
  margin: 0 auto;
  margin-top:37px;
  min-height: 100%;
  width:875px;
}

Now, let's take a look at the styles for the two child divs:

#child1 {
  overflow:auto;
  min-height:150px;
  border-bottom:1px solid #bbb;
  background-color:#eee;
  opacity:0.4;
}

#child2 {
  height:100%;
  background-color:white;
}

While the parent div extends to fill 100% of the page height, I've noticed that child2 does not extend all the way down to the bottom of the page like the parent div does.

Answer №1

When it comes to setting the height of an element, things may not work as you expect. The percentage specified in height: 100% is calculated by referencing the first parent element with a defined height and either absolute or relative positioning.

There's a workaround for the body tag where some browsers may behave as desired when setting its height to 100% of the page. However, this method may not work if you nest elements deeper within the structure.

If you want to stretch a div to the bottom of the page using absolute positioning without the need for JavaScript, here's an approach that is quite cross-browser compliant:

<div id="parent">
   <div id="childNorm"></div>
   <div id="childStrech"></div>
</div>

#parent
  {
      position: absolute;
      width: 1000px;
      bottom: 0;
      top: 0;
      margin: auto;
      background-color: black;
  }

  #childNorm
  {
     position: absolute;
     width: 1000px;
     top: 0;
     height: 50px;
     background-color: blue;
     color: white;
  }

  #childStrech
  {
     position: absolute;
     width: 1000px;
     top: 50px;
     bottom: 0;
     background-color: red;
     color: white;
  }

To see a demonstration, check out this Jsfiddle link: http://jsfiddle.net/t7ZpX/

The key trick here is using absolute positioning and adding bottom: 0; to make the element stretch to the bottom of the page. Just be mindful of how you position the elements accordingly.

Answer №2

Indeed, one of the frustrating quirks in CSS is how min-height is not treated as actual "height" when calculating other styles. Check out this example at http://jsfiddle.net/3raLu/3/. To make a child element full height, you may need to apply height: 100% to its parent div. Alternatively, if it can be absolutely positioned, another solution is shown here: http://jsfiddle.net/3raLu/6/.

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

You are unable to move the image to the top of the screen after zooming it with CSS

I implemented an image viewer component with interactive buttons for rotating, zooming in, and zooming out. Upon clicking a button, CSS transform is applied to the image. However, I encountered an issue where, when zooming the image, it cannot be scrolled ...

What is the best way to update the image card in bootstrap?

Could someone please assist me in changing the image displayed? I keep getting the same copied image, but I would like id 1 to have a different image. This is crucial as I plan to present this for my defense in class. Thank you. let htmlString = ` <di ...

The JQuery loading symbol does not prevent keyboard interactions

I successfully implemented a Jquery busy indicator in my application using the following link: However, I noticed that even though it prevents users from clicking on input elements while loading, I can still navigate to these elements by pressing the tab ...

When using Angular JS, you can easily display a message and refresh the page with just one click

Is there a way to automatically refresh the page after displaying a message or alert indicating "Successful" or the opposite? How can I make this work? I attempted to refresh the code but the message does not appear afterwards. HTML CODE: <div class= ...

JQuery Powered Text Analyzer

Our team is in the process of developing a text analyzer tool to review emails before sending them out. The goal is to involve all team members in selecting the best emails to send to clients: Implemented the following HTML code: <p class="sentence" ...

What is the reason for index.html requesting the css or js modules as if they were directories when loading?

I am currently developing a server using expressjs: 'use strict'; var express = require('express'); var logger = require('morgan'); var path = require('path'); var bodyParser = require('body-parser'); va ...

Retrieving checkbox values from a MySQL database in HTML/PHP

My database contains data stored in varchar format, such as S,M,L,XL. On my PHP page, I have checkboxes corresponding to these options. Is it feasible to fetch this data from the database and display the pre-checked checkboxes in HTML using PHP? (For exa ...

Vertically align the left div in the center, while maintaining the standard alignment of the right div

I have been experimenting with Bootstrap 4 and attempting to implement the Vertical alignment feature as outlined on the webpage: https://getbootstrap.com/docs/4.0/layout/grid/ Despite reviewing my code multiple times, I am unable to identify the mistake ...

Developing a webpage linked to a multimedia library

My family has requested that I take on the task of organizing our 'family memories' by creating an access page (which I envision as a web page) that will link to our photos and videos. Each item will be 'tagged' with information such as ...

Troubleshooting Image Map Failure on Internet Explorer 10

<img src="images/imagemap.png" width="600" height="100" border="0" usemap="#map" /> <map name="map"> <!-- #$-:Image map file created by GIMP Image Map plug-in --> <!-- #$-:GIMP Image Map plug-in by Maurits Rijk --> <!-- #$-:Plea ...

Guide on how to incorporate an external javascript file into an HTML button

I am currently working on an exercise through Udacity that involves creating an HTML form with JavaScript functions. The task is to input text into the form, submit it, and have it displayed on the webpage within a blue square. This exercise is designed to ...

Angular Floating Panels: A Guide to Creating Dynamic User Interfaces

In the process of developing an angular app, I require a dock-able panel. After researching available controls online, I found them difficult to use and they compromised our screen layout. As a result, I am considering building a custom dock panel from s ...

"Efficiently setting up individual select functions for each option in a UI select menu

I've integrated UI Selectmenu into my current project UI selectmenu includes a select option that allows for setting select behavior across all selectmenu options, as shown in the code snippet below: $('.anything'). selectmenu({ ...

What could be causing the whitespace around this element when using SASS from Refills / Bourbon.io?

I've recently started using the impressive SASS framework, Boubon.io created by the talented team at thoughtbot. As I experiment with their supplied templates (known as Refills), I'm finding that Bourbon.io serves as a versatile alternative to po ...

No mouse cursor activity while in Pointer Lock mode

Using Pointer Lock for capturing the cursor in a game being developed in JavaScript with three.js has been quite interesting. Despite extensive online research, the reason why the cursor doesn't seem to move on Chrome OS remains elusive. A working exa ...

Is there a way to navigate to a specific <li> element using scrolling?

Is there a way to direct a user to a URL and have it automatically scroll to a specific <li> element? For example, navigating to mysite.com/something.html#someItem should take the user directly to: Something here ...

I am encountering an issue where I am unable to access properties of null while trying to read the 'pulsate' function within the

The current version of my material-ui library is as follows: "@mui/icons-material": "^5.5.1", "@mui/material": "^5.5.1", This is how I included the Button component : import Button from "@mui/material/Button&qu ...

Allow all images on the webpage to be easily dragged and dropped into a designated upload section

I am in the process of developing a browser extension that allows users to save images from web pages into their favorites, similar to Pinterest. The functionality involves clicking on the extension icon which adds a special field to the HTML where users c ...

Styling definition lists and background containers with CSS

Currently, I am attempting to enclose a series of elements within a box and showcase some text beneath it: <div class="ft_models"> <dl> <dt>Models:</dt> <dd> <div>Item 1</div> ...

Verify the validity of a form using JavaScript and by utilizing HTML5 validation, then proceed to save the form data in local storage

I'm fairly new to working with JavaScript and jQuery. Currently, I am attempting to validate the HTML5 validation of a form using JavaScript. When the form is valid, I want to save the data in localstorage WITHOUT having to reload the webpage. Here is ...