What is the best way to ensure that a <div> extends all the way to the bottom?

My goal is to have this element reach the bottom of the screen, but I also have a header which complicates things. Setting height: 100%; or 100vh results in the page being too big and scrollable. I don't want to manually calculate it because of responsiveness. Despite trying every possible solution, my CSS still looks messy.

*{
    box-sizing: border-box;
}

html, body{
    min-height: 100% !important;
    height: 100%;
    margin: 0;
    padding: 0;
}

body{
    font-family: Verdana, Geneva, Tahoma, sans-serif;
    color: #2e3e56;
    background-color: whitesmoke;
    width: 65%;
    margin: auto;
}

/*header styling*/
#header{
    text-align: center;
    background-color: #2e3e56;
    color: #d0cbc5;
    font-size: 1.1rem;
    padding: 0.3% 0% 0.3% 0%;
    margin: 0;
    top: 0;
}

#header table{
    width: 100%;
}

#hnametd p{
    margin: 0;
    padding: 0;
    color: white;
    font-weight: bold;
}

#hnametd h1{
    font-family: 'Times New Roman', Times, serif;
    margin: 0;
    padding: 0;
}

#hlogotd{
    width: 18%;
    padding: 0;
}

#hlogo{
    width: 100%;
    float: left;
    overflow: hidden;
}
/*-------*/

/*hr-area styling*/
hr{
    height: 10px;
    border: none;
    margin: 0;
    padding: 0;
}

.dblue{
    background-color: #156390;
}

.lblue{
    background-color: #7296ab;
}

.gray{
    background-color: #d0cbc5;
}

.hrbottom{
    height: 15px;
    width: 100%;
}
/*-------*/

#wrapper{
    height: fit-content;
    width: 100%;
    background-color: #2e3e56;
    padding-right: 3.5%;
    overflow: hidden;
}

/*sidebar styling*/
#sidebar{
    float: left;
    color: white;
    font-size: 0.955rem;
    padding: 2.2% 0 2.2% 2.2%;
    max-width: 21%;
}

#sidebar ul{
    list-style-type: none;
    margin: 0 auto;
    padding: 0;
}

.con-li{
    margin: 1em 0 1em 0;
}

#sidebar h2{
    font-family: 'Times New Roman', Times…
...

Answer №1

Utilizing flexbox below, I opted to maintain your original HTML structure by setting the body as a flex container (although it could also be another parent element with 100% height).

Here are the key adjustments I made:

/* The body serves as the flex container in this case */
body {
    height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

html, body{
    height: 100vh;
}

#wrapper{
    flex: 1 1 auto;
}

To view the complete CSS based on your initial code, please visit: https://codepen.io/andrew1357/pen/rNmwGpz?editors=1100

Answer №2

There are various strategies to accomplish this objective. One method involves calculating the minimum height by subtracting the total height of other elements. For instance, in this case, the header and 6 <hr> tags sum up to a height of 247px. It's also feasible to ensure browser compatibility by opting for <header> tags instead of using div containers.

*{
    box-sizing: border-box;
}

html, body{
    min-height: 100% !important;
    height: 100%;
    margin: 0;
    padding: 0;
}

body{
    font-family: Verdana, Geneva, Tahoma, sans-serif;
    color: #2e3e56;
    background-color: whitesmoke;
    width: 65%;
    margin: auto;
}
/* Rest of the CSS code remains unchanged */

    </div></p>
    </div></answer2>
<exanswer2><div class="answer" i="68435985" l="4.0" c="1626665245" v="1" a="THVrZQ==" ai="15670457">
<p>There are several ways to achieve this. One possibility is to calculate the minimum required height by deducting the sum of all other heights. In this particular example, the header and those six <hr> tags combine to give a height of 247px. Additionally, you can enhance cross-browser support and employ <header> tags as an alternative to using div elements.</p>
<p><div>
<div>
<pre class="snippet-code-css lang-css"><code>*{
    box-sizing: border-box;
}
/* CSS code continues... */

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="test.css">
</head>
<header id="header">
        <table>
            <tr>
                <td id="hlogotd"><img src="logo.svg" alt="Logo" id="hlogo"></td>
                <td id="hnametd">
                    <h1>Header heading</h1>
                    <p>Header placeholder text.</p>
                </td>
            </tr>
        </table>
    </header>
    <hr class="dblue">
    <hr class="lblue">
    <hr class="gray">
    <div id="wrapper">
        <div id="sidebar">
            <h2>Sidebar heading</h2>
            <p>Some placeholder text.</p>
        </div>
        <div id="main">
            <h1>Welcome!</h1>
            <hr class="dblue">
            <p>Some placeholder text.</p>
        </div>
    </div>
    <hr class="gray hrbottom">
    <hr class="lblue hrbottom">
    <hr class="dblue hrbottom">
</html>

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

CSS: Placing text within an icon - A simple guide

Hello, I am currently working on an Ionic application where I have rounded areas to display information. My challenge is to incorporate a number inside a heart icon within the second area. I want to ensure that the alignment of the number and the heart is ...

