Utilize the entirety of the available space in a flexbox layout using child divs in HTML/CSS

Here is a link to a fiddle: https://jsfiddle.net/8dvhx0ap/1/

Below is the HTML:

.sidenav {
  height: 100%;
  width: 160px;
  position: fixed;
  top: 0;
  left: 0;
  background-color: black;
  display: flex;
}

.smallDiv {
  background-color: green;
  padding: 10px;
}

.bigDiv {
  background-color: blue;
  padding: 10px;
  width: 60vw;
  height: 60vh;
}

.main {
  margin-left: 160px;
  flex: 1 1 auto;
}

.container {
  width: 100%;
  display: block;
  box-sizing: border-box;
  padding: 20px;
}

.grid {
  justify-content: center !important;
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  box-sizing: border-box;
}
<div id="app">
  <div style="height: 50px; background-color: red;"></div>
  <aside class="sidenav"></aside>
  <div class="main">
    <div class="container">
      <div class="grid">
        <div class="smallDiv">
          <input type="text"><br><br>
          <input type="text"><br><br>
          <input type="text"><br><br>
          <input type="text"><br><br>
          <input type="text"><br><br>
        </div>
        <div class="bigDiv">

        </div>
      </div>
    </div>
  </div>
</div>

This setup aims to have the blue div fill the empty space dynamically. When viewed on a standard screen, the green div should be on the left, and the large blue area next to it should take up all available space without any scrollbars. Resizing the page will adjust the size of the blue div accordingly. On smaller screens, like phones, the two divs should stack vertically.

How can I achieve the goal of having the blue div fill the empty space in the layout?

Answer №1

Upon inspection, I observed that the top red div was partially obscured by the black sidenav div. This indicated a need for HTML restructuring. By adding content, we can better understand how it should function.

Additionally, you may wish for the black sidenav div to be hidden on mobile devices, which can be achieved through an appropriate media query.

        .container {
            display: flex;
            flex-direction: row;
        }
        
        .main {
            flex-grow: 1;
            display: flex;
            flex-direction: column;
        }
        
        .sidenav {
            flex-basis: 160px;
            flex-shrink: 0;
            color: white;
            background-color: black;
            display: flex;
        }
        
        .topDiv {
            flex-grow: 1;
            height: 60px;
            background-color: red;
        }
        
        .grid {
            display: flex;
            flex-direction: row;
        }
        
        .smallDiv {
            background-color: green;
            padding: 10px;
        }
        
        .bigDiv {
            background-color: blue;
        }
        
        @media only screen and (max-width: 600px) {
            .grid {
                flex-direction: column;
            }
        }
    <div class="container">
        <aside class="sidenav">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Vel facilis alias incidunt aperiam sequi a earum delectus nam similique nostrum, tenetur esse aliquid veritatis dicta tempore? Error asperiores tempore illo!</aside>
        <div class="main">
            <div class="topDiv">
                <h1>A Heading</h1>
            </div>
            <div class="grid">
                <div class="smallDiv">
                    <input type="text"><br><br>
                    <input type="text"><br><br>
                    <input type="text"><br><br>
                    <input type="text"><br><br>
                    <input type="text"><br><br>
                </div>
                <div class="bigDiv">
                    Lorem ipsum dolor sit amet consectetur, adipisicing elit. Amet ab doloribus nostrum deleniti debitis, et odit tempora obcaecati perferendis dolorum ratione asperiores odio ipsum. Sequi consequatur qui nisi quibusdam praesentium!
                </div>
            </div>
        </div>
    </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

Assign HTML Form Input Value to JavaScript Variable

Is there a way to display the user's input in a form before they submit it? I have set up text elements on the page with specific IDs, but when I try to assign the values of these elements to the input fields, it doesn't seem to be working as exp ...

The Main page is being graced by a floating Twitter Bootstrap video

When trying to display a video next to a paragraph, I encountered an issue where the paragraph was taking up 100% of the screen and the video was floating over it, obstructing part of the text. Despite setting the row and cols, the layout wasn't behav ...

Troubleshooting problem with CSS layout when adjusting the height of a section

I recently delved into creating a simple website using HTML5 and have successfully set up the site structure. Everything has been smooth sailing so far. However, I've encountered a section of the site that seems to have a mind of its own. When I ass ...

