"Div does not perfectly match the height of its parent div when using the 'inherit' value

I have 3 divs inside a parent div, intended to be arranged like so:

 ______________________________________
|                                      |
|                HEADER                |
|______________________________________|
 ____   _______________________________
|    | |                               |
|    | |                               |
|    | |                               |
|    | |                               |
| N  | |                               |
| A  | |            CONTENT            |
| V  | |                               |
| -  | |                               |
| B  | |                               |
| A  | |                               |
| R  | |                               |
|    | |                               |
|    | |                               |
|____| |_______________________________|

The #header has a fixed height and is working properly.

However, the content inside the #content div is dynamic based on the loaded page. I am trying to make the #nav-bar match the height of #content at all times, but I haven't been successful in doing so.

I want both #content and #nav-bar to have a min-height of 100% of the screen height (taking up the full height of the screen at minimum), and when #content grows beyond that, I want #nav-bar to grow accordingly.

I have managed to make #nav-bar's height grow, but it's not matching exactly with #content, resulting in mismatched heights.

Here is the relevant code snippet:

HTML:

<div id='container'>
    <div id='center-panel'>
        <div id='top-banner'></div>
        <div id='nav-bar'>
            <?php
            echo get_nav_list();
            ?>
        </div>
        <div id='header'><span id='fact-tag'>current points leader</span><span id='fact-value'>4</span></div>
        <div id='content'>Lorem ipsum...
        </div>
    </div>
</div>

CSS:

html, body {
    background-color: #ccc;
    min-height: 100%;
    margin: auto;
    font-family: 'Noto Sans', 'Tahoma', sans-serif;
    font-size: 12pt;
    color: #666;
}

#container {
    height: 100%;
    overflow: scroll;
}

#center-panel {
    margin-left: auto;
    margin-right: auto;
    width: 800px;
    background-color: #fff;
    min-height: 100%;
}

#header {   
    height: 33px;
    max-height: 33px;
    text-align: right;
    color: #aaa;
    padding: 0px 15px 0px 160px;
    font-size: 18pt;
    border: 2px dashed blue;
}

#header span#fact-tag {
    font-weight: 700;
}

#header span#fact-value {
    display: inline-block;
    min-width: 40px;
    background-color: #ccc;
    margin-left: 10px;
    color: #444;
    padding: 0px 3px 0px 3px;
}

#nav-bar {  
    float: left;
    width: 110px;
    text-align: center;
    height: inherit;
    font-family: 'Oxygen', 'Tahoma', sans-serif;
    padding: 33px 0px 0px 0px;
    border: 2px dashed red;
}

#nav-bar ul {   
    list-style-type: none;
    text-align: left;
    font-size: 10pt;
    padding: 0px;
    margin: 0px;
}

#nav-bar > ul > li {    
    min-height: 35px;
    background-color: #444;
    color: #ccc;
    border-left: 6px solid #444;
    border-bottom: 3px solid #666;
    cursor: pointer;
}

#content {  
    padding: 60px 45px 30px 160px;
    border: 2px dashed green;
}

Javascript:

$('#nav-bar ul li').on('click', function() {
    $(this).css('border-left-color', '#ad9557').siblings().css('border-left-color', '#666');
    $(this).children().slideDown('fast');
    $(this).siblings().children('ul').slideUp('fast');
});

$('#nav-bar').css('height', $('#container').height());

Answer №1

Avoid using display: table or the float property for layout purposes. Tables should be reserved for tabular data, while floats are intended to allow text to flow around them in paragraphs. To address this issue, simply apply position: absolute to your #navbar element and make adjustments to other properties as needed to achieve the desired layout.

#navbar {
    padding: 33px 0px 0px 0px;
    float: left;
}

#navbar {
    position: absolute;
    height: 100%;
}
#container {
    position: relative;
}
#nav-bar ul {
    margin: 33px 0 0;
}

The margin on #nav-bar ul was modified to accommodate the navbar's padding, preventing it from extending beyond the page limit. For further information on absolute positioning, refer to this resource.

This solution does not rely on JavaScript and is compatible with most browsers.

Edit: To adjust the page based on the user's screen height and the #content div, you can use a simple jQuery script to check if the content div requires resizing.

($('#content').offset().top+$('#content').height() < $(window).height())?($('#content').height($(window).height()-90-$('#content').offset().top)):null;

This code essentially ensures that the content div will expand to fit the window height if necessary, ensuring optimal display without unnecessary scrollbars.

Answer №2

Consider using the CSS property display:table to ensure that the heights of both columns are the same, regardless of their content.

Answer №3

Give this a shot:

To make it crucial, include '!important':

$('#navigation-bar').css('height', ($('#container').height()+'!important'));

Answer №4

To avoid compatibility issues with IE8, you can use the calc function:

#nav-bar,
#content {
    min-height: calc(100% - 33px); // 33px represents the height of the header
}

If you must support IE8, you can achieve the same effect using jQuery which you are already utilizing:

$('#nav-bar, #content').css('min-height', ($(window).height() - 33) + 'px');

If you want '#nav-bar' to always match the height of '#content', you can add this line of code:

$('#nav-bar').css('height', $('#content').height() + 'px');

Alternatively, you can consider implementing flex-box.

Answer №5

Here is my interpretation of the solution with a link to a jsFiddle. I made most of the adjustments in the CSS and HTML elements, while keeping the nav height adjustment very similar to the code you provided:

$('#nav-bar').css(
    'height',
     $('#content-container').height() - parseInt($('#nav-bar').css('border-width'), 10) * 2);

