Ensuring CSS Fills Div Height to Match Page Height

I'm trying to make the bottom div fill the remaining height of the page, not just its parent div

body, html {
  margin: 0px;
}

.top {
  background-color: green;
  width: 100px;
  height: 50px;
}

.bottom {
  background-color: red;
  width: 100px;
  height: 100%;
}
<div class="top"></div>
<div class="bottom"></div>

Answer №1

To find the page's 100% height minus the top DIV's 50px, you can utilize the 'calc' function with 'vh'.

Here is an example snippet to illustrate this:

body, html {
  margin: 0px;
}

.top {
  background-color: green;
  width: 100px;
  height: 50px;
}

.bottom {
  background-color: red;
  width: 100px;
  height: calc(100vh - 50px);
}
<div class="top"></div>
<div class="bottom"></div>

Answer №2

Feel free to test out the code snippet provided below, but please be cautious as it contains CSS3 which may not be fully supported by all browsers. For compatibility information, you can refer to this link.

If needed, an alternative approach is to use Javascript, which ensures support across most mainstream browsers.

body, html {
  height: 100%;
  margin: 0px;
}

.top {
  background-color: green;
  width: 100px;
  height: 50px;
}

.bottom {
  background-color: red;
  width: 100px;
  height: 100%;
  height: -webkit-calc(100% - 50px);
  height: calc(100% - 50px);
}
<div class="top"></div>
<div class="bottom"></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

Displaying only icons in a Bootstrap 4 dropdown menu using jQuery

I am looking to design a unique Web Icon Font Dropdown Menu where the associated text is displayed next to the Flag Icons only when the menu is open. Once a selection is made and the menu is closed, I want to show only the Active Flag Icon without any acc ...

Drag and Drop: Unable to Return Item to Its Original Position

I'm facing an issue with an HTML code that Drag Drop feature, everything works fine except for dragging it back to its original position. Below is the snippet of the HTML code: function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ...

The custom loading screen is obscuring just a portion of the scene

I am having an issue with my custom loading screen. When I try to display it, it only covers half of the scene: Image while loading Here is the code I am using for displaying the loading screen: BABYLON.DefaultLoadingScreen.prototype.displayLoadingUI = ...

Exploring Vue and Webpack's tree shaking functionality, side effects management, and its impact on CSS: Loading unused component CSS

We are currently grappling with the challenge of effectively implementing tree shaking for CSS within Vue Single File Components using Webpack. Within our package.json file, we have specified: "sideEffects": ["*.css", "*.less","*.vue"], which appears to b ...

Display the total count of textboxes only once following the entry of a new value into the textbox

<html> <title> </title> <head> <link rel="stylesheet" href="new_2.css" type="text/css"/> <script src="jquery-1.10.2.min.js"></script> <script> function GeneratingTextFields() { var NumberOfText = $("#Num ...

Prevent table content from appearing above the 'sticky' header

I am facing an issue with a single table that has a fixed header and scrolling body. The problem arises when the body extends beyond the header while scrolling. I have provided a zoomed-in clip of the problem. Can anyone offer a solution for this without u ...

Is there a way for me to recognize when the Cancel Button is clicked on the Select File Dialog for an <input type="file" /> element?

When managing file uploads on a website, utilizing a hidden <input type="file" /> element is necessary. To identify which file has been chosen in the Select File Dialog, we can utilize the onchange event. However, how do we detect if the ...

What is the best way to adjust the font size in a Vuetify component?

I am using v-text-field and v-select components along with vue-loader in my project. However, I encountered an issue when trying to change the font-size - it did not work as expected. Can someone please guide me on how to successfully change the font-size? ...

Is there a way to eliminate the :target pseudo-class from the entirety of a page using just HTML and CSS?

For an assignment, I have created a menu with a list of items positioned on the left side of the screen. The requirement is that when a menu item is clicked, it should move outside the menu using only CSS/HTML. I managed to achieve this by utilizing the : ...

What is the reason behind the persistent presence of the caret in my dropdown button?

Is there a way to remove the caret next to a chat icon button in a dropdown menu? I am using Bootstrap 4 and have tried this: .message-notification-chat-icon-mobile .dropdown-toggle::after { display: none; } <link rel="stylesheet" href="https://cdn ...

What is the best way to incorporate a button in my code that automatically triggers changes at regular intervals?

Is there a way to automatically change the color of my working traffic light in JavaScript on a timed basis, rather than relying solely on button clicks? Here is the current code I am using: <!DOCTYPE html> <html> <head> <style> # ...

Unusual actions observed when using CSS pseudo selector :checked

I have implemented a unique CSS solution to showcase a five-star rating system on my website. The method I followed can be found at . While this technique works flawlessly in some sections of my website, it strangely fails in others. Could this be due to c ...

Inspect every div and perform an action if the criteria are met

I need assistance with adding text inside a specific div based on certain conditions. Currently, all the div elements are being populated with the added text. Please review my code for more clarity on what I am trying to achieve. Can you suggest the most e ...

Having trouble with the "foreach" binding in knockout where the "html" binding buttons are not functioning properly in Javascript

I'm experiencing an issue with the following code: <html lang="en"> <head> <link href="css/bootstrap.min.css" rel="stylesheet"> </head> <body> <script type='text/javascript' src='knockout-3. ...

Sending attachments via email without saving them on the server in PHP Mailer

I need help with my PHP mailer code. I want to send an attachment without saving it on the server. Can someone guide me on how to achieve this? $mail = new PHPMailer(); $mail->IsSMTP(); // set mailer to use SMTP $mail->Host = "localh ...

Implementing a sticky second Navbar with Bootstrap 5 and Vanilla JavaScript to stay fixed at the top during scrolling

I am currently working on a project that involves two navbars. The first navbar is dedicated to contact information, while the second navbar contains menu links. My goal is to have the second navbar fixed at the top of the page when scrolling, and I am lo ...

I've exhausted all options from stackoverflow with no success. Google Chrome's Autofill feature is wreaking havoc on my theme. Any suggestions on how to tackle this issue?

I designed a template with a stunning CSS layout. All I want is to have a transparent background with white font color, but Google Chrome seems to be causing issues with my theme. What steps should I take? I've exhausted all resources available onlin ...

Difficulty Aligning Header Elements - Combining Navigation and Logo on the Same Line

I am currently facing an issue with positioning a logo and navigation links within a <header> element. When I apply the CSS property text-align:center;, the logo aligns to the center on one line while the navigation links appear on another line direc ...

Loading CSS and JS files in a modular manner

Each time I add a new HTML file to my website, I find myself repeating the process of including various CSS and JavaScript files: <!-- Source for .css style rules --> <link rel="stylesheet" type="text/css" href="/Hexdra/assets/css/style.css" ...

Serving both HTML and JSON responses concurrently using PHP

Consider a scenario where a webpage contains elements generated in HTML directly from the server backend and others generated through JavaScript with data sourced from JSON. Upon initially loading the page, the HTML layout and its elements are delivered in ...