Create a div element that expands to occupy the remaining space of the screen's height

I am trying to adjust the min-height of content2 to be equal to the screen height minus the height of other divs.

In the current HTML/CSS setup provided below, the resulting outcome exceeds the screen height. How can I achieve my desired effect? The footer (copyright section) is nested within content2, hence I need the height of content2 to match that of the screen exactly.


                body {
                    margin:0;
                    border:0;
                    background-color:#24373B;
                    color: #fff;
                }

                #headerBG {
                    min-width:980px;
                    background-color:#000;
                }

                #header {
                    width:980px;
                }

                #content1 {
                    width:980px;
                }

                #content2 {
                    width:960px;
                    padding:10px;
                    margin-top:30px;
                    background-color:#355258;
                    min-height:100hv; /* Issue may lie here */
                }
              
<body>
                  <div id="headerBG">
                      <div id="header">header</div>
                  </div>
                  
                  <div id="content1">content1</div>
                  <div id="content2">content2</div>
               </body>

Answer №1

Don't forget to use 100vh instead of 100hv as the minimum height. Here's the corrected code:

#content2{
    width:960px;
    padding:10px;
    margin-top:30px;
    background-color:#355258;
    min-height: calc(100vh - 50px);
}

Prior to using vw or vh units, make sure to define them in your CSS:

html {
    width: 100%;
    height: 100%;
}
body {
    width: 100%;
    height: 100%;
}

Answer №2

Using CSS table layout instead of viewport units like (vw, vh) can help achieve the desired effect. By setting up a table structure, you can make the height of the pink area expand to fill out all the available space automatically.

Check out this example on jsfiddle: http://jsfiddle.net/qtu89zhm/

html, body {
    height: 100%;
    margin: 0;
}
.table {
    display: table;
    width: 100%;
    height: 100%;
}
.row {
    display: table-row;
}
.cell {
    display: table-cell;
}
.box {
    width: 500px;
    margin: 0 auto;
}
.content-2 .cell, .content-2 .box {
    height: 100%;
}
.header .cell {
    background: gray;
}
.header .box {
    background: teal;
}
.content-1 .box {
    background: gold;
}
.content-2 .box {
    background: pink;
}
<div class="table">
    <div class="row header">
        <div class="cell">
            <div class="box">header</div>
        </div>
    </div>
    <div class="row content-1">
        <div class="cell">
            <div class="box">content1</div>
        </div>
    </div>
    <div class="row content-2">
        <div class="cell">
            <div class="box">content2</div>
        </div>
    </div>
</div>

Answer №3

If I've grasped your question correctly...

body{
margin:0;
border:0;
background-color:#555555;
  height:100vh;
  width:100vw;
}
#headerBG{
min-width:980px;
background-color:#AAAAAA;
  height:10%;
}
#header{
width:980px;
  height:100%;
  width:10%;
    
background-color:#FFF;
}
#content1{
width:980px;
  height:40%;
background-color:#000000;
  margin-left:5%;
  width:90%;
}
#content2{
margin-top:30px;
background-color:#FFFFFF;
  margin-left:5%;
  width:90%;
  height:50%;
}
<body>
<div id="headerBG">
    <div id="header">
    </div>
</div>
<div id="content1">
</div>
<div id="content2">
</div>
</body>

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

Create an array of objects in JavaScript using JSON data

