Setting minimum page height for varying screen resolutions

I encountered a small issue. I am trying to design my website so that it fills at least the full vertical resolution of the user. Here is my HTML code:

<div id="head"></div>
<div id="body"></div>
<div id="legs"></div>

And this is my CSS code:

html {
    margin: 0;
    padding: 0;
}
#head {
    width: 100%;
    height: 65px;
    background-color: gold;
}
#body {
    width: 80%;
    height: 400px; /* This should adjust based on remaining height */
    background-color: blue;
    margin: 0 auto;
}
#legs {
    width: 100%;
    height: 20px;
    background-color: gold;
}

You can view the live example on jsfiddle http://jsfiddle.net/QgfJ3/embedded/result/. In the example, #head is 65px high and #legs is 20px high. The challenge lies in making #body's height expand based on the user's resolution. For instance, if a user has an 800x600 resolution, it utilizes 65px from #head and 20px from #legs, totaling 85px. There would be 715px remaining, so how can we dynamically set this height for #body?

Answer №1

To dynamically adjust the height of elements, you can utilize JavaScript or the calc function in CSS (http://caniuse.com/calc)

With jQuery:

$(window).on('resize', function() {
    $('#body').height($(window).height() - $('#head').height() - $('#legs').height());
});

Pure Javascript (may not be compatible with Internet Explorer):

window.addEventListener('resize', function() {
    document.querySelector('#body').style.height = window.innerHeight - 85 + 'px';
}, false);

Using calc in CSS:

#body {
    height: calc(100% - 85px);
}

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

Data input not populating in email

After filling out the form, Gmail pops up with no data entered. How can I ensure that the information I input in the form is transferred to the email? <form class="" action="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_em ...

Exploring ways to incorporate zero padding exclusively for mobile devices through Bootstrap

On mobile sm devices, I am trying to achieve padding: 0, but so far nothing has worked. I referenced this post on Stack Overflow and also attempted the code below: The default padding of 15px on the right and left for col-*-12 is causing issues. I only wa ...

Attempting to dynamically alter the background color of a sticky header once a specific class reaches the top of the viewport

I am in need of a solution to alter the background color of a simple sticky header whenever a div/class/id reaches the top of the window, and revert back when scrolled up. This change should occur multiple times. The base code for the sticky header can be ...

Gradually reveal each page as it loads while a spinner rotates in anticipation

I am attempting to create a fade-in effect for the entire page once it has finished loading, This is my current code: <script type="text/javascript"> $(window).bind("load", function() { $('#overlay').fadeOut(function() { ...

PHP-generated image of a security code

I am utilizing sessions to generate an image through a seccode.php file, with the image source set as 'seccode.php' on my sign-in page. While this method typically works fine, occasionally users attempting to use the sign-in page multiple times m ...

Differentiate pointer for checkbox HTML

Just starting out with html and css, and I'm working with a checkbox: <input type="checkbox" name="vehicle" value="Bike">I have a bike<br> My goal is to display a hand icon when hovering over the checkbox. Ho ...

Having Trouble with the Spacing Between Navbar and Page Content in Bootstrap?

Let's tackle the issue at hand: Here is the code snippet. A lot of it has been borrowed from examples and tutorials. <!-- Navigation --> <nav class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <!-- Brand a ...

Is there a way for me to position my chat messages on the right side in the chat room?

I have a react chat application and I'm looking to customize the appearance of my messages. Currently, all entries with an orange vertical bar belong to me and are displayed on the left side of the chat room. I would like to move them to the right sid ...

Efficient segmentation of text using CSS and JavaScript

Currently, I'm involved in a project that allows users to create their own timelines with events. However, there seems to be an issue with the event titles. Users are able to create events with extremely long titles, such as: `1231231231231231231231 ...

Tips on extracting specific strings when they are located next to each other using BeautifulSoup

When using BeautifulSoup to parse the result of an HTML page, I noticed that the specific information I'm looking for is structured like this: <i class="fa fa-circle align-middle font-80" style="color: #45C414; margin-right: 15px"></i>Dep ...

Adjusting column sizes smaller than 1/12 within the Primeng flex grid in Angular 8

In using primeng grid, a common desire is to have 2 columns behave a certain way: the first column should take up 1/13 of the total space the second column should take up the remaining space. Many attempt different solutions like: <div class="p-g ...

Which option is more effective: using an id or using !important?

While some say to avoid using the !important rule and others argue that since ids are unique, there is no need to target like #main > #child and you can simply use #child. In my particular case: #main > div{ color: red; } #child{ color: blue;/ ...

The alignment issue persists with the menu header, as the div is not functioning correctly

I've been struggling to properly align my menu on the website. As it stands, when a user arrives and is not logged in, they see the header below: https://i.stack.imgur.com/mTKse.png Once the user logs in, the login/register text is replaced by a dro ...

What is the best way to eliminate an animation from a component while using it within a different Angular Component?

I have developed an Angular Component that enhances the look of a photo with styling and animation effects. Now, I want to utilize the same styling for another component without applying the animation. Styling and Animation for the photo: .pic { position ...

The text in the TextArea is not displaying line breaks as expected

Similar Question: Issue with new line preservation in .val() method for textarea I'm facing an issue where my text area does not preserve newlines when I enter a message with multiple lines. Even after retrieving the value from the text area, the ...

The innerHTML inside Angular components appears scrambled, with only the final innerHTML rendering correctly

When working on my Angular project (version 8), I encountered an issue where a list of static HTML content retrieved from a database is not rendering correctly in the parent HTML. Strangely, only the last div with innerHTML is being rendered correctly, whi ...

Completing every input box

Here is a piece of code to display a table: <div class="scroll"> <table class="table table-striped"> <thead> <tr> <th>Image</th> <th>Name</th> <th>Author</th> ...

Can a PHP variable be passed along with a div ID in any way?

I am attempting to transfer a PHP value through the "href" attribute from one div to another. While we can achieve this between pages, like so: <a href="somepage.php?id=<?php echo $id;?>"></a> Is there a way to accomplish this using an ...

Is it possible to create a replicating text box in AngularJS that multiplies when entering input

I am experimenting with creating a sequence of text boxes that dynamically generate new empty text boxes as the user enters information into each one. Each text box is required to have an ng-model value associated with it, and they should all be generated ...

Tips for customizing the calendar icon on a date input in Firefox versions greater than 108

Prior to version 109, Firefox allowed for the selection of the date picker's icon similar to Chromium: input[type="date"]::-webkit-calendar-picker-indicator { display: none; /* Hides the icon in Chrome and Firefox < v109 */ } This func ...