Increase the character count when two spans contain data

I am currently working with the code provided below. The user interface allows users to choose either a single date or a range of dates. These dates are displayed within span tags, with a "-" symbol intended to be shown if there is a valid toDate. However, ...

Tips for Formatting Text with Line Breaks in an HTML Textbox

Kemandirian spesies ialah keupayaan haiwan dan tumbuhan untuk mengekalkan spesiesnya bagi mengelakkan kepupusan. Ciri dan tingkah laku khas haiwan untuk melindungi diri daripada musuh seperti: (i) Memutuskan anggota badan; (ii) Menyembur dakwat hitam; (iii ...

The JavaScript code is not running as expected!

i have this index.html file: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" ...

No matter how tall I make it, the scroll bar just won't stop showing

Check out this fiddle for more information: http://jsfiddle.net/LAueQ/ Even after increasing the height of the campaignDiv div, the scrollbar keeps showing up. If possible, I would like to avoid using overflow: hidden. ...

Applying Compiled CSS file in Node.js using Express and SASS not working as expected

Recently, I've delved into utilizing NodeJS with Express to serve my pug files. In addition, I decided to incorporate the "express-sass-middleware" to compile and serve the scss/css files in my project. While everything seems to be functioning as expe ...

Tri-party class switch-up

I successfully implemented two radio buttons to toggle between alternative texts: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...

Full height Ion-slides component in Ionic Framework version 3

I have encountered an issue with the ion-slides component where the slide content height exceeds the view and prevents scrolling to see the rest of the content. How can I scroll down to view the entire slide content? Here is the HTML code I am using: < ...

Organize and conceal identical columns in an Angular 4+ table

My goal is to organize the table by both name and first name. If there are any duplicate entries, I plan to keep just one line visible while adding an accordion feature to display the duplicates in a collapsed manner. Included below is the TS and HTML ...

Tips for aligning input fields in a single row within an HTML card

Is there a way to arrange Quarter and Month in the same row, and Week and Date in a row below? <base target="_top"> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR ...

The footer should never be positioned below the sidebar

Hello, I'm trying to position my footer below my side navigation bar. It's working fine with the header, but I can't seem to figure out how to do it for the footer. Also, another question - how can I ensure that the text on a smaller screen ...

Is there a way to transform JSON file data into an HTML table without relying on variables?

The json file I have now holds data from an HTML form in a specific format. This information will be updated whenever a new person fills out the form. { "name": "donald", "age": "34", "gender": "male", "email": "<a href="/cdn-cgi/l/email-protection" cl ...

Can you explain the contrast between "#msg" and "#msg div" when used in a style tag?

<html> <head> <style> #msg { visibility: hidden; position: absolute; left: 0px; top: 0px; width:100%; height:100%; text-align:center; ...

When using Next.js, I have found that the global.css file only applies styles successfully when the code is pasted directly into the page.tsx file. However, when attempting to

I recently started exploring nextjs and came across this video "https://www.youtube.com/watch?v=KzqNLDMSdMc&ab_channel=TheBraveCoders" This is when I realized that the CSS styles were not being applied to HeaderTop (the first component cre ...

Tips for enabling only a single div to expand when the "read more" button is clicked

I'm having trouble with my blog-style page where I want only one "read more" link to expand while collapsing the others. I've tried various jQuery methods and HTML structures, but haven't been able to achieve the desired result. Can someone ...

Ways to center align text in a div vertically

I'm working with 2 divs that are floating side by side. The left div contains a part number, which is only one line. The right div holds the product description, which can span multiple lines. I am looking for a way to vertically align the text in t ...

Is there a way to solve the issue of Textarea restricting users to input only one character on iOS devices?

Working with Framework7 and Cordova has been great overall. However, I have encountered an issue specifically on IOS devices (both simulator and real device) where I am unable to enter text completely into a textarea. Whenever I try to input text using th ...

Looking for a list that you can edit with CSS styling?

I'm struggling to create a flexible list that can be easily modified. My goal is to have an ordered list with unique labels such as 1,2,3,4,5,6,7,8,9,0,A,B,!,# Although I've managed to number the list items, I am stuck on adding the extra label ...