I have fetched a collection of dictionaries from my API: The data can be retrieved in JSON format like this: [{"2020-03-11 14:00:00":10.765736766809729} ,{"2020-03-11 15:00:00":10.788090128755387}, {"2020-03-11 16:00:00":10.70594897472582}, {"2020-03-11 1 ...

What is the process for including a new item in a JavaScript dictionary?

I'm currently learning JavaScript and I've encountered a challenge. I have a dictionary that I'd like to update whenever a button is clicked and the user enters some data in a prompt. However, for some reason, I am unable to successfully upd ...

What is the proper way to include jQuery script in HTML document?

I am facing an issue with the banners on my website. When viewed on mobile devices, the SWF banner does not show up. In this situation, I want to display an <img> tag instead, but the jQuery code is not functioning correctly. My template structure l ...

Show the chosen choice in a select dropdown with custom text using Angular

I successfully implemented this code snippet to display a dropdown list of countries and their phone codes. However, I encountered an issue where I need to only show the selected option's code without the country name. The first screenshot shows how i ...

What is the best way to retrieve an object from the state in my React application?

I have been struggling to locate an item from a collection so that I can update my react component. Despite the fact that the propertyState object is not empty and contains a list that I have successfully console logged, I keep receiving an undefined obj ...

Displaying the outcome of an HTML form submission on the current page

Within the navigation bar, I have included the following form code: <form id="form"> <p> <label for="textarea"></label> <textarea name="textarea" id="textarea" cols="100" rows="5"> ...

Tips for displaying an mp4 video using an HTML video tag when the video is uploaded through Multer

I am encountering an issue while trying to play a video on my website. The video is uploaded through multer using node.js and in the express framework. The file gets inserted into the database and assigned a specific folder with a filename like 1b47c24b20c ...

The importance of context visibility for functions in JavaScript within a React.js environment

Why is it that the react state is visible in the function handleFinishChange, but cannot be seen in validationFinishTime? Both are passed to the component InputFieldForm. When executing this code, an error of Uncaught TypeError: Cannot read property ' ...

Trouble with AJAX communicating with PHP and not rendering fresh data

I have created a row of images that are clickable, and once clicked, a variable is sent via AJAX to a PHP file for a database query. The PHP file receives the variable and executes the correct query. However, instead of updating the HTML on my page, it sim ...

Updating state in React with a nested promise within the useEffect hook

Trying to update the component state using React.useState with the help of useEffect. The reason behind using useEffect is that the API call response (EmployeeState > init()) determines what gets displayed on the UI. Component: import { EmployeeState } ...

Deciphering the evolution of APIs and managing internal API systems

I'm currently exploring the world of APIs and I have a few questions that are puzzling me. Question1: I understand that APIs facilitate communication between different applications. But why would a company need an API for internal use? For example, i ...

Is there a way I can obtain the code for a message box?

When I refer to a message box, I am talking about a container that gives users the ability to input their text and access different features like BOLD, ITALIC, color, justify, etc., in order to customize their message's appearance! (Think of it as the ...

What is the best way to attach to the beforeSubmit event of a dynamically loaded AJAX form in Yii?

Recently, I encountered an issue with dynamically loading a form via AJAX and attempting to bind the beforeSubmit event. Strangely, the event didn't seem to work as expected, causing the form to submit and the browser to navigate to a new page. This b ...

Strengthening JavaScript Security

Throughout the past few years, I have delved into various javascript libraries like Raphael.js and D3, experimenting with animations sourced from different corners of the internet for my own learning. I've obtained code snippets from Git repositories ...

Determine the variable height of a div containing dynamic content in order to execute a transform

Looking to create a dynamic container that expands when a button/link is clicked? The challenge lies in achieving this expansion effect with a smooth transition. By using the height: auto property, the container can expand to fit its content, but incorpora ...

What is the best way to showcase my products in a horizontal line? I am seeking a solution using PHP and Bootstrap to exhibit all the items retrieved

I am experiencing an issue with displaying the most recent events from my database using PHP. The Bootstrap framework and CSS are causing them to appear as a single column on the page, but I would like them to be displayed in rows of three. I have attempt ...

What steps should be taken to trigger an API call once 3 characters have been entered into a field

In my current project, I am dealing with a parent and child component setup. The child component includes an input field that will emit the user-entered value to the parent component using the following syntax: <parent-component (sendInputValue)="g ...

What is the best way to access an Ajax response outside of its function using JQuery?

Just starting out with JQuery and wondering how to utilize the Ajax response ( HTTP GET ) outside of the function in my code snippet below: $.ajax ({ type: "GET", url: "/api", success: success, error: error ...

Dividing JSON File Entries Among Several Files

I am facing a challenge with a file that contains an overwhelming amount of data objects in JSON format. The structure is as follows: { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {}, "geometry": ...

Tips for keeping a reading item in place while scrolling

To enhance the user experience, I would like to improve readability without affecting the scroll position. Question: Is there a way to fix the scrolling item at a specific position (item with the .active class)? I am looking to maintain a consistent read ...