Extend the height of a div to reach the beginning of the following div

I'm attempting to make my main content and menu divs extend up to the bottom of the footer div. Here's my HTML:

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}
#wrapper {
  min-height: 100%;
  position: relative;
}
#header {
  padding: 10px;
  background: #3095C7;
}
#main {
  padding-bottom: 80px;
  bull
}
#content {
  padding-left: 310px;
  background: #FFEFC4;
}
#menu {
  background: #67b5d1;
  width: 300px;
  position: absolute;
  float: left;
}
#footer {
  text-align: center;
  padding: 30px 0;
  width: 100%;
  height: 50px;
  position: absolute;
  bottom: 0;
  left: 0;
  background: #3095C7;
}
<div id="wrapper">
  <div id="header"></div>
  <div id="main">
    <div id="menu">
      menu here
    </div>
    <div id="content">
      content here
    </div>
  </div>
  <div id="footer">footer content</div>
</div>

I've set the main, content, and menu heights to 100%, but they only reach the very bottom of the wrapper div, extending beyond the footer. What I want is for the menu and content to span from the header down to the top of the footer, filling the entire page. I've experimented with using vh units, but it isn't always accurate when resizing the window.

Are there any tricks to ensure the divs fill all available space without overlapping the footer?

Answer №1

Utilizing flexbox for a straightforward layout (compatible with browsers IE10+ and most modern browsers)

  1. To begin, configure #wrapper with
    display: flex; flex-flow: column nowrap
  2. Next, set up #main with flex: 1 1 auto and apply display: flex to ensure it expands to the required height and allows its children to grow proportionally.
  3. Apply flex: 0 1 300px to #menu (while eliminating floats/position: absolute) and set #content to flex: 1 1 auto.

UPDATE Don't forget to remove position: absolute (as well as associated bottom/left rules) from the footer. Additionally, add more content and adjust the left menu to prevent collapsing by changing flex: 0 0 300px instead of flex: 0 1 300px.

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}
#wrapper {
  min-height: 100%;
  display:flex;
  flex-flow: column nowrap
}
#header {
  padding: 10px;
  background: #3095C7;
}
#main {
  display: flex;
  flex: 1 1 auto;
}
#content {
  flex: 1 1 auto;
  background: #FFEFC4;
}
#menu {
  background: #67b5d1;
  flex: 0 0 300px;
}
#footer {
  text-align: center;
  padding: 30px 0;
  width: 100%;
  height: 50px;
  background: #3095C7;
}
<div id="wrapper">
  <div id="header"></div>
  <div id="main">
    <div id="menu">
      menu here
    </div>
    <div id="content">
      [Lots of lorem ipsum content]
    </div>
  </div>
  <div id="footer">footer content</div>
</div>

Answer №2

Your code seems to be a little messy.

If I understand correctly, you have a header and footer with fixed heights, and you want the content of your website to fill the height of the vertical window.

When using height:100%, keep in mind that all parent elements need to also have the same height set to 100%. However, this may cause an issue as the total height might exceed the viewport height and create a scroll bar.

You can utilize the CSS calc function to subtract pixels from the 100% height. In your case, I calculated the pixels you need to subtract, and the height for your content should be:

height: calc(100% - 110px);

And the height of your main container should be:

height: calc(100% - 20px);

You will need to make several changes to your CSS to achieve the desired layout. Please review my suggestion here: FIDDLE

(I have also used calc to set the width of your content since your menu has a fixed width)

One important note: if you are not using border-box for your containers, be cautious when applying this technique. Padding values will add to the width (or height) of your elements. It is recommended to include the following rule in your CSS:

* {
    box-sizing: border-box;
}

Once you get accustomed to this practice, your development process will become smoother. This is just my modest opinion; others may have differing viewpoints.

Answer №3

By assuming that the footer has a fixed height, you can easily set the height of #menu and #content using the CSS property position: absolute:

position: absolute;
top: 0;
bottom: 110px; /* #footer height */

In most cases, absolutely positioned elements have auto values for height and width which adjust according to the content within the element. However, non-replaced absolutely positioned elements can be stretched to fill the available space by specifying specific values (rather than auto) for both top and bottom while leaving height unspecified (i.e., auto). The same applies to left, right, and width.

For more information, refer to: https://developer.mozilla.org/en-US/docs/Web/CSS/position#Notes

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}
#wrapper {
  min-height: 100%;
  position: relative;
}
#header {
  padding: 10px;
  background: #3095C7;
}
#main {
  position: absolute;
  top: 20px;
  bottom: 0;
  width: 100%;
}
#content {
  position: absolute;
  left: 310px;
  right: 0;
  top: 0;
  bottom: 50px;
  background: #FFEFC4;
}
#menu {
  background: #67b5d1;
  top: 0;
  bottom: 50px;
  width: 300px;
  position: absolute;
}
#footer {
  text-align: center;
  padding: 30px 0;
  width: 100%;
  height: 50px;
  position: absolute;
  bottom: 0;
  left: 0;
  background: #3095C7;
}
<div id="wrapper">
  <div id="header"></div>
  <div id="main">
    <div id="menu">
      menu here
    </div>
    <div id="content">
      content here
    </div>
  </div>
  <div id="footer">footer content</div>
</div>

Answer №4

