Having trouble with a basic CSS Height example, could use some assistance. Thank you in advance!

Could someone shed light on why my straightforward CSS 100% example isn't working as expected?

HTML:

<div id="header">header</div>
<div id="nav">Nav</div>
<div id="title">title</div>
<div id="content">
    Content
</div>

CSS:

html {
    height:100%;
    padding: 0;
    margin: 0;
}
body {
    height: 100%;
    padding: 0;
    margin: 0;
}
#header {
    background-color:red;
    padding: 0;
    margin: 0;
}
#nav {
    background-color:gray;
    padding: 0;
    margin: 0;
}
#title {
    background-color:azure;
    padding: 0;
    margin: 0;
}
#content {
    background-color:antiquewhite;
    height:100%;
    padding: 0;
    margin: 0;
}

I expected no vertical scroll bar to appear, but it does.

View the fiddle demo here: http://jsfiddle.net/codeowl/9wABW/

Appreciate your insights,

Best regards,

Scott

UPDATE:

This is what I came up with:

I implemented a stack and fill approach which looks like this. Unfortunately, I couldn't access the window in JavaScript on Fiddle, so I will paste the code:

CSS:

#header {
    background-color:red;
}
#nav {
    background-color:gray;
}
#title {
    background-color:azure;
}
#content {
    background:green;
}

HTML:

<div id="header" class="stack-y">header</div>
<div id="nav" class="stack-y">Nav</div>
<div id="title" class="stack-y">title</div>
<div id="content" class="fill-y">
    <div data-role="splitter"
        data-panes="[
        { scrollable: false, collapsible: true, size: '300px' },
        { scrollable: false, collapsible: true }
        ]" 
        class="fill-y">
        <div>
            Left Pane
        </div>
        <div>
            Right Pane
        </div>
    </div>
    <div class="stack-y">Test Content</div>
</div>

Java Script:

$(document).ready(function () {
    var fResizeLayout = null;

    fResizeLayout = function() {
        var aFillElements = $('.fill-y');
        $.each(aFillElements, function (i, e) {
            var p = null,
                iPY = 0,
                iY = 0,
                iH = 0;
            e = $(e);
            p = e.parent();
            if (p.prop('tagName') === 'body') { iPY = $(window).height(); }
            else { iPY = p.innerHeight(); }
            e.siblings('.stack-y').each(function () {
                iY += $(this).outerHeight(true);
            });
            iH = (iPY - iY - parseInt(e.css('border-top-width'), 10) - parseInt(e.css('border-bottom-width'), 10));
            e.height(iH);
        });
        kendo.resize($('#content'));
    };

    kendo.init($('#content'));
    fResizeLayout();

    $(window).on('resize', function () {
        if (this.resizeTO) clearTimeout(this.resizeTO);
        this.resizeTO = setTimeout(function () {
            $(this).trigger('resizeEnd');
        }, 200);
    });

    $(window).on('resizeEnd', function () {
        fResizeLayout();
    });            
});

To make the kendo part work, ensure you include the necessary libraries:

<link href="http://cdn.kendostatic.com/2013.3.1119/styles/kendo.common.min.css" rel="stylesheet" type="text/css" />
<link href="http://cdn.kendostatic.com/2013.3.1119/styles/kendo.default.min.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://cdn.kendostatic.com/2013.3.1119/js/kendo.all.min.js"></script>

If you don't need the keno libraries:

HTML:

<div id="header" class="stack-y">header</div>
<div id="nav" class="stack-y">Nav</div>
<div id="title" class="stack-y">title</div>
<div id="content" class="fill-y">
    Test Fill Content
</div>
<div class="stack-y">Test Stacked Content</div>

JavaScript:

$(document).ready(function () {
    var fResizeLayout = null;

    fResizeLayout = function() {
        var aFillElements = $('.fill-y');
        $.each(aFillElements, function (i, e) {
            var p = null,
                iPY = 0,
                iY = 0,
                iH = 0;
            e = $(e);
            p = e.parent();
            if (p.prop('tagName') === 'body') { iPY = $(window).height(); }
            else { iPY = p.innerHeight(); }
            e.siblings('.stack-y').each(function () {
                iY += $(this).outerHeight(true);
            });
            iH = (iPY - iY - parseInt(e.css('border-top-width'), 10) - parseInt(e.css('border-bottom-width'), 10));
            e.height(iH);
        });
    };

    fResizeLayout();

    $(window).on('resize', function () {
        if (this.resizeTO) clearTimeout(this.resizeTO);
        this.resizeTO = setTimeout(function () {
            $(this).trigger('resizeEnd');
        }, 200);
    });

    $(window).on('resizeEnd', function () {
        fResizeLayout();
    });            
});

