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

Steps for accessing a particular tab on a separate webpage

I am working on a home page that includes an IFrame displaying a separate HTML page. Within the embedded page, there is a link that, when clicked, should open another page in the same IFrame with a specified tab displayed. This new page contains 3 tabs, ...

No matter how many times I modified the code in the ReactDOM.render() method within my index.js file, the end result remained unchanged

When I ran npx create-react-app my-app, and then proceeded to cd my-app and npm start, a browser opened on localhost:3000. While looking at the index.js file, I noticed that the ReactDOM.render() method included the following code: ReactDOM.render( <Rea ...

Adjusting the height of a card in Reactstrap

I am currently exploring Reactstrap and aiming to ensure a specific Card adjusts according to the size of the window by setting its aspect ratio using percentages rather than pixels. Interestingly, while adjusting the width works as desired, I'm faci ...

Trouble with Flex 3 styles not fully applying to dynamically generated tabs within a TabNavigator

When using ActionScript to dynamically create a tab, I noticed that the style is applied to the skins but not to the text of the newly created tab until I click on another tab and then go back to it. ActionScript private function clickAddTabHandler(event ...

Background image loading gets delayed causing it to flicker

Instead of having separate files for different elements on my site, I decided to keep all the JavaScript in one large file called my_scripts.js. To speed up loading times, I defer the loading of this file using inline JavaScript and make sure it is the las ...

The surprising margins that Bootstrap sneaks into columns

It seems like bootstrap is mysteriously including an unseen margin in my column classes. Even after inspecting an element labeled as 'col-lg-3,' there is no visible margin CSS, and attempting to manually add margin:0 still results in a margin bei ...

Blog Format Featuring Side-by-Side Columns

Is there a way to achieve horizontal columns that don't cut off articles and flow seamlessly into the next column? I want the columns to have varying heights, such as Post A at 60% height, followed by Post B at 40% height, then Post C at 30%, and fina ...

What is the solution for incorporating multiple elements in knockout's applyBindingsToNode function?

I am currently using knockout applyBindingsToNode to dynamically add and remove elements in order to update my html. I need to cut the binding, which is why I am utilizing applyBindingsToNode. In an example I have provided, if you click on the button "Reb ...

Is there a way to adjust the background color of the clicked tab with divs and revert the others back to their original color?

<span class="row top-bar-left align-items-center" style="width: 80%; float:left"> <div tabindex="1" class="link tab" routerLink="details" routerLinkActive="active" [qu ...

Firebase issue: The function ref.once is throwing an Uncaught TypeError and is not recognized

I am attempting to utilize Firebase for both uploading a file and storing it in my database simultaneously. I want to track the number of uploads so that I can rename each file uniquely. While I have successfully uploaded and stored a copy in the database, ...

CSS: Adjusting the vertical position of text without affecting the background

I am currently working on some CSS buttons that expand in size when hovered over. I have successfully increased the text size as well, but now I am facing an issue with aligning the text a few pixels lower without affecting the background image layout. Ca ...

Implementing JavaScript Code in TypeScript

Every JavaScript code should also be valid in TypeScript, but when attempting to run the following code snippet below, an error is triggered. Could someone convert this JavaScript code into TypeScript? Error: 20:9 - TS2531 Error: Object is possibly 'z ...

Use PHP to highlight the row that has been selected in an HTML table

I am currently working on a project where I have styled multiple HTML tables on my website using alternating gray and white bands. Now, my goal is to highlight the selected row in a darker shade of gray. I have experimented with various solutions, and the ...

Using Selenium and PhantomJS for web scraping to retrieve information on product details

As a beginner in web scraping with Python, I am currently working on extracting product details from a webpage using Selenium and PhantomJS. However, I am facing a challenge as the page does not display the rendered HTML when I try to access it using "driv ...

Tips for adjusting the position of the second child in my navigation menu using CSS

Struggling with customizing my navigation menu in CSS, I'm seeking assistance. My goal is to have the second child element of the navigation menu on a new line instead of below the first child (refer to the image for clarification). An example of the ...

Discover how to access and manipulate JSON files in an Angular application using

Currently, I am diving into learning TypeScript with Angular and I'm interested in reading a JSON file. The structure of my JSON file is as follows: { "nb": "7", "extport": "1176",, "REQ_EMAIL": ...

Unveil as you scroll - ScrollMagic

I am working on a skill chart that dynamically fills when the document loads. I am exploring the possibility of using ScrollMagic to animate the chart filling as the user scrolls down. After trying various combinations of classes and pseudo-classes in the ...

Have the quotes within my markup been replaced with HTML entities?

Currently, I am working on a content page that uses a master page which includes a text box and a button. My goal is to have the button execute some JavaScript code before performing any other actions. At the moment, I am in the testing phase of this JavaS ...

Styling <CardHeader> component with React and Material UI CSS

Incorporating React and Material UI, my goal is to arrange 3 divs within a <Card> <CardHeader/>, positioning them with left, center, and right alignment as illustrated below. The modification required is minor - I simply need to eliminate the ...

Is there a way to determine if a table cell contains overflowing text compared to another cell?

Is there a way to detect if the content of the fourth td overflows from the second td? I am looking for a method to determine which td has overflowed text and identify which td's text is overflowing. What approach can be used to determine which td i ...