How can a Vue.js toggle button dynamically change based on the combined state of two checkboxes?

My current project involves implementing a toggle feature that allows users to enable a day and then select between AM or PM using checkboxes. One issue I'm facing is how to de-toggle the button if a user unchecks both AM and PM options. Check out t ...

What is the best way to modify the size of the letter background?

I'm facing an issue with a word that has a background color. While using the CSS property background-color: blue, allows me to give the word a background color and display: inline-block helps in sizing it exactly to the word, the problem arises when I ...

Trouble Arising in Showing the "X" Symbol upon Initial Click in Tic-Tac-Toe Match

Description: I'm currently developing a tic-tac-toe game, and I've run into an interesting issue. When I click on any box for the first time, the "X" symbol doesn't show up. However, it works fine after the initial click. Problem Details: ...

Is there a method to uncover the code that controls the display of a <div> element?

As a fresh face at the company, I've been given the responsibility of developing a chart that is similar to one already present on their website. While I have the knowledge and skills to create it, I am struggling to locate the specific code where the ...

Split the input string into individual characters for each entry

As a beginner in Angular, I am currently exploring ways to split a single input field into separate inputs for each word. I attempted to achieve this using jQuery but struggled and also learned that mixing jQuery with Angular is discouraged. Here's th ...

What is the best way to locate a specific button in jQuery?

How can I determine if the target of a click event is a button element in a document? $(document).click(function(e){ if(e.target.nodeName.toLowerCase() === 'button') { // Do something } Is the code above correct for this purpose ...

I'm looking for a way to retrieve an object from a select element using AngularJS. Can

I am working with a select HTML element that looks like this: <select required="" ng-model="studentGroup"> <option value="" selected="">--select group--</option> <option value="1">java 15-1</option> <option value="2">ja ...

Is there a way to customize the CSS ul menu specifically for the JavaScript autocomplete feature?

I have created a search page with autocomplete for the first textbox, but I noticed that the ul/li CSS classes used for styling the main menu are impacting the JavaScript list. How can I override the menu styling to display a regular list format? Being a ...

Adjust the element colors within a Vue loop with dynamic changes

Hey there! I'm currently working on achieving a unique design inspiration that involves colorful badges grouped together. Here's a visual reference: In the image, you can see these badges grouped in pairs, but they can actually be in sets of 4 o ...

Guide on programmatically choosing an option within a variable with the help of jQuery

Imagine having a variable named html, which holds select options like this: var html = '<select>'+ '<option value="10">10</option>'+ '<option value="20">20</option>'+ ...

Adjusting the size of the snap increments

When using gridstack, I have the ability to resize my widgets. However, I've noticed that when dragging on the widgets' handles, they snap to specific sizes, which seems to be a fixed amount. If I wanted to set the widget's size to something ...

Enable automatic full-screen mode on Google Chrome upon page load

I would greatly appreciate it if you could provide an answer to this question, even if it is marked as a duplicate. I have tried various solutions and sought help, but unfortunately, nothing seems to be working for me... What I really need is for the brow ...

Whenever I include "border:0" in the styling of my hr element, a significant number of hr elements fail to appear on the screen

I have been experimenting with using hr elements to divide sections of my web page. Here is a snippet of the CSS code I am using: hr{ content: " "; display: block; height: .1px; width: 95%; margin:15px; background-color: #dfdfdf; ...

Showcase your skills in CSS, HTML, and Javascript

I attempted to search for something similar but came up empty-handed. I have two buttons. When I click one of them, I want to hide a certain division, but it's not working as expected. The divs d2 and d3 are initially hidden when the page opens, whic ...

Customize Cell Styling with Bootstrap Full Calendar CSS

I am attempting to implement a Bootstrap calendar feature where cells are colored green if the timestamp is greater than today's date. This can be achieved by: $checkTime > $today cell.css = green background I came across this code snippet on St ...

When my screen measures 1700px wide, a width of 100% in CSS appears significantly narrower than a width of 500px

Currently, I am working on designing a responsive layout for a 3D globe. The code snippet that stores the structure is as follows: <div class="text-center main"> <canvas id='globe' width='100%' height='100%'> ...

Styling an image with dual attributes using CSS

I'm looking to customize two img classes with different properties. For instance, in my CSS, I want to define: img { padding-left: 15pt; float: right; } and then: img1 { padding-left: 15pt; float: left; } where img1 would serve as a tag fo ...

Display various divs simultaneously based on the quantity of items in the dropdown menu

In my project, there is a dynamic select list that retrieves items from the database. Users have the ability to add or delete items from this list. Below is some code related to this functionality: <div class="form-group col-md-3"> <la ...

What causes the scrollbar to not extend to the bottom when connected to another scrollbar via a JavaScript equation?

I have been working on a code that involves multiple scrollbars that are all linked together. When you move one scrollbar, the other two will move proportionally. However, due to differences in width, the scroller doesn't always reach the end of the s ...