Special thanks to Carlos for the resizeEnd implementation:

Thank you to everyone who contributed!

Hoping this solution can be helpful to others.

Warm regards,

Scott

Answer №1

Update

Here is a solution that perfectly achieves the desired layout, utilizing display:table-row in the nested content.

html {
    height:100%;
    padding: 0;
    margin: 0;
}
body {
    height: 100%;
    padding: 0;
    margin: 0;
}
#header {
    background-color:blue;
    padding: 0;
    margin: 0;
    display: table-row;
    height:1px;
}
#nav {
    background-color:black;
    padding: 0;
    margin: 0;
    display:table-row;
    height:1px;
}
#title {
    background-color:skyblue;
    padding: 0;
    margin: 0;
    display:table-row;
    height:1px;
}
#content {
    background:yellow;
    padding: 0;
    margin: 0;
    display:table-row;
}
#wrapper {height:100%;width:100%;margin:0;padding:0;display:table}

<div id="wrapper">    
    <div id="header">header</div>

    <div id="nav">Nav</div>
    <div id="title">title</div>
    <div id="content">
        Content
    </div>
</div>

Take a look at your revised fiddle

To achieve the necessary structure, it's essential to have a wrapper for the elements and experiment with table CSS properties like display: table-row as per your specific layout requirements.

<div id="wrapper">    
    <div id="header">header</div>

    <div id="nav">Nav</div>
    <div id="title">title</div>
    <div id="content">
        Content
    </div>
</div>



html {
    height:100%;
    padding: 0;
    margin: 0;
}
body {
    height: 100%;
    padding: 0;
    margin: 0;
}
#header {
    background-color:blue;
    padding: 0;
    margin: 0;
}
#nav {
    background-color:black;
    padding: 0;
    margin: 0;
}
#title {
    background-color:yellow;
    padding: 0;
    margin: 0;
}
#content {

    padding: 0;
    margin: 0;
}
#wrapper {height:100%;margin:0;padding:0;background-color:lightcoral;}

Answer №2

To achieve the desired result, replace height:100% with height:auto in your CSS code. This simple adjustment should solve the issue for you. 😊

Answer №3

Consider the implications of your actions. By instructing content to occupy 100% of its parent element, namely body, you are essentially expanding it to fill the entire window.

As a result, even though content will take up the full height of the browser, the cumulative size of all four divs will actually be 100% (body size) plus the heights of header, nav, and title.

To rectify this issue, one solution is to ensure that the combined heights of the inner contents equate to 100%, adjusting the percentages as necessary. Here is an example for reference:

http://jsfiddle.net/9wABW/3/

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

Tips for properly capitalizing the first letter of each word connected through ng-model

After receiving a JSON response from an API, I am binding it to form elements using ng-model. The data returned from the API is in UPPERCASE. How can I apply a filter or CSS to capitalize the first letter of each word? <input type="text" ng-model="s ...

Troubleshooting: Issues with locating CSS and JS files (404 error) while utilizing URL parameters within Django platform

I've designed a dashboard page that showcases various graphs. The page automatically updates the graph data every hour. You can access the page at the following URL: http://localhost/dashboard I'd like to give users the option to specify the ...

Ways to extract information from a URL when utilizing JQuery code

Code snippet: $(document).ready(function(){ $(".uni_type").click(function(){ uni_type = this.id; location.href = "filter-colleges.php?uni_type="+uni_type; }); }); HTML code: <ul> <li class="uni_type" id="central univ ...

Struggling to locate the distinctive XPath for the button

I am facing a situation where the XPath is non-unique and identifies 3 elements on the page that change positions upon refresh: <div class="col-xs-12 Hover"> <button data-testid="continueCheckoutButton" ng- class="continueDellMetricsC ...

