The remaining visible portion of a viewport is equivalent to the height of an element

Is there a way to dynamically set a div's height so that it expands from its starting position to the end of its parent div, which is 100% of the viewport minus 20 pixels?

Here is an example of how you can achieve this using jQuery:

$(document).ready(function(){
  windowHeight = $(window).height() - 20;
  $('.child').css('min-height', windowHeight);
});

Answer №1

If I grasp your inquiry correctly, it seems you have a parent container with 100% height. Within this container, there is an element that is 20px in height, followed by another element that should expand to the bottom. It would be helpful if you could provide more clarity in your question or perhaps include a code snippet using jsfiddle and/or a screenshot.

Consider this straightforward illustration:

<div class="wrapper">
    <div class="header"> 
        Heading 
    </div>
    <div class="stretch">
        Stretching content
    </div>
</div>

Here are the styles to achieve what I believe you are looking for:

.wrapper {
    height: 100%;
}
.header {
    height: 20px;
}
.stretch {
    height: 100%;
    padding-top: 20px;
    margin-top: -20px;
}

The 20px of padding is included in the overall 100% height calculation. This means that the actual visible height of your .stretch element will be exactly 100% - 20px (as desired), but there will be a downward shift of 20px due to the padding. By applying a negative margin equal to the padding size, the element is brought back up by 20px, resulting in the expected height of 100% - 20px.

You can view the implementation in action (I added some color highlights for emphasis and adjusted the body and html height in the example since jsFiddle doesn't handle that automatically, which shouldn't be necessary in your case): http://jsfiddle.net/48aET/

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

What is the best way to display the legend on my PieChart?

I am attempting to display the names of colors in my PieChart as a legend. Here is the JSON data: { "choice": "20-29 yrs. old", "count": 4 }, { "choice": "30-39 yrs. old", "count": 2 }, { "ch ...

NodeJS Jest test failing due to a global variable being set for a different test already

I am currently working on a project in TypeScript using Node.js and Jest. I have a function that may or may not modify a global variable, which is a TypeScript Map. After one test modifies the global variable, it retains that value for subsequent tests. I ...

A comprehensive guide on using Lua Script in Node.js to efficiently insert multiple records into a Redis Hash

How can I efficiently insert multiple records into a Redis Hash using Lua Script in Node.js? I currently have the following code which utilizes multi and exec for inserting data. How can I modify it to use a lua script instead? return new Promise ...

The issue with session storage persisting even after closing the iframe

Encountering a persistent issue where the sessionStorage remains populated even after closing an iframe and opening another one with the same destination. I assumed that the sessionStorage would be reset and start afresh each time. The iframe is contained ...

getting the child td element within a tr using a variable that changes dynamically

I'm facing an issue where I am trying to access the <td> of a <tr> using the jQuery .eq() function. The code snippet below illustrates what I am attempting: var foundElement = 3; var values = $(this).children("td:nth-child('" + foun ...

How can jQuery be used to display the size and type of linked files in title attributes?

For instance: Prior to <a target="_blank" href="http://www.adobe.com/devnet/acrobat/pdfs/reader_overview.pdf"> Adobe Reader JavaScript specification </a> As the file is in PDF format, the title should read title="PDF, 93KB, opens in a new ...

Transforming an ordinary JavaScript object into a class instance

As I was delving into Angular's documentation on "Interacting with backend services using HTTP", I came across the following statement in the "Requesting a typed response" section: ...because the response is a plain object that cannot be automatical ...

Reactjs is having issues with Datatable functions

I am making use of the Datatable Library for creating tables easily. After fetching data with the Fetch API and rendering it to the table, everything seems to work smoothly. However, I am facing issues with DataTable Functions such as sorting, searching, ...

Ways to obtain the output of an If/Else statement

It seems like I might be missing something, but I am unsure of how to extract the result from an else-if statement. Take this code snippet that I've been working on for example: In this scenario, the output would read "It's warm!", and what I wa ...

Fetching data from ExpressionEngine using AJAX

Looking to display ExpressionEngine entries in a popup modal using jQuery and ajax. When the user clicks on an entry title, the full entry should be displayed in the modal. Additionally, next and previous buttons within the modal are desired for navigation ...

BottomNavigation wrapping React Router is failing to load the updated component

I'm struggling to understand why I can't seem to load the DMO page from a BottomNavigation component. Navigating to "/dom" works fine, but clicking the buttons doesn't do anything. The initial "/" loads perfectly. Any ide ...

What is the best way to modify a variable in a nested element using ng-click in the parent element?

Review this code snippet: <section class="page-section about-us" scroll-bookmark="about-us" ng-click="activeSection=true" ng-init="activeSection=false"> <div class="page-content sub-content active-section">{{activeSection}} < ...

Having a problem with the hover effect on the navbar component

Whenever I hover over the individual links, I notice that the color change doesn't extend all the way up. I have a feeling there is a more efficient way to achieve this than my current approach. Any assistance would be greatly appreciated! HTML: < ...

Experiencing a snag with implementing Firebase version 9 FCM in Next.js 12

I decided to incorporate push notifications into my Next.js (version 12) app, so I integrated Firebase Cloud Messaging. Here is the implementation: import { initializeApp, getApp, getApps } from "firebase/app" import { getMessaging, getToken } fr ...

What might be causing the excessive margins in my Bootstrap grid layout?

I've recently integrated Bootstrap into my Angular project using npm, but I'm facing an issue with excessive margins on the sides. Can anyone provide assistance? Below is the code snippet: <div class="container"> <div class="row"> ...

Converting an object with a combination of different index types into a string representation

I'm dealing with a unique object structure that behaves similarly to an array, except it contains a mix of index types (numbers and strings). Here's an example: var myObj = []; myObj[0] = 'a'; myObj[1] = 'b'; myObj[2] = &apos ...

Ways to detect when the window printing process has been completed

Within my application, I attempted to generate a voucher page for the user using the following code: var htm ="<div>Voucher Details</div>"; $('#divprint').html(htm); window.setTimeout('window.print()',2000); The &apo ...

Users are encountering timeout issues when attempting to connect to the Azure Postgres flexible database through the node.js server deployed on the Azure App Service

My node.js express server is deployed on Azure App Services, connecting to an Azure flexible Postgresql database. Strangely, everything works fine when running the server locally, but once it's deployed to Azure App Service, all requests time out: Th ...

Populate an HTML table using a JavaScript array containing objects

Greetings, fellow coders! I am new to the coding world and this community, and despite my efforts in searching for a solution, I couldn't find exactly what I was looking for. So, here is my question: I have an array structured as follows: const arr ...

Conceal content after a specific duration and display an alternative in its position, continuously repeating the loop

Imagine creating a captivating performance that unfolds right before your eyes. Picture this: as soon as the page loads, the initial text gracefully fades away after just three seconds. In its place, a mesmerizing animation reveals the second text, which ...