Maximizing the height of a flex container

Two flex containers are utilized: one for the navigation bar and the other for the page content.

To make the content occupy the entire height of the page, the container's height was set to 100vh. However, a challenge arose when the navigation bar's height needed to be subtracted from the viewport.

/* Mixins Definitions */
/* Actual CSS */
.navbar {
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  margin: 0;
  padding: 0;
  background: rgba(0, 0, 0, 0.81);
  font-size: 14px;
  -webkit-flex-flow: row nowrap;
  justify-content: flex-end;
  align-items: stretch;
  list-style: none;
}
.navbar li {
  margin: 0;
  line-height: 3.2em;
  display: block;
}
.navbar li i {
  margin-right: 10px;
}
.navbar li a {
  color: #9d9d9d;
  text-decoration: none;
  padding: 15px;
  margin: 0;
}
.navbar li a:hover {
  color: #FFFFFF;
}
.navbar li:first-child {
  margin-right: auto;
}
.navbar li:first-child a {
  font-size: 20px;
}
@media all {
}
@media all and (max-width: 600px) {
  .navbar {
    justify-content: space-around;
  }
  .navbar li:first-child {
    display: none;
  }
}
.container {
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  margin: 0;
  padding: 0;
}
.container .sidebar {
  width: 16.5%;
  border-right: 1px solid #ee0005;
}
.container .sidebar .navigation {
  padding: 0;
  list-style: none;
}
.container .sidebar .navigation a {
  padding: 10px 15px 10px 20px;
  text-decoration: none;
  display: inline-block;
  color: #337ab7;
  width: 100%;
}
.container .sidebar .navigation a:hover {
  background-color: #EEEEEE;
}
.container .sidebar .navigation .active > a {
  background-color: #428bca;
  color: #FFFFFF;
}
.container .content {
  flex: 1;
  height: 100%;
  padding: 30px;
}
* {
  box-sizing: border-box;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
}
body {
  background-color: #f8f8f8;
  margin: 0;
  min-height: 100%;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.42857143;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<body>
<ul class="navbar">
    <li><a href="#">Company</a></li>
    <li><a href="#"><i class="fa fa-sign-in fa-lg" aria-hidden="true"></i>Log In</a></li>
    <li><a href="#">Dashboard</a></li>
</ul>
<div class="container">
    <div class="sidebar">
        <ul class="navigation">
            <li><a href="/dashboard">Dashboard</a></li>
            <li class="active"><a href="/hosts">Hosts <span class="sr-only">(current)</span></a></li>
            <li><a href="/trusers">Users</a></li>
        </ul>
    </div>
    <div class="content">Content</div>
</div>
</body>

https://jsfiddle.net/0xj1v8mw/1/

Answer №1

Why not try setting the height on the body instead of .container, along with some additional flex rules?

Check out this fiddle for more details.

/* Mixins Definitions */


/* Actual CSS */

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

.navbar {
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  margin: 0;
  padding: 0;
  background: rgba(0, 0, 0, 0.81);
  font-size: 14px;
  -webkit-flex-flow: row nowrap;
  justify-content: flex-end;
  align-items: stretch;
  list-style: none;
}

.navbar li {
  margin: 0;
  line-height: 3.2em;
  display: block;
}

.navbar li i {
  margin-right: 10px;
}

.navbar li a {
  color: #9d9d9d;
  text-decoration: none;
  padding: 15px;
  margin: 0;
}

.navbar li a:hover {
  color: #FFFFFF;
}

.navbar li:first-child {
  margin-right: auto;
}

.navbar li:first-child a {
  font-size: 20px;
}

@media all {}

@media all and (max-width: 600px) {
  .navbar {
    justify-content: space-around;
  }
  .navbar li:first-child {
    display: none;
  }
}

.container {
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  margin: 0;
  padding: 0;
  flex: 1;
}

.container .sidebar {
  width: 16.5%;
  border-right: 1px solid #ee0005;
}

.container .sidebar .navigation {
  padding: 0;
  list-style: none;
}

.container .sidebar .navigation a {
  padding: 10px 15px 10px 20px;
  text-decoration: none;
  display: inline-block;
  color: #337ab7;
  width: 100%;
}

.container .sidebar .navigation a:hover {
  background-color: #EEEEEE;
}

.container .sidebar .navigation .active>a {
  background-color: #428bca;
  color: #FFFFFF;
}

.container .content {
  flex: 1;
  height: 100%;
  padding: 30px;
}

* {
  box-sizing: border-box;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
}

body {
  background-color: #f8f8f8;
  margin: 0;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.42857143;
}
<body>
  <ul class="navbar">
    <li><a href="#">Company</a></li>
    <li><a href="#"><i class="fa fa-sign-in fa-lg" aria-hidden="true"></i>Log In</a></li>
    <li><a href="#">Dashboard</a></li>
  </ul>
  <div class="container">
    <div class="sidebar">
      <ul class="navigation">
        <li><a href="/dashboard">Dashboard</a></li>
        <li class="active"><a href="/hosts">Hosts <span class="sr-only">(current)</span></a></li>
        <li><a href="/trusers">Users</a></li>
      </ul>
    </div>
    <div class="content">sdfsd</div>
  </div>
</body>

Answer №2

Implemented height: 100% to the html, body, and .container elements:

/* Mixins Definitions */
/* Actual CSS */
html, body {
  height: 100%;
}
.navbar {
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  margin: 0;
  padding: 0;
  background: rgba(0, 0, 0, 0.81);
  font-size: 14px;
  -webkit-flex-flow: row nowrap;
  justify-content: flex-end;
  align-items: stretch;
  list-style: none;
}
.navbar li {
  margin: 0;
  line-height: 3.2em;
  display: block;
}
.navbar li i {
  margin-right: 10px;
}
.navbar li a {
  color: #9d9d9d;
  text-decoration: none;
  padding: 15px;
  margin: 0;
}
.navbar li a:hover {
  color: #FFFFFF;
}
.navbar li:first-child {
  margin-right: auto;
}
.navbar li:first-child a {
  font-size: 20px;
}
@media all {
}
@media all and (max-width: 600px) {
  .navbar {
    justify-content: space-around;
  }
  .navbar li:first-child {
    display: none;
  }
}
.container {
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  margin: 0;
  padding: 0;
  height: 100%;
}
.container .sidebar {
  width: 16.5%;
  border-right: 1px solid #ee0005;
}
.container .sidebar .navigation {
  padding: 0;
  list-style: none;
}
.container .sidebar .navigation a {
  padding: 10px 15px 10px 20px;
  text-decoration: none;
  display: inline-block;
  color: #337ab7;
  width: 100%;
}
.container .sidebar .navigation a:hover {
  background-color: #EEEEEE;
}
.container .sidebar .navigation .active > a {
  background-color: #428bca;
  color: #FFFFFF;
}
.container .content {
  flex: 1;
  height: 100%;
  padding: 30px;
}
* {
  box-sizing: border-box;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
}
body {
  background-color: #f8f8f8;
  margin: 0;
  min-height: 100%;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.42857143;
}
<body>
<ul class="navbar">
    <li><a href="#">Company</a></li>
    <li><a href="#"><i class="fa fa-sign-in fa-lg" aria-hidden="true"></i>Log In</a></li>
    <li><a href="#">Dashboard</a></li>
</ul>
<div class="container">
    <div class="sidebar">
        <ul class="navigation">
            <li><a href="/dashboard">Dashboard</a></li>
            <li class="active"><a href="/hosts">Hosts <span class="sr-only">(current)</span></a></li>
            <li><a href="/trusers">Users</a></li>
        </ul>
    </div>
    <div class="content">sdfsd</div>
</div>
</body>

Answer №3

To style the container class, you can simply apply height: calc(100vh - 46px);.

I noticed in the demo you provided that the height of your navigation bar is 46px.

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

Ensure to verify the presence of a null value within the ngFor iteration

I have a webpage where I am displaying information in buttons. These buttons show various objects from a list along with their corresponding fields (e.g. object.name, object.age, etc.). Sometimes, one of those fields is null. How can I check if a value is ...

having difficulty setting up tabs using uib-tabset

I have been experimenting with dynamically creating tabs using uib-tabset. Each tab is supposed to contain a different form, but the issue I am facing is that the textbox from the first form is being overwritten by the newly generated tab. When I enter d ...

Omit child DIV element in JavaScript and the Document Object Model

I have a situation with two div elements. One is <div class="card" id="openWebsite"> and the other is a sub-division <div class="card__btn"> Here's the issue: When someone clicks on the main div, they get red ...

Guide on sending information using ajax using a button within a form

I've been working on creating a form that utilizes HTML5 validations and integrates Ajax to show the status of mail sending. However, I'm facing an issue where clicking the send button does not initiate the expected action. Form HTML: <div c ...

Is it possible to reuse a variable within a single HTML tag when using Angular 2?

I encountered a strange issue with Angular 2 that may be a bug. I noticed that I couldn't print the same variable in a template twice within the same HTML tag. When I tried to use the following code, it resulted in error messages. <div class=" ...

Having Trouble Aligning Contents in a Row Horizontally using Bootstrap 5

My Images are refusing to align in a horizontal row no matter what I try. Output: https://i.sstatic.net/echGV.png .img-size-square { width: 100%; height: auto; max-width: 400px !important; max-height: 400px !important; } .container-f ...

Is it possible to personalize the Carousel-indicators feature within react-responsive-carousel?

I am currently utilizing the react-responsive-carousel library. I would like to modify the default indicators CSS, changing the dots shape to a line shape. Here is my code snippet: <CarouselWrapper sx={{ marginTop: "124px", maxHeight: & ...

retrieve data with the help of DOMDocument

I'm currently working on extracting a specific value from the HTML snippet below using DOMDocument: <h3> <meta itemprop="priceCurrency" content="EUR">€ <meta itemprop="price" content="465.0000">465 </h3> The value ...

Utilizing Selenium to extract an item from an unsorted list

While I may not be an expert in HTML, I embarked on the journey of creating a basic web scraper using Selenium. My aim was to extract comments from reddit.com, and encountered numerous challenges when trying to identify each element. The specific portion t ...

Where should AJAX-related content be placed within a hyperlink?

When needing a link to contain information for an AJAX call, where is the correct place to include the info? I have typically placed it in the rel attribute, but after reviewing the documentation for rel, it appears that this may not be the right location ...

Choose to conceal beneath the table within Bootstrap 4

As a budding web developer, I am currently using Bootstrap 4 in my project. If you take a look at my preview by clicking here and then selecting "Liczba sztuk," you'll notice that I am facing an issue. The items within the select dropdown are gettin ...

Turn only one bracket on the accordion

When clicking on a specific header, I want only one chevron to rotate instead of all the chevrons rotating. I am currently unsure how to specify which chevron should rotate individually. My project is in ASP.NET MVC 5 and I am using razor view to loop th ...

Video with a personalized play button overlay

I'm currently facing an issue with adding text above a GIF on a video play button specifically for mobile devices. The custom play button is necessary because no mobile device supports video autoplay. I've tried various methods to display the tex ...

Finding the Perfect Placement for an Element in Relation to its Companion

Is there a way to achieve an effect similar to Position Relative in CSS using jQuery? I have developed a tooltip that I want to attach to various objects like textboxes, checkboxes, and other text elements. My code looks something like this: <input i ...

My HTML loop is functioning properly with a variable, however, it is not working as expected

Hi there! Here is a piece of code that includes a loop with a timer. I am trying to change a variable using a button, however, it's not working as expected. <!doctype html> <html> <body> <script> var timer = 1000; var counter ...

Using HTML and JavaScript, we can set two different color values - one for the background and one for the h1 title

I am trying to save two values, one for the h1 tag and one for the body background. I want the user to select color 1 and color 2. When I press the third button, all changes should be applied and the colors should change. I have attempted this but with no ...

Automatically print multiple HTML files with a printer or a Network Printer

I am in search of a way to automatically print multiple HTML files or URLs with just one click from my custom android app. Previously, I utilized the Google Cloud API for this purpose, which involved adding the files to Google Cloud and having them printed ...

Using ion-icon inside a clickable ion-card while applying float: right does not render properly

I am facing an issue with a list of ion-cards that have clickable icons appearing when you hover over them. The problem is that due to the floating nature of the icons, they are not positioned correctly (as shown in the image below) and end up getting cove ...

Creating a Authentic Screw Top Appearance with CSS

I am striving to create a realistic screw head. Here is what I have done so far: <div class="screw"><div class="indent"></div></div> .screw { position: absolute; top: 10px; left: 49%; width: 30px; height: 30px ...

Implementing a click function that toggles between adding and removing a class, keeping track of the number of clicks, and utilizing localStorage to prevent repeated clicking in the

Hi there! I'm currently working on a simple widget that features a clickable icon and a counter next to it. When the icon is clicked, it should toggle between an empty heart and a filled heart using addClass/removeClass. Additionally, each click incr ...