htmlcss

CSS to create navigation pane and body


I am trying to create a simple view with HTML/CSS as follows:

enter image description here

The left-hand navigation pane needs to be 100px wide (fixed/absolute), and the right pane needs to take up the remaining space. Both panes need to fill the entire view (100% of height, less the header height).

I can get what I want (roughly) with the following HTML/CSS, but not sure this is the best approach. Also I can't seem to find a method that allows the body to fill the remaining viewport height.

.navigation {
  display: inline-block;
  width: 100px;
  /* this needs to be absolute */
  height: 800px;
  /* want to avoid absolute and fill to bottom of viewport */
  background-color: aqua;
}

.body {
  display: inline-block;
  height: 800px;
  /* want to avoid absolute and fill to bottom of viewport */
  width: 1820px;
  /* want to avoid absolute and fill to edge of viewport */
  background-color: blue;
}
<div class="main">
  <div class="navigation">
    Navigation
  </div>
  <div class="body">
    <div class="body">Body</div>
  </div>
</div>


Solution

  • For a more "up to date" solution, you could consider using grid layout, here's a read that might peak your interest.

    And here's an example that how you could write your layout with grid if you don't mind flattening your HTML structure a bit:

    body {
      /* make sure the body fill the full height of the window */
      height: 100vh;
      margin: 0;
      
      display: grid;
      overflow: hidden;
      
      /* define the layout of the page with 2 columns and 2 rows */
      grid-template-areas:
        'nav  nav'
        'side main';
        
      /* define the rows' heights, 40px for top nav and main takes remaining area */
      grid-template-rows: 40px 1fr;
      
      /* define the columns' widths, 100px for navigation and main takes remaining area */
      grid-template-columns: 100px 1fr;
    }
    
    nav {
      grid-area: nav;
      background-color: green;
    }
    
    aside {
      grid-area: side;
      background-color: aqua;
    }
    
    main {
      grid-area: main;
      background-color: blue;
      overflow-y: auto;
    }
    <nav>Top</nav>
    <aside>Navigation</aside>
    <main>
      <p>Lorem ipsum</p>
      <br><br><br><br><br>
      <p>Lorem ipsum</p>
      <br><br><br><br><br>
      <p>Lorem ipsum</p>
      <br><br><br><br><br>
      <p>Lorem ipsum</p>
      <br><br><br><br><br>
      <p>Lorem ipsum</p>
      <br><br><br><br><br>
      <p>Lorem ipsum</p>
      <br><br><br><br><br>
      <p>Lorem ipsum</p>
    </main>