The minimum height of the body and main elements using Flexbox

Link to code snippet

I am attempting to make the body expand using min-height, but it doesn't seem to be working as expected. Despite researching similar topics, I am having trouble understanding the issue.

HTML and CSS Code:

body,
html {
  padding: 0;
  margin: 0;
  min-height: 100%;
}

body {
  display: flex;
  background: #eee;
  flex-direction: column;
}

.menu {
  background: red;
  color: #fff;
  padding: 10px;
}

.wrap {
  display: flex;
}

.sidebar {
  background: #ddd;
  width: 300px;
}

.main {
  background: #ccc;
  flex: 1;
}

.footer {
  background: #000;
  color: #fff;
  padding: 10px;
}
<div class="menu">Menu</div>

<div class="wrap">
  <div class="sidebar">Sidebar</div>
  <div class="main">Main</div>
</div>

<div class="footer">Footer</div>

The desired outcome is for the main section to stretch and fill the height if it is less than 100%.

Answer №1

Implement flex: 1 on the element you want to center:

.Container {
  display: flex;
  min-height: 100vh;
  flex-direction: column;
}

.Content {
  flex: 1;
  background-color:#bbb;
}
<body class="Container">
  <header>This is the header text ☺</header>
  <main class="Content">…</main>
  <footer>This is the footer text ☻</footer>
</body>

Answer №2

If you're struggling to make the min-height property work with relative units, especially in IE11, there's a simple workaround.

The primary function of min-height is to take precedence over the height property when the height value is smaller than the specified min-height. This concept is straightforward. However, an issue arises when using a relative unit (% or vh) for min-height without setting a corresponding height. Since the relative unit lacks a specific reference point, it can lead to unexpected behavior.

In most modern browsers, except for Internet Explorer, adjusting the unit from % to vh can resolve the problem:

body {
  min-height: 100vh;
}

For Internet Explorer compatibility, setting a minimal height value is necessary (to be overridden by min-height):

body {
  height: 1px;
  min-height: 100vh;
}

Alternatively, if you prefer to stick with the % unit, the rules should also be applied to the html element:

html, body {
  height: 1px;
  min-height: 100%;
}

To create a cross-browser solution, ensure that height: 1px is set on the body element and include flex-grow: 1 for the .wrap container to allow it to expand faster than the menu and footer:

body,
html {
  padding: 0;
  margin: 0;
  height: 1px; /* added */
  min-height: 100%;
}

body {
  display: flex;
  background: #eee;
  flex-direction: column;
}

.menu {
  background: red;
  color: #fff;
  padding: 10px;
}

.wrap {
  display: flex;
  flex-grow: 1; /* added */
}

.sidebar {
  background: #ddd;
  width: 300px;
}

.main {
  background: #ccc;
  flex: 1;
}

.footer {
  background: #000;
  color: #fff;
  padding: 10px;
}
<div class="menu">Menu</div>

<div class="wrap">
    <div class="sidebar">Sidebar</div>
    <div class="main">Main</div>
</div>

<div class="footer">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 utilize nav and main-nav in CSS?

Working with HTML and CSS <nav class="nav main-nav"> </nav> Styling with CSS main-nav li{ Padding:0 5% } I am a beginner in HTML and CSS and facing an issue. I noticed that when using main-nav in CSS, I'm not getting the de ...

Guide to configuring an Angular Material Footer using Flex-Layout

Could someone help me with setting up the footer in my Angular Material app? I want it to: stick to the bottom when the content height is smaller than the view-port move down or get pushed down when the content height exceeds the view-port One important ...

Even though the if statement has been implemented, the page continues to show null objects

So, I have an if statement that filters out null objects and it's working fine. However, when I try to insert something in join() like join("li"), all null objects are displayed on the page as shown in the image. I just want to display objects that ar ...

CSS :hover activates only when the mouse is in motion

Looking at a simple example I put together: HTML <div id="sample"></div> CSS #sample { width:400px; height:400px; background-color:green; display:none; } #sample:hover{ background-color:red; } It's a hidden DIV tha ...

React does not support having two :focus selectors on a single JSX element

I'm working with a React script that includes a specific element. I want to apply more than one :focus-within selector in CSS, but for some reason only the dropdown selector is being expressed when I focus on the element. Here's the code: React J ...

