JavaScript may not display height as anticipated

Currently, I am experimenting with JavaScript in order to achieve a website section that is 100% the height of the screen minus the "nav" bar.

Imagine it as having two visible components - the first one being large and the second one small at the bottom.

The CSS code for this is:

#mainHeader{
     height: 100vh;
}

#nav{
     height 90px;
}

I have implemented this functionality using JavaScript/jQuery and it kicks in during the body's onload event.

<body onload = "function()"> </body>

var mainHeaderHeight = $("#mainHeader").outerHeight(true) - $("nav").outerHeight(true);
$("#mainHeader").css("height",mainHeaderHeight + "px");

This results in the following visual appearance:

Answer №1

To avoid the hassle of using jQuery, you have an alternative option available in CSS3 called calc (especially since you are already utilizing the relatively "modern" vh):

#mainHeader {
    height: calc(100vh - 90px);
}

Answer №2

Make sure to target the #nav element in your CSS instead of the nav element.

var mainHeaderHeight = $("#mainHeader").outerHeight(true) - $("#nav").outerHeight(true);
$("#mainHeader").css("height",mainHeaderHeight + "px");
html, body {
  margin: 0;
  padding: 0;
}

#mainHeader{
  height: 100vh;
  margin: 0;
  padding: 0;
}

#nav{
  height 90px;
  margin: 0;
  padding: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="mainHeader">main</div>
<div id="nav">nav</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

Updating the configuration settings for CKEditor 5 using Reactjs

I followed the configuration provided in another answer at But, I am facing an issue: Failed to compile. ./src/components/cards/CreateCard.js Line 59:22: Parsing error: Unexpected token, expected "}" 57 | editor={ClassicEditor} 58 | ...

Experimenting with combining a CSS class alongside the Nth-child selector

Hello, I've been researching various sources including Stackoverflow on how to effectively use an Nth child selector and a Class together but have not had much success so far. In essence, my menu consists of Main categories (class = cat) and subcateg ...

Error message: "Reactjs - TypeError: The property 'map' cannot be read as it is undefined in the table"

I have encountered an issue while using the material-ui table. I am able to map the items and display them successfully, but when trying to map the orders.items in the table, I get the following error: TypeError: Cannot read property 'map' of u ...

What is the best way to extract multiple variables from a function in JavaScript?

After creating the function buttonEffects() with four different button effects, I am now facing the challenge of bringing these variables outside of the function and using them in another function. Is it possible to achieve this without recoding everything ...

Display data fetched from concurrent Ajax calls in a React Component

I am currently in the process of converting my original project to Reactjs. Since I am new to this and it's my first application, I could use some guidance on how to effectively implement this in a React-friendly way and enhance both my skills and the ...

Having Multiple File Inputs [type=file] in one webpage

Is it possible to have two separate inputs for uploading images, each setting a different background image in a div? The second file input is: <input type='file' id='getval' name="logo" /> I want this file to be set as the back ...

Creating a vertical navigation menu and displaying its submenu on corresponding pages in a horizontal layout

I'm struggling to design a navigation layout for my website where I want to display the main menu items like this: Menu 1 Menu 2 Menu 3 Menu 4 Menu 5 and have their respective sub-menus displayed horizontally on their main menu pages like this: ...

"Waiting for results with Node.js and Mongo DB is like trying to catch a

I am currently working on developing a Web Api using NodeJs and MongoDB. One issue I have encountered is that my await statements are not being awaited, leading to unexpected behavior in my code. Code async find_nearby_places(lng, lat, tag, maxDistanc ...

CakePHP includes built-in jQuery pagination and sorting functionality

Does CakePHP have built-in jQuery pagination and sorting functionality similar to regular jQuery? I have searched online but couldn't find relevant information. Can someone please point me in the right direction? Thank you, Himanshu Sharma ...

What is the method to retrieve the class name of an element when the div is created as '<div id 'one' class="element one"></div>'?

I have three elements named one, two, and three, declared as seen below: <div id =container> <div id 'one' class="element one"></div> <div id 'two' class="element two"></div> < ...

Access exclusive RSS feeds from Twitter

I am facing a challenge in showcasing the most recent 3 tweets from Twitter on my client's website. The tweets are marked as private, making it difficult to access them. How can I pass the username and password to retrieve these latest tweets? Is it p ...

Navigating into iframe can be tricky when the previous iframe is not properly closed in Selenium Webdriver. Here

One issue we are facing is the excessive use of iframes in HTML. It has become troublesome to locate elements as some are buried within multiple layers of iframes. I'm trying to access an element inside the outermost iframe (the second one), but I&ap ...

Pressing the tab key while using Angular will halt any additional processes from running

I recently integrated a search bar into my Angular application. When I enter text in the input field, a customized dropdown menu appears below it. However, when I press the tab key, I expect the dropdown to close and for the focus to shift to the next inpu ...

Showcasing pictures on ReactJS

I am currently working on developing an application to showcase images on my React webpage. These images have already been uploaded to a local folder. In order to integrate my React application with a NodeJS server and MongoDB, I connected the two. My goa ...

Condition has been fulfilled for both ngShow and ngHide

I am attempting to show a loading icon while data is being loaded, and then display the data once it is ready. However, I am encountering a problem where for a brief moment, both the loading icon and the data are visible... Below is my code snippet: $scop ...

Combining ASP.NET MVC with HTML5 for dynamic web development

We are in the process of planning a new project and are interested in utilizing ASP.NET with the MVC pattern. Additionally, we want to incorporate the new features of HTML5. However, we have been having trouble finding sufficient information or connecting ...

Unlock the secret: Using Javascript and Protractor to uncover the elusive "hidden" style attribute

My website has a search feature that displays a warning message when invalid data, such as special characters, is used in the search. Upon loading the page, the CSS initially loads like this: <div class="searchError" id="isearchError" style="display: ...

Having Trouble with Jquery UI Date Picker?

I have included my form code below. <div class="threeleft second" id='displayallformshow' style="float: none;width: 80%;padding:10px;"> <form action="investor_submit.php"> <div class='formdiv'> ...

Explaining the structure of a nested object within a TypeScript declaration file

As I work on my project, I encounter the challenge of importing an object created by a dynamic function. The dynamic nature of the keys on this object poses a problem for the IDE, as it cannot determine what keys are present based on the provided config. T ...

The image loads successfully from an AJAX request in CodePen, but fails to load in the local

I am currently working on integrating a weather API into my basic website and I would like to incorporate the weather icons as well. The API request successfully works in both my local and live environments, however, I encounter an error with the icon spec ...