Using a combination of display:flex and setting the height in 'vh' units can effectively adjust the height of the footer

While experimenting with the CSS property display: flex and setting the height as a percentage of the viewport, I encountered an interesting issue with the footer. Despite specifying its height to be 200px (in red), it appears smaller than intended. Upon applying a media query and resizing the browser width, the footer visually expands to 200px (in green).

This behavior was consistently observed across various browsers such as Opera, Firefox, Chrome, IE, and Edge, indicating that it may not be a bug. But how can this anomaly be rationalized?

.header-body-footer-container {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 30px);
}

.body {
    height: 100%;
    min-height: 20px;
}

.footer {
    height: 200px;
    background-color: red;
}

    .footer::after {
        content: " - height is not 200px!";
    }

@media only screen and (max-width: 600px) {
    .header-body-footer-container {
        height: auto;
    }

    .footer {
        background-color: green;
    }

        .footer::after {
            content: " - height is 200px!";
        }
}
<div class="header-body-footer-container">
    <div class="header">header</div>
    <div class="body">body</div>
    <div class="footer">footer</div>
</div>

Answer №1

Update the CSS rule from height:200px; to min-height: 200px; for the class .footer

.header-body-footer-container {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 30px);
}

.body {
    height: 100%;
    min-height: 20px;
}

.footer {
    min-height: 200px;
    background-color: red;
}

    .footer::after {
        content: " - height is not 200px!";
    }

@media only screen and (max-width: 600px) {
    .header-body-footer-container {
        height: auto;
    }

    .footer {
        background-color: green;
    }

        .footer::after {
            content: " - height is 200px!";
        }
}
<div class="header-body-footer-container">
    <div class="header">header</div>
    <div class="body">body</div>
    <div class="footer">footer</div>
</div>

Answer №2

Special thanks to Vikas for providing the solution that helped me understand.

Context:

After realizing my height setting was being disregarded (but Vikas's min-height solution worked), I inferred there must be an implicit max-height. And indeed, it appears that the default value of flex-shrink (which is 1) establishes this implicit max-height.

To prevent the footer from shrinking in size due to flex, I needed to enforce flex-shrink: 0 on the .footer element.

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

How can we extract CSS values from dynamic HTML attributes or classes to steer clear of inline styling?

It is common knowledge that inline styles are considered bad practice and may not be compatible with certain security policies, such as the Content Security Policy. The ideal goal is to achieve this without relying on inline styles: <?php $spacer_heig ...

Altering the display attribute cancels out any opacity transitions

When using JavaScript to change the opacity of an element with transition: opacity 1s, the expected duration of one second is met, as demonstrated here: onload = e => { var p = document.getElementsByTagName("p")[0]; p.style.opacity = 1; }; p { ...

Increasing the height of a vertical division to reach its maximum vertical capacity

Looking for ways to make my html page more visually appealing. It is divided into 4 sections: Header, menu, content, and footer. The header is positioned at the top of the page, while the footer is located at the bottom. In between these sections are the m ...

Is there a way to implement hover behavior for a Material-UI Button within a ButtonGroup component?

When using MUI v5, I am encountering an issue where the first button in the code provided is only half working. The button is initially colored red (both the border and text), however, upon hovering over it, the color of the border changes to blue. This is ...

Using JQuery to nest a header tag within a div element

Having trouble adding a header to a div element. The h1 tag just shows 'loading' and I'm not sure why. <div data-role="page" id="addviewtask"> <div id="header" data-role="header" data-theme="a" data-position="fixed"> ...

What is the significance of vh and percentage values in CSS?

Can anyone help me figure out why the .navbar is not displaying correctly on half of the screen? I think there might be another container causing the issue, but I'm unsure. I've set the body to 100vh to cover the entire screen The .navbar ...

Generating nested arrays using Vue.js

Calculating the total costs of products selected by a user for each company is my current task. Below is the code snippet I am using: let companiesProductPrices = []; let prices = []; this.selectedCompaniesDetails.forEach ...

How to center align text in the media-body using Bootstrap 4 Beta

Struggling to vertically align the text in media-body to the middle when using the new Bootstrap 4 beta version. Tried an approach mentioned in this solution, but it didn't work with the latest version of Bootstrap. Attempted using text-center and a ...

Subdirectory picture styling

Let's take a look at how everything is structured: https://i.sstatic.net/gvyFH.png I am attempting to retrieve an image from a subfolder using a CSS file Although I have attempted several methods, none of them seem to work as expected background-i ...

Arrange the child divs on top of the parent div

<div id="1"> <div id="2"> </div> </div> It is proving challenging to position div2 above div1 in the vertical dimension. Applying a negative top position does not have the desired effect, as it appears that the top border of the ...

Transition effects in the gallery are only triggered once all images have been seen

*Update 2 After identifying that the images were not fading in/out due to being considered unloaded, I resolved the issue by incorporating the initial 3 lines of jQuery code. I anticipate refactoring this and will provide an update once completed. JSFidd ...

Updating Icons Using jQuery Click Function

I have a group of links with icons on them. When a link is clicked, the associated icon changes to a loading image to indicate that the link is active. The issue arises on subsequent clicks, as the loading image does not revert back to the icon and multip ...

Steps to avoid the letter "e" from being copied into the input field using Vue.js

<vs-input :value="contract.value" :class="isMobileOnly ? 'w-full' : 'w-1/2'" class="mx-auto" type="number" ...

Executing Javascript code in Web2Py works on a local environment, but faces issues when the

Trying to incorporate a simple JavaScript code in a web2py VIEW to fetch an external URL has presented some challenges: <script src="http://widgets.aapc.com/countdown/aapc_cdwidgetbox_220.js"></script> The script runs smoothly locally but fai ...

Validation of Button Groups and Automatic Disabling after Initial Click with HTML, CSS, and JavaScript

Criteria: Upon clicking a button from the selection of four buttons, all other buttons should become disabled except for the Next button. An alert window must appear when the Next button is clicked without selecting any other buttons, preventing navigatio ...

Counting the days: how to calculate without using negative numbers

After performing the calculation, the result shows -30. Is there a way to eliminate the negative sign and get 30 instead? $days = (strtotime(date("Y-m-d")) - strtotime($info['expiredate'])) / (60 * 60 * 24) echo abs($days); Any suggestions on ...

Arrange the text in a div element in a horizontal direction

While developing a friends list script for a website, I encountered an interesting UI issue in the chat section. When a lengthy chat message is added to the chat content div with the CSS attribute overflow: scroll, it causes the div to stretch out horizont ...

My website is currently experiencing issues with all of the CSS references not working. This problem applies to both external and internal CSS files and consistently

I'm encountering 404 errors when trying to load both internal and external files on a website through my browser. My code snippet looks like this: <link rel="stylesheet" type = "text/css" href='anoceanofsky.css'/> Despite clearing m ...

Unable to access POST data from AJAX request in an external PHP script

I've exhausted all my options searching online, but I still can't figure out how to retrieve data from a POST array in an external PHP file. Here is the HTML code for the text field: <form> <textarea id="comment_text_area" name="comment ...

Parsing HTML using PHP's Simple HTML DOM Parser

I'm having trouble extracting specific information from HTML code using a DOM parser. <div id="posts"> <div class="post"> <div class="user">me:</div> <div class="post">I am an apple</div> &l ...