The data labels in the main chart of a Highcharts column with drilldown appear blurry, with the exception of the columns that have been drilled

When setting up a Column Chart with Drilldown: The main chart displayed data labels in a blurred black color. However, when clicked, the next columns that appear (the drilled down columns) are clear and easy to read (white color, no blur). I discovered a ...

What is the best way to ensure that the scroll position on each page in shinydashboard remains unaffected by changes in the scroll position on other pages?

When navigating between pages A and B in my shinydashboard app, I noticed that the scroll position of one page affects the scroll position of the other. How do I make the scrolls independent? To illustrate my issue, I've included a stable shinydashbo ...

Scroll Magic not cooperating with Simple Tween local copy

Greetings! I am attempting to replicate this simple tween example using scrollmagic.js. It functions properly on jsfiddle. To confirm, I added scene.addIndicators() to observe the start, end, and trigger hook. It functions flawlessly. As displayed in the ...

The assigned javascript/CSS style is not being applied to every button

In an attempt to customize a list, I have written the following code: <script> for (n = 0; n < 3; n++) { var node = document.createElement("li"); var btn = document.createElement("button"); $("button").css({ 'background-c ...

Trouble with Bootstrap Popover's visibility

My current setup involves a popover that is not initializing properly. The code I am using is shown below: HTML: <div data-toggle="popover" data-placement="bottom" data-content="xyz">...</div> JavaScript: $(function () { $('[data-t ...

What is the method for creating text outlines using html5/css3?

I have been experimenting with using shadows to outline text, which is a solution I came across in various Stack Overflow threads. However, support for text-outline and text-stroke features is currently limited. I am encountering an unusual issue with on ...

Ensure that only one button from the collection is activated

One of the features on my website includes 3 blocks with buttons. These buttons trigger a change in the displayed picture when clicked. However, it is crucial to ensure that when a new track is activated, the previous button returns to its original state. ...

Ensure that the divs are properly aligned and construct columns within a single column

I've been working on creating a step bar that needs to adapt to any number of steps provided by the backend. Here's what I have so far: https://i.sstatic.net/cj4qZ.png It looks fine, but connecting the circles with bars would be beneficial. How ...

Change the background color of the selectInput component in Shiny to fit your

Take a look at this sample shiny app: library(shiny) ui <- fluidPage(tags$head(includeCSS("www/mycss.css")), selectInput("foo", "Choose", width = '20%', multiple = F, selected = "red1", choices = list(red = c ...

In Angular, you can add a unique design to an HTMLElement by applying a

I'm currently facing an issue with Typescript where I am attempting to apply a style on a HTMLElement. Below is the code snippet: styleChoice(element:HTMLElement){ console.log(element); element.style.background="rgba(228, 48, 48, 0.2)&qu ...

Text area in the footer of a Bootstrap-4 modal that can be scrolled through

In my Angular-6 project, I am implementing a Bootstrap-4 modal. The Modal-Header has a fixed height, Modal-Body is scrollable, and Modal-Footer has a variable height but is not scrollable. Below is a snippet of my basic HTML: /*for debugging purpose*/ ...

What is the best method to assign a CSS class to specific nodes within a TreeView using CodeBehind?

I need to apply unique styling to certain nodes in the codebehind. Within my TreeView, parents have two categories of children. One category matches the parent type (e.g. organizationalUnit), while the other does not (e.g. organizationalMembers). I aim t ...

Locate the immediate parent element and assign a class to it using jQuery

CSS: <div class="views-row views-row-10 views-row-even views-row-last"> <div class="sonuc"> <span>text1</span> </div> </div> <div class="views-row views-row-11 views-row-even views-row-last"> ...

Border color options for select boxes

Currently, I am working on my first Django-bootstrap project and dealing with a select box. I am trying to customize it so that when clicked, the border of the select box and its options turns yellow, which is working well. However, there is an additional ...

Load content dynamically through AJAX while also running JavaScript and CSS

For quite some time now, I've been trying to solve the mystery of loading a complete webpage through AJAX while ensuring that all JavaScript and CSS are executed properly. Unfortunately, my attempts have only resulted in displaying plain text without ...