The calculation for

parseInt($('#nav-bar').css('border-width'), 10) * 2
takes into consideration the border effects on #nav-bar. Your implementation may not require this additional step.

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

Using blending modes with gradient colors in an SVG design

I'm struggling to get my logo to change colors upon scrolling into a button with similar gradient colors, but I just can't seem to make it work. Can anyone lend me a hand? Thank you! Take a look at my Logo. I've attempted to add some styles ...

What are some ways to optimize the efficiency of handling a sizable JSON object in React Native?

I am currently developing an application using React Native and have encountered significant slowdowns during transitions when loading more data. I am exploring alternative app structuring methods to prevent these performance issues, especially as the JSON ...

Achieve focus in EditorFor MVC using JavaScript

You have a button that triggers a pop-up window <a data-dialog="AddProduct" href="@Url.Action("AddProduct", "Outputs")" id="addproduct" class="pop-up-window btn btn-success">Add Product <span class="glyphicon glyphicon-plus-sign" aria-hidden= ...

The parameters passed in an axios get request are not carried over to a create request

Exploring the capabilities of the YouTube API with ReactJS While working with the YouTube API, I came across the create method in axios. However, I faced an issue where the params were getting overwritten. What am I missing here? I have a file named yout ...

How can I retrieve the span html element without using a class with ajax?

Trying to work with Ajax syntax for the first time, I have this HTML code. <div class="select2-container select2" id="s2id_ServiceID-2-1" style="width: 100%;"> <a href="javascript:void(0)" onclick="return false;" class="select2-choice" tabind ...

Display the hidden div element when clicked

In my code, I have two div elements as follows: <div class='staticMap'></div> <div class='geolocation-common-map'></div> Initially, the 'geolocation-common-map' div is removed using jQuery when the pa ...

Load a query in Codeigniter when the page is first accessed

Can someone provide guidance on calling a query when the page loads or is ready in CodeIgniter? I am unsure of how to execute a query using AJAX or JavaScript. For example, calling an update query. Thank you. ...

What could be the reason for not receiving a response from my API when using django rest framework with jquery?

Currently, I am utilizing Django REST framework to establish an API that functions seamlessly with cURL. Below is an example of a successful cURL request: curl -H 'Accept: application/json; indent=4' -u ratan:qazwsxedc123 http://127.0.0.1:8000/a ...

`After compilation, the Material UI component `theme.spacing` was not found in the definition

Currently, I am developing a text editor using Material-UI and draft.js, where the functionality is enclosed in Material-UI components. I have been comfortably working with version ~3.9, but for this specific project, I decided to upgrade to 4.0. Although ...

Accessing CSS content from a bundle within the code of an ASP.NET MVC website

I have a website that utilizes bootstrap along with a customized bootstrap theme. The website is designed to send notification emails to its users upon certain actions being triggered. For these notification emails, I want the content to be in well-format ...

Attempting to update the appearance of each item within an array by utilizing the ForEach() method

I am working on a function that dynamically creates new divs based on a specific condition. Once these divs are created, I store them in an array using the .push() method like so: function SnakeBody(){ BodySnake = document.createElement("div"); tabulei ...

Center text in form-control items on Bootstrap 3 grids

Can you provide guidance on achieving proper alignment of inputs and labels across bootstrap grids? I am looking to ensure that the labels are positioned exactly the same as the first column. Please refer to my plunk: http://plnkr.co/edit/jaMAp38Rr1g7BLW ...

The JavaScript code created in CodePen doesn't seem to function properly when integrated into my website

During a learning program, I created a basic random quote generator. The code worked flawlessly on codepen.io. Using a simple JavaScript (jQuery) function, I displayed random quotes along with their authors, which were stored in an array. I ensured that t ...

The dropdown feature on my theme seems to be malfunctioning on Wordpress

I'm having trouble figuring out what I'm doing wrong. The dropdown navigation works fine with the default Wordpress twentyeleven theme, but when I switch to my custom theme, the dropdown stops appearing. Here is the code I'm using: <div ...

Determining the precise spacing needed for a personalized cursor

Currently, I'm facing an obstacle in my attempt to design a custom cursor/crosshair inside a canvas. The problem lies in the Length, Width, and Gap dimensions assigned to the four rectangles that make up the cursor, as it is resulting in an incorrect ...

How can I turn off automatic ellipsis on my IOS device?

Currently, I am working on a web application that involves displaying location descriptions retrieved from an API. The issue I am encountering is that the description is being cut off with an ellipsis after a certain number of lines when viewed on an iPhon ...

Steps for displaying only one collapse at a time using either Bootstrap 3 or Bootstrap 4

As I work on constructing a website from the ground up using Bootstrap 4, I have encountered a roadblock that may be simple for an experienced Bootstrap developer. While I was able to find a solution involving raw Javascript code to collapse one element at ...

Discovering the names of all data-* attributes

Is there a way to extract an array containing all the custom attribute names used in a document or element? (e.g. data-abc, data-pqr) ...

What is the best way to use jQuery ajax to send various row identifiers when updating table data on another PHP page by selecting checkboxes?

When I select both of the green checkboxes, only the most recent table id is sent. The code below includes both the checkbox markup and jQuery function. Checkbox: <div class='md-checkbox has-success'> <input type='checkbox&apo ...

Acquire Formik Validation for the Current Year and Beyond

How can I ensure that the input in Formik is restricted to the currentYear and later years only? const currentYear = new Date().getFullYear(); expiryYear: yup .string() .required('Please select an expiry year') .min(4, `Year format must be grea ...