Is there a way to make height: 100% adjust to accommodate other elements with specific pixel heights within the same div?

I am facing a challenge with a container that has a specified height and contains two nested divs. The first div has a height defined in pixels, and I want the second div to fill up the remaining space in the container, which is essentially 100% minus the height of the first div.

Is there a way to solve this issue without relying on JavaScript? While JavaScript can be used to adjust the container's height (which is how I ended up here), it seems like there should be a more fundamental solution without using JavaScript, as the problem could potentially cascade into other issues.

Example

http://jsfiddle.net/h3gsz/1/

HTML

<div id="container">
    <div id="top_content"></div>
    <div id="remaining_content"></div>
</div>

CSS

#container {
    width: 400px;
    height: 400px;
    border: 5px solid black;
}
#top_content {
    background-color: blue;
    width: 100%;
    height: 50px;
}
#remaining_content {
    background-color: red;
    width: 100%;
    height: 100%;
}

Edit

While an answer has been provided for the original fiddle, simplifying the question led to new issues being introduced in the solution: http://jsfiddle.net/h3gsz/6/

I removed the inline-block styling and a max-width value, which caused problems with the positioning and width of the remaining content. This resulted in an unnecessary horizontal scrollbar. I am contemplating whether to create a new question for this problem or not.

Answer №1

.container-box {
    width: 300px;
    height: 300px;
    border: 3px solid black;
    position: relative;
}
.top-section {
    background-color: green;
    width: 100%;
    height: 40px;
}
.lower-section {
    background-color: yellow;
    width: 100%;
    top: 40px;
    bottom: 0;
    position: absolute;
}

http://jsfiddle.net/h3gsz/7/

Answer №2

Have you thought about implementing overflow:hidden;?

#wrapper {
    width: 500px;
    height: 500px;
    border: 3px solid blue;
    overflow:hidden;
}

Check out the JSFiddle.

Answer №3

Why not just utilize the auto attribute?

http://jsfiddle.net/k6p9w/2/

CSS:

#wrapper {
  width: 500px;
  height: auto;
  border: 3px solid black;
}
#header {
  background-color: green;
  width: 100%;
  height: 70px;
}
#content {
  background-color: yellow;
  width: 100%;
  height: auto;
}

Answer №4

Another way to achieve this is by utilizing the display:table; property. Check out the example here

.main, .sidebar {
float: none;
padding: 20px;
vertical-align: top;
}

.container {
display: table;
}

.main {
width: 400px;
background-color: LightSlateGrey;
display: table-cell;
}

.sidebar {
width: 200px;
display: table-cell;
background-color: Tomato;
}

Answer №5

If you're looking for an alternative solution, consider the following approach:

#wrapper { 
width: 450px; 
height: 450px; 
border: 3px solid gray;
padding: 10px;
overflow: hidden;
}  
#header { 
background-color: orange;
width: 100%;
height: 50px; 
} 
#content { 
background-color: yellow; 
width: 100%; 
height: 100%; 
margin-bottom: 0;
} 

Depending on your specific requirements, you may opt to eliminate the #content <div>

HTML:

<div id="wrapper">
  <div id="header"></div>
</div>

CSS:

#wrapper {
  background-color: pink;
  width: 450px;
  height: auto;
  min-height: 450px;
  border: 3px solid gray;
  overflow: hidden;
  word-wrap: break-word;
}
#header {
  background-color: orange;
  width: 100%;
  height: 50px;
}

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

Issue with fixed header in Vuetify v-data-table not functional

While working with the Vuetify v-data-table component, I have enabled the fixed-header property. However, I have noticed that the table header is scrolling along with the table body. I am using the latest version of Chrome. Does anyone know how to addres ...

Analyzing HTML content (not properly formatted) using JSoup

When attempting to parse an HTML page using Jsoup, I encountered some unusual issues. The problematic page is located at , and it is not well-formed. This could potentially impact the parsing process. According to Chrome: /html/body/table[2]/tbody/tr/t ...

Creating CSS boxes in a dual column layout with a step-by-step approach

Currently, I am in the process of developing a user interface for a menu that looks something like this: https://i.sstatic.net/GuzRm.png I am exploring different implementation methods for this design. In this project, I am utilizing Bootstrap framework. ...

Text that is superimposed onto an image

