Ensure that the side navigation panel is equal in height to the content

I'm struggling to make my right side navigation div fill up the remaining vertical space based on the content box size. I've experimented with setting the height of the body and the div to 100%, using absolute positioning, and even shedding a few tears, but none of these methods are giving me the desired outcome.

Check out the Fiddle I've been working on as a test: https://jsfiddle.net/5punpLs6/

#nav {
    float: right;
    background-color: blue;
    height: 100%;
}
#content {
    background-color: #333;
    overflow: hidden;
}
#footer {
    background-color: green;
    height: 50px;
}
<div id="nav">
    <ul>
        <li>home</li>
        <li>home</li>
        <li>home</li>
    </ul>
</div>
<div id="content">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed cursus nulla nisl, sit amet hendrerit erat iaculis in. Ut tristique sem purus, sit amet condimentum ex auctor vitae. Aliquam ornare lorem quis odio laoreet placerat. Fusce nec egestas erat, ac porta lectus. Vivamus accumsan sed elit quis volutpat. Donec suscipit fermentum arcu eu consectetur. Aenean sit amet neque at enim dapibus imperdiet. Ut facilisis tellus leo, sed rhoncus lacus suscipit quis. Morbi sit amet quam ullamcorper, tincidunt lacus a, hendrerit dui. Pellentesque laoreet lectus eget tempor suscipit. Nam quis condimentum eros, a facilisis neque. Pellentesque ultrices aliquet nisi vitae interdum.</p>
</div>
<div id="footer"></div>

Answer №1

When facing this issue, you have a couple of choices. One option is to follow the recommendation already given and add a container. Keeping things organized within a container can often provide a clean solution.

If for some reason you prefer not to use a container, an alternative approach would be to utilize jQuery after the webpage has loaded without adding any extra HTML. You can achieve this by including jQuery and using a script like:

window.onload = function(){
    $('#nav').height($('#content').height());
}

I tested this method in your JSFiddle and it appears to accomplish what you are aiming for.

Alternatively, you might consider utilizing a pre-built sidebar navigation menu to save time and effort. Instead of reinventing the wheel, using existing resources like this one can be beneficial. I have personally used it in some of my projects with success.

Answer №2

When using percentage height like height:100%, it will only work if the parent element has a specified height. In your situation, you may not actually require this if you simply want the background color to cover the entire area. In that case, wrapping the navigation and content sections in a wrapper div and setting the background on that wrapper would suffice.

Check out the JsFiddle Demo here

#wrapper {
    background-color: blue;
}
#nav {
    float: right;
}
#content {
    background-color: #333;
    overflow: auto;
}
#footer {
    background-color: green;
    height: 50px;
}
<div id="wrapper">
    <div id="nav">
        <ul>
            <li>Item</li>
            <li>Item</li>
            <li>Item</li>
        </ul>
    </div>
    <div id="content">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed cursus nulla nisl, sit amet hendrerit erat iaculis in. Ut tristique sem purus, sit amet condimentum ex auctor vitae. Aliquam ornare lorem quis odio laoreet placerat...</p>
    </div>
</div>
<div id="footer"></div>

You could also utilize CSS table layout to ensure both content and nav sections have the same height. Remember to slightly adjust the order of the markup.

View the JsFiddle Demo for CSS table layout

#wrapper {
    display: table;
    border-collapse: collapse;
}
#content, #nav {
    display: table-cell;
    vertical-align: top;
}
#content {
    background-color: #333;
}
#nav {
    background-color: blue;
}
#footer {
    background-color: green;
    height: 50px;
}
<div id="wrapper">
    <div id="content">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed cursus nulla nisl, sit amet hendrerit erat iaculis in. Ut tristique sem purus, sit amet condimentum ex auctor vitae. Aliquam ornare lorem quis odio laoreet placerat...</p>
    </div>
    <div id="nav">
        <ul>
            <li>Item</li>
            <li>Item</li>
            <li>Item</li>
        </ul>
    </div>
</div>
<div id="footer"></div>

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

What is the best way to assign attributes to all items in an array, excluding the currently selected one?

I need to implement dynamic buttons in my HTML document while a JavaScript class is running and receives a response from the backend. I am using jQuery and vanilla JS, and have included an example below to demonstrate this functionality. The goal is to dis ...

Trouble with Div Alignment in Web Browsers: Firefox vs. Google Chrome

I have encountered an issue with the layout of my website when using the Div element. While it appears correctly in Internet Explorer, the display gets distorted in Firefox and Google Chrome when the div is expanded. I have searched online for solutions b ...

Using JavaScript to retrieve the text value of a custom button