To incorporate a set height in your CSS, for instance, height: 100px, and then define overflow as hidden.

In essence, simply include:

height: 100px;
overflow: hidden;

in both your menu and content sections.

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

Renew the Look of Dynamic HTML with Updated Styles

In the middle of my website, there is a console that contains links to dynamically update its content using javascript. Everything works smoothly, but the new HTML added dynamically does not have any styling applied to it. Is there a way to refresh the CS ...

How can I utilize CSS shapes to create clickable images?

I'm facing a challenge in making an input field and button functional even when they are positioned behind a uniquely shaped PNG image. https://i.stack.imgur.com/eOg0N.jpg Initially, I believed that by applying a shape to the image, it would automat ...

Element is not being overlapped by higher z-index

#navbarContentHamburger { background-image: url(../img/657904-64.png); background-size: contain; z-index: 3; position: fixed; width: 22px; height: 20px; top: 7.7px; left:8px; } .open { width: 4% !important; transiti ...

Should I create CSS rules for different screen sizes or should I dynamically render components based on the screen size?

As I delve into learning React, I find myself unsure about the most effective approach to make websites responsive. Currently, I am utilizing Semantic UI React as my CSS Library. While everything displays perfectly on desktop, I encounter some issues when ...

Transforming the style of a particular element within React using Array().fill().map's method

Looking for a way to dynamically change the color of an array in React + styled jsx? Let's say you want to change the color when the number of elements in the array is a multiple of 4 (e.g. the index is 4, 8, etc.) Is there a clever solution to achi ...

Create a script that ensures my website can be set as the homepage on any internet browser

I am currently in search of a way to prompt users on my website to set it as their homepage. Upon clicking "Yes," I would like to execute a script that will automatically make my website the user's browser homepage. I have come across a Similar Thread ...

Chosen link fails to update color

I've successfully implemented a navigation bar that changes the content in the body when each link is selected. Utilizing AJAX for dynamic content changing, I can change the color of menu items on hover but am facing issues with changing the color upo ...

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> ...

Arranging 2 Divs next to each other and optimizing CSS code for efficiency

I’ve been delving into the world of CSS for a couple of months now, and I’ve hit a roadblock trying to align these two divs on the same line. My logo fits perfectly on the header bar, but I can't seem to get my login button to cooperate. Any tips ...

What's the best way to format text as bold in a .ts file so that it appears as [innerText] in the HTML section?

When looking to emphasize specific text without using [innerHTML], what is the alternative method besides the usual line break changes in the interface? How can we make certain text bold? For instance: .ts file string = This is a STRING bold testing.&bso ...

Having Trouble with Unevenly Spaced Child Elements in CSS Flexbox?

I'm running into a problem while utilizing CSS Flexbox to design a layout with multiple child elements. The issue at hand is that the child elements are not aligning correctly within the parent container. I aim for them to be evenly distributed and ce ...

Guide to adding text descriptions for synchronized media as outlined in WCAG 2.0 Success Criterion 1.2.2

Based on the guidelines of WCAG 2.0 success criterion 1.2.2: 1.2.2 Captions (Prerecorded) Level A Captions are provided for all prerecorded audio content in synchronized media, except when the media is a media alternative for text and is clearly labeled ...

What is the process of implementing a tree view hierarchy in PHP using CodeIgniter?

I have an array structured like this: Array ( [0] => Array ( [id] => 1 [child_name] => "Emma" [parent_name] => "Mr Brown" ) [1] => Array ( [id] => 2 ...

Minimize duplication of HTML code by refraining from PHP usage

It seems like this task may be simple for some individuals. To prevent redundancy in code across multiple pages, my goal is to insert segments of HTML code without having to manually input all of the elements each time. It's similar to a page controll ...

What is the best way to add an array into another array?

I am working with a main array consisting of 64 positions, and for each position, I need to create another array. For example: someArray[index] = [someObject]. How can I generate these arrays? And how can I access someObject.name and someObject.lastName f ...

Trigger an "onMouseMove" event when the "pointer-events-none" css property is applied to fire

Currently, I am dealing with a React Component that contains an absolutely positioned div overlaying other content within my application. This particular div is utilized to trigger react's onMouseMove event. My objective is to apply the CSS value poi ...

Basic JavaScript Calculator Utilizing JQuery

I have been diligently working on a Javascript Calculator for quite some time now. The CSS and HTML elements seem to be in order. However, the calculator is only functioning properly for sevens and division operations! While the input for other numbers g ...

Concealed HTML element or generated on-the-fly using JavaScript

What is considered the best practice? I have multiple windows on a page: Auth, Execution Result, and Popups. I can display them in two ways: Create them in pure HTML with CSS, set to display: none, and then fadeIn using jQuery. Create the elements ...

What could be causing the issue with image handling in ASP.NET deployed on a local IIS server?

My web app, built using ASP.NET 4 MVC2 architecture in Visual Studio 2010, has been deployed to my local IIS server under the virtual directory EasyMan4. Recently, I added an image to the Content Folder via Solution Explorer. In the Site.Master page, I ins ...

What are the benefits of incorporating frontend libraries into our projects?

Currently working on a web page using Vue.js, without a backend at the moment. In need of tabs, I explored the options and came across vue-tabs-component. However, to proceed with the installation, the following command is required: npm install vue-tabs- ...