Struggling to implement a hovering text over an image? I attempted to follow a tutorial, but couldn't adapt it to my project. While I managed to create a fixed overlay, I aim for a responsive solution that adjusts with resolution changes. My goal is t ...

What obstacles may hinder the loading of CSS with PHP variables?

Visit this website for agriculture updates: I have utilized PHP to generate specific color schemes and styles. It seems to function correctly on all tested desktop laptops, however, it fails to load the CSS files (only loads HTML) on a Galaxy Note runnin ...

Removing fragment identifier from the URL

If you go to stackoverflow.com/#_=_, the expression window.location.hash will display #_=_. All good so far. But when you run window.location.hash = '' to get rid of the hash, the URL changes to stackoverflow.com/#. (Note the unexpected # at the ...

What distinguishes the creation of HTML Emails from HTML Email Signatures?

In my opinion, I may be overthinking this but is there a distinction in how HTML Emails and HTML Email Signatures are constructed and displayed? It seems that when I search for HTML Email Signatures, results mainly focus on HTML Emails. Some results do tou ...

Styling Descendants Conditionally in Angular 2

.parent { color: blue; } .parent * { color: red; } .child { color: black; } .grandchild { color: green; } <div class="parent"> Parent <div>Child <div>GrandChild</div> </div> </div> Imagine a scenari ...

Having trouble with my JSFiddle code not functioning properly in a standard web browser for a Google Authenticator

Here is the code snippet that I am having trouble with: http://jsfiddle.net/sk1hg4h3/1/. It works fine in jsfiddle, but not in a normal browser. I have included the necessary elements in the head to call all the external files. <head> <scri ...

Change the size of the image to use as a background

I have an image that is 2000x2000 pixels in size. I originally believed that more pixels equated to better quality with less loss. Now, I am looking to display it on my browser at a resolution of 500x500. To achieve this, I attempted to set the image as t ...

My confusion is running high when it comes to navigating the mobile version of my website

Creating a navigation bar for my website has been challenging. Whenever I switch to mobile view, the list in my navigation bar shifts 40px to the right side of the screen where there's nothing. Is there any way you can assist me with this issue? ...

Use PHP to create a new JSON file on the server by submitting a form, then utilize a second form to update

My current goal is to create a json file using a form and then update that file with another form. Both of these processes are handled in the process.php file. I have managed to successfully update the json file if it is named as data.json initially in pro ...

The dividers flicker in and out of view

I have a menu with 7 elements, where clicking on an element causes its content to appear by fading in. If another element is clicked, the current content fades out and the new content fades in. I've successfully implemented this concept for 3 of the 7 ...

Unable to download the jQuery Plugin

I am looking to install a gallery without using flash, and I came across this jQuery plugin called Grid-A-Licious. However, I am having trouble figuring out how to install and use it since it is distributed as a .zip archive with two .js files but no index ...

Guide to positioning a toolbar within an element when hovering over it in HTML with the help of JQuery

function attachMenuBar() { $('body').on('mouseover mouseout', '*:not(.printToolBar)', function (e) { if (this === e.target) { (e.type === 'mouseover' ? setMenuBox(e.target) : removeMenuBox(e.t ...

When my webpage is opened in Firefox, I notice that it automatically scrolls down upon loading

I recently embarked on the task of building a website from scratch but ran into an unusual bug in Firefox. The issue causes the page to scroll down to the first div, completely bypassing its margin. I want to clarify that I am not seeking a solution spe ...

Adjust the color of the text as it scrolls by

As I work on developing my website using the Neve Theme on WordPress, I have encountered an issue with customizing the header block. I am using a plugin to set a background color for the header after scrolling 100px down the page, but this makes the text h ...

"Aligning two images side by side in a horizontal

[enter image description here][1]I am trying to place two images on my website, but I am struggling to vertically align them properly. I have attempted using line-height and vertical alignment, however, I cannot seem to pinpoint the issue or identify any m ...

Display div - conceal div - pause for 15 minutes - continue the cycle

I have a challenging JavaScript task that I've been struggling with for quite some time. The goal is to display a div for 5 seconds, hide it, wait for 15 minutes, then show it again for another 5 seconds, and continue this process in an infinite loop. ...

Is HTML5 Ajax Upload Degrading?

Recently, I came across a fascinating technique for uploading files using Ajax: http://www.sitepoint.com/html5-javascript-file-upload-progress-bar/ This method has piqued my interest and raised a couple of questions in my mind. Surprisingly, the tech ...