Recently, I encountered a peculiar button in my code: <button type="button" id="ext-gen26" class=" x-btn-text">button text here</button> Despite its unique appearance, I am struggling to locate it based on the tex ...

Coloring a table in vue.js based on performance rankings

I'm facing an issue with sorting my data performance in vue js. Here is the script I have created so far: <template> <div> <div class="row"> <h2> Campaign Performance </h2> <table class=&q ...

When it comes to designing responsive websites, the use of `@

Being new to CSS, I am open to criticism and feedback. @media (max-width: 735px) {... } @media (min-width: 735px) {... } @media (width: 320px) {... } @media (width: 360px) {... } @media (width: 375px) {... } @media (width: 414px) {... } I have tried us ...

Clicking on a checkbox and subsequently removing validation through jquery

Situation : When the 'I am Fresher' checkbox is selected, I want to hide the 'Experience field', but still keep its validation visible so that my form cannot be submitted. My Requirement : When the 'I am fresher' checkbox is ...

Demo showcasing the issue with the side navbar in Bootstrap not functioning as expected

I'm currently working on implementing a side nav bar using the code snippet from this link: However, when I integrate this code into my application, the left nav bar only extends to the height of the links, and the content area begins after the left ...

Tips on utilizing jQuery or JavaScript to efficiently filter out outcomes exceeding a specified range

<input type="range" name="rangeInput" min="100000" max="750000" onchange="updateTextInput(this.value);"> Displayed above is the slider code that provides a value output below. <div id="sliderValue"> <p>Max Value £</p> <input t ...

Prevent users from including spaces in their usernames during registration

I've encountered an issue with my registration form. It currently allows users to register usernames with spaces, such as "user name" instead of just "username" without any spaces. Despite searching and reading numerous tutorials, none have been of m ...

How can you efficiently showcase multiple fetch functions on a single page by utilizing loops in PHP?

I am attempting to showcase the countries from each continent on a single page using buttons for each continent. When a user clicks on a button, I want the countries of the selected continent to be displayed. However, I am encountering an issue where only ...

Is there a way to automatically submit a form with the logged-in user as the default selection?

Once you complete the sign-up process, you will be directed to the login page. After logging in successfully, you will be redirected to a different page where you can fill out a form with additional information about yourself as a new user. The issue arise ...

Issue with custom page not displaying summary image in Moodle

Whenever I try to fetch a summary image on my customized page, it does not show the image's name and instead displays a small icon image. echo format_text($course->summary); Click here for an example image ...

Chrome scrolling causing Webkit mask to shift position

I have successfully created a rounded Google map after much effort. Here is the link to the JSFiddle: http://jsfiddle.net/DZTvR/13/ However, I encountered an issue in Chrome where the mask remains static while scrolling, unlike other browsers like FF, Op ...

Adjustable background image with no need for a scroll bar

I am looking to create a static webpage that remains fullscreen without any scrolling. My goal is to have a centered form with the background image adjusting to the window size. As a beginner, I apologize if this request seems too simple. .container { ...

Picture appears to be off-center

I created a loginPage.php file with the following content: <?php //some php stuff ?> <!DOCTYPE html> <html> <head> <title>Login Form Design</title> <link rel="stylesheet" type="text/css" href="stylelogin.c ...

Retrieve the scope object using angular.element and the $ID parameter

In order to transfer data from an angular application to scripts that operate outside of angular, I am seeking a solution since I cannot modify the code of the angular app. By utilizing Angular Batarang and NG-Inspector extensions in Chrome, I have identi ...

Ensure that the floated div and its non-floated counterpart are both set to 100% height

I want to design 3 colored divs with specific dimensions and placements. The left div should have a fixed width and take up 100% of the height. The right div should fill up the remaining space (100% height and width minus the left div). The last div sho ...

JavaScript and jQuery code: Trigger the vjs-fade-out class to toggle when the userActive value in video.js changes

I am currently utilizing video.js to develop a player that is compatible with various devices. One crucial feature I have included is a custom "return to menu" button located in the top right corner of the video player. The challenge I am facing is dynamic ...

"Strategies for using Selenium in Python to simulate clicks without relying on class, id, or name attributes

I am currently attempting to locate the "X" button and click on it, but I am struggling to find a way to do so. Below is the HTML code for the element: <div style="cursor: pointer; float:right; border-radius: 3px; background-color: red; font-size: ...

Choose from a dropdown menu to either activate or deactivate a user account

I'm new to php and I need help getting this code to function properly delete.php <?php include_once 'dbconfig.php'; if($_POST['del_id']) { $id = $_POST['del_id']; $stmt=$db_con->prepare("UPDATE tbluse ...