Popover by Bootstrap does not appear on the designated button

I've encountered a strange issue with my popover being disconnected from the button. I've managed to recreate the problem in this Fiddle, but I'm struggling to pinpoint the source of the problem. I suspect it could be a CSS issue, but so far ...

MS Edge modifies the attribute's empty value to 1

I have written a JavaScript code to extract values from a list, but in the Windows Edge browser, it returns a value of 1 even when the actual value of the <li> tag is blank. For example: HTML Code <ul> <li value="">Test 1</li&g ...

Personalized Pinterest button to link to a custom URL (Text Link, Image, or Both)

I've been searching for a solution without success. I'm looking to customize the image for my Pinterest (Pin It) button and pin a specific image by URL, not just the current page. Here is the custom link I created: <a href="http://pinterest. ...

Is it possible to automatically close navigation dropdowns when the screen size changes?

I am using a bootstrap 4 navbar with dropdowns that open with CSS animation/fade-in on desktop, and a separate toggle button for mobile. Everything works fine, but when I open the dropdowns on mobile and then resize the window screen, they remain open whic ...

Displaying genuine HTML content in a React application using Algolia Instantsearch

After setting up a demo app using React with an Algolia search feature, I uploaded some indices into Algolia. The content consists of raw HTML. Is there a way to display this content as real HTML using Algolia? ...

How to align button text and other ion-col text at the bottom in Ionic 2

Here is the code snippet I am working with: <ion-row><ion-col ><button ion-button outline no-padding large (click)="setUserName()">D</button>ay in the life </ion-col></ion-row> I find the layout unattractive as the le ...

Creating a three-column layout with position fixed in the center

I'm struggling with achieving a maximum width of 400px for the entire set of three columns and centering them. ---------------------------------------------------- | | | |----------------------------- ...

Image placed as background for registration form

In my Vue project, I am trying to create a login form with an image as the background. However, I am facing an issue where the image is not displaying on the page. Can someone guide me on how to add a picture behind the form? HTML: Below is the HTML code ...

The header does not extend fully to the top edge

I am attempting to relocate the header to the topmost position on my webpage. However, despite my efforts, it remains in its current position. You can view an example of the issue in this fiddle http://jsfiddle.net/DCtH7/4/ Upon examining the fiddle, you ...

Ways to display the chosen selection post-selection of the option

My form has a table structure in HTML as shown below: <tr> <td>Quotation Category<span class="required" style="color: red">*</td> <td> <div class="form-group col-md- ...

Activate the DecimalPad Keyboard Type for Mobile Safari Using HTML

Query: Can the keyboard type decimalPad be activated in a HTML form displayed on mobile Safari (i.e. a numeric keyboard with a decimal point)? Note: While a "numeric" keyboard can be achieved by setting <input type="number" pattern="d\*" />, I ...

Creating a horizontal alignment for social media icons

Need assistance with aligning my social icons horizontally. I initially tried floating them to the left or using inline-block, but it's not working as expected. Any help would be greatly appreciated. Thanks! <!-- Customizing Social Network Icons ...

Steps to generate a unique URL for launching a Bootstrap Modal directly

I've encountered a problem where I'm trying to trigger a Modal in window B using a URL when clicking on an image link in window A. The image is nested inside an anchor tag like this: <a href="myserver.mydestinationB.com#myModal><img src= ...

Employing a selection menu within a form to execute a SQL SELECT query

Thank you for taking the time to read my question. I am currently in the process of developing a form that will allow users to search a database for cards used in a collectible card game. One important feature I want to include is a dropdown menu that ena ...

BOOTSTRAP: changing the layout of panels in various sizes for mobile devices

I'm struggling with how to reorganize my panels on mobile devices. Each panel has a different size. Please refer to the attached screenshot for the page layout on large screens (col-lg): https://i.sstatic.net/xcqaT.png EDIT: The layout on large scre ...

Error: Trying to access the 'cvs' property of an undefined object in a class during initialization

Recently, I created a class called "genGameObject" which is intended to generate a Game Object and draw content inside of it. Initially, I suspected it was just a minor typing error, but after thorough inspection, I couldn't find any (If you happen t ...