I desire for the body to utilize the full height of the screen

My website consists of a header div and a body div. I am trying to make the body div expand to fill the entire screen height.

When I set the height to 100vh, it expands correctly but it does not take into account the height of the header div, resulting in a scrollbar appearing. I do not want this scrollbar to be visible.

html {
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
}

.header {
  position: relative;
}

.body {
  position: relative;
  top: 0px;
  bottom: 1rem;
  flex-grow: 1;
}


/* for visualization purpose only */

.header {
  background: blue;
  color: white;
}

.body {
  background: green;
  color: white;
}
<body>
  <div class="header">header</div>
  <div class="body">body</div>
</body>

See current result here

You can view my codepen example at: https://codepen.io/sforest1975/pen/YzMaEQO

I aim for the body div to occupy the full screen height without exceeding 100% or showing a scrollbar.

Desired outcome:

I prefer not to specify the height of the header if possible. Avoiding the use of px is important to ensure consistent results across different font sizes and browsers.

Answer №1

To enhance the structure of your HTML, consider using semantic tags instead of classes for better readability, accessibility, and improved SEO ranking. Utilize tags like header and main.

Here are some key tasks to fulfill your requirements:

  1. Reset the default box-sizing to border-box to accurately calculate element height including padding and borders.
  2. Replace the default body margin with padding.
  3. Add min-height: 100vh to the body for minimum screen height coverage.
  4. Ensure the "body" (or main element) takes up remaining space by using flex-grow: 1.

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0.5em;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

main {
  flex-grow: 1;
}

/* for visualisation only */
header,
main {
  border: 2px dashed red;
}
<body>
  <header>header</header>
  <main>body</main>
</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

JavaScript tutorial: Locate a specific word in a file and display the subsequent word

Seeking assistance with a task: I need to open a locally stored server file, search for a specific word, and then print the next word after finding the specified one. I have managed to write code that successfully opens the file, but it currently prints e ...

Alter the functionality of the input element that has a tag of 'multiple'

I am encountering an issue with a form that allows users to upload multiple photos. When selecting multiple files at once, everything works as expected. However, if a user wants to add images one by one, each new addition deletes the previous file due to t ...

Assign multiple EventListeners to an element and remove specific ones individually

I have a specific element, in this case the $(window) in the code example. I want to attach multiple EventListeners to this element, specifically the "scroll" event twice. These two EventListeners have distinct functionalities that I prefer not to combine, ...

Turn off sticky sidebar feature for mobile-friendly layout

I have encountered an issue with my script that I need assistance with. I am trying to disable this script for responsive or mobile view, but despite trying various methods, it is not functioning as expected. <script type="text/javascript"> $(func ...

Tips for creating a dynamic sticky footer

Seeking out a solution for a sticky footer that adjusts its height based on the width of the browser. Creating sticky footers in flexible designs can be tricky. I've come across various suggestions, discussions, and fixes for implementing sticky foot ...

Concealing axis lines within the initial circular grid or opting not to include them

Is there a way to incorporate some whitespace within the center circle of the radar chart? I'm aiming for the axis to commence at 1 radius (the initial circular line) or perhaps have the stoke set to 0 for the primary radius. Any assistance would be g ...

Tips for adjusting the height of a selection box in jQuery Mobile

I am trying to make the select box height the same as the label (模板分類:). <select id="TamplateClass" style="height:1.5em">...</select> you can use CSS #TamplateClass{height:1.5em;} However, no matter what I try, the results are all th ...

The struggle of media queries: How can you reset an element to its original display style when it's set to none?

If faced with the following scenario, how would you handle it? Imagine this: You need to hide a visible element when a specific media query is met: .myElement { display: block; } and the media query to hide it: @media (min-width: 1000px) { .myElement ...

Can Link Tags be Used outside the <li> Tag in WordPress Widgets?

Is it possible to link the <li> tags in widgets? For example, normally the <a> tag is inside the <li> tag: <div class="widget_archive"> <h3 class="footer-widget-title">Archive</h3> <ul> ...

JavaScript - A simple way to retrieve the dimensions of an image consistently

I'm currently working on a piece of Jquery code that is designed to display an image fitting within the screen's dimensions. These images are generated dynamically as needed. However, I am facing an issue where the height and width of the image a ...

Creating a cohesive design by ensuring buttons match the appearance of other elements, and vice versa

During the process of building a website, I encountered a challenge in making buttons work seamlessly with other elements to ensure uniformity in appearance. This issue often arises in menus, where some buttons are functional while others are simply li ...

"Tricks to prompt the browser to refresh and load up-to-date content

Currently experiencing issues with my web browser (both on my laptop and smartphone). I am working on a website page (HTML, javascript) and have observed that my browser keeps retrieving cached information instead of loading the page each time, preventing ...

Issue with Bootstrap card "sub-components" such as card-heading not functioning

I'm facing an issue while trying to integrate BlazorStrap into an existing .NET6 Blazor web assembly app. It seems like there is a problem with Bootstrap that needs to be resolved before I can get BlazorStrap to function properly. Let's focus on ...

My code seems to be malfunctioning - why can't I keep the aspect ratio?

UPDATE: Can someone please assist me with maintaining the aspect ratio of a drawn image? I am struggling to achieve this and would appreciate any help. I have been attempting to upload an image, draw it using the imageDraw() function, and fit it within a ...

In the production mode, Nuxt styles may not be properly applied

After working on this project for about four months, everything seems fine in the development version. However, once I build the project and upload it to the server, some of my styles are not applied. For more information, I have imported styles both in c ...

Is it recommended to apply the ARIA directory role to a breadcrumb navigation list?

Currently, I am working on enhancing the accessibility of a breadcrumbs component that is structured around a <ul> element. In my quest for solutions, I came across the concept of ARIA roles and specifically the directory role. While it seems like a ...

VSCode displaying HTML errors within a .ts file

There is a strange issue with some of my .ts files showing errors that are typically found in HTML files. For example, I am seeing "Can't bind to 'ngClass' since it isn't a known property of 'div'" appearing over an import in ...

JQuery Mobile pop-up error: Property 'is' is not defined

I am attempting to utilize jQuery to open a dialog box. I have followed the instructions provided in this link: Here is the code snippet for the dialog: <div data-role="popup" id="popupDialog" data-overlay-theme="a" data-theme="c" style="max-width ...

What could be causing the malfunction in my Bootstrap navbar?

<!-- Including Bootstrap CSS files locally-->> <link rel="stylesheet" href="bootstrap.min.css"> <link rel="stylesheet" href="bui.css" <!-- Creating a Navbar--> <nav class="navbar navbar-expand-lg navbar-light bg- ...

The height of the li element is not properly aligning with the height of

I am having some trouble with creating a menu panel. I have set the height for both the li and menu div to be the same, but there appears to be a margin at the bottom of the li. I would like the li to completely fill the menu height. Any help is greatly a ...