0

I want to layout a web page with a basic 3 section layout. There is a fixed, static top navigation bar on top. Just below it are two sections that will take up the full width and remaining height of the page. There is a fixed sidebar (non-collapsible) and to the right of it is the main pane that will show content (read dashboard).

Currently, I'm just hard-coding percent widths for the sidebar and main pane. Applying a min/max width to the sidebar breaks the layout. I can deal with it being a fixed width if need be.

How can I get the main pane section to take up the remaining width not used by the side bar if I assign the sidebar min/max/fixed width?

html {
  height: 100%;
}
body {
  padding: 0px;
  height: 100%;
}
div {
  text-align: center;
  color: white;
}
.top-nav {
  background-color: green;
  height: 65px;
  margin: 0px;
  padding: 0px;
}
.content {
  height: 100%;
  width: 100%;
}
.side-bar {
  background-color: blue;
  width: 10%;
  height: 100%;
  float: left;
}
.main-pane {
  background-color: red;
  height: 100%;
  width: 90%;
  float: right;
}
<div class="top-nav">
  Fixed Top Navigation
</div>
<div class="content">
  <div class="side-bar">
    Fixed Side Bar
  </div>
  <div class="main-pane">
    Main Pane
  </div>
</div>

Here is the above code in a pen.

Edit: I am open to other solutions that tackle this problem a completely different way if there are more preferred solutions e.g. flexbox.

Alan Thomas
  • 1,006
  • 2
  • 14
  • 31
  • by fixed do you mean they need to scroll with the user or just not change in dimension – TedMeftah Dec 16 '16 at 21:25
  • 1
    @Mohamed-Ted They must scroll with the user, i.e. only the content in the main pane is scrollable. – Alan Thomas Dec 16 '16 at 21:28
  • Please check this question: http://stackoverflow.com/questions/26001765/research-on-creating-grids-that-combine-percentage-and-static-e-g-pixel-value – luke Dec 16 '16 at 22:02

5 Answers5

3

I changed the sidebar to a fixed size, and changed the positioning to absolute. The main content panel I compensated for the width of the sidebar with padding. Please check out updated pen. http://codepen.io/anon/pen/ENGbva?editors=1100

<div class="top-nav">
  Fixed Top Navigation
</div>
<div class="content">
 <div class="side-bar">
   Fixed Side Bar
 </div>
 <div class="main-pane">
   Main Pane
 </div>
</div>

html{
  height: 100%;
}
body {
  padding: 0px;
  height: 100%;
}
div{
  //text-align: center;
  color: white;
}
.top-nav {
  background-color: green;
  height: 65px;
  margin: 0px;
  padding: 0px;
}
.content {
  height: 100%;
  width: 100%;
  position: relative;
}
.side-bar {
  background-color: blue;
  width: 200px;
  height: 100%;
  float: left;
  position: absolute;
  left: 0;
  z-index: 999;
}
.main-pane {
  background-color: red;
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  z-index: 1;
  padding-left: 200px;
}
oompahlumpa
  • 495
  • 2
  • 10
1

Here is a solution with a little bit more backward compatibility. Essentially you're placing both divs on top of one another but giving your main page div a margin so that the content is not hidden by the nav bar.

    html {
      height: 100%;
    }
    body {
      padding: 0px;
      height: 100%;
    }
    div {
      text-align: center;
      color: white;
    }
    .top-nav {
      background-color: green;
      height: 65px;
      margin: 0px;
      padding: 0px;
    }
    .content {
      height: 100%;
      width: 100%;
    }
.side-bar {
  background-color: blue;
  width: 150px;
  height: 100%;
  float: left;
  position: absolute;
}
.main-pane {
  background-color: red;
  height: 100%;

  margin-left: 150px;
  overflow-y: scroll;
}
    <div class="top-nav">
      Fixed Top Navigation
    </div>
    <div class="content">
      <div class="side-bar">
        Fixed Side Bar
      </div>
      <div class="main-pane">
        Main Pane
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
    <BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll<BR>Scroll
      </div>
    </div>
Lou Fancy
  • 156
  • 7
0

On browsers that support flex box (which is all modern browsers).

html {
  height: 100%;
}
body {
  padding: 0px;
  height: 100%;
  
  /*make the body a flex container*/
  display: flex;
  flex-direction: column;
}
div {
  text-align: center;
  color: white;
}
.top-nav {
  background-color: green;
  height: 65px;
  margin: 0px;
  padding: 0px;
  
  /*prevent div from shrinking or growing*/
  flex-shrink: 0
  flex-grow: 0
}
.content {
  width: 100%;
  /*tell .content to grow as much as it can in the body*/
  flex: 1;
  
  display: flex;
}
.side-bar {
  background-color: blue;
  width: 10%;
  
  /*prevent div from shrinking or growing*/
  flex-shrink: 0;
  flex-grow: 0;
}
.main-pane {
  flex-grow: 1;
  background-color: red;
  overflow-y: scroll;
}
<div class="top-nav">
  Fixed Top Navigation
</div>
<div class="content">
  <div class="side-bar">
    Fixed Side Bar
  </div>
  <div class="main-pane">
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
    <div>Main Pane</div>
  </div>
</div>

The extra scroll is added by the snippet viewer padding

TedMeftah
  • 1,845
  • 1
  • 21
  • 29
  • 1
    Great. I just added a min-width attribute to the side-bar and it worked as intended. My only concern is, how can I only have 1 scroll bar? Preferably, there would be one scrollbar that acts as the entire page's scrollbar, but only the content in the main pane would move. – Alan Thomas Dec 16 '16 at 21:46
0

I gave the .sidebar and .top-nav position:fixed, moved .content and .sidebar down by 65px (giving .content relative positioning) and it seems to be doing what you want...?

HTML:

<div class="top-nav">
  Fixed Top Navigation
</div>
<div class="content">
 <div class="side-bar">
   Fixed Side Bar
 </div>
 <div class="main-pane">
   Main Pane
 </div>
</div>

CSS:

html{
  height: 100%;
}
body {
  padding: 0px;
  height: 100%;
}
div{
  text-align: center;
  color: white;
}
.top-nav {
  background-color: green;
  height: 65px;
  margin: 0px;
  padding: 0px;
  position:fixed;
  width:100%;
  z-index:100;
}
.content {
  height: 100%;
  width: 100%;
  position:relative;
  top:65px;
}
.side-bar {
  background-color: blue;
  width: 10%;
  height: 100%;
  float: left;
  position:fixed;
  top:65px;
}
.main-pane {
  background-color: red;
  height: 100%;
  width: 90%;
  float: right;
}

Codepen Fork: http://codepen.io/mattDangerly/pen/xRmPQE?editors=1100

Halfacre
  • 565
  • 8
  • 26
0

you may use flex and overflow: http://codepen.io/gc-nomade/pen/VmqrGW?editors=1100

html {
  height: 100%;
}
body {
  margin:0;
  padding: 0px;
  height: 100%;
  display: flex;
  flex-direction: column;
}
div {
  text-align: center;
  color: white;
}
.top-nav {
  background-color: green;
  height: 65px;
  margin: 0px;
  padding: 0px;
}
.content {
  flex: 1;
  display: flex;
  overflow: hidden;
}
.side-bar {
  background-color: blue;
  width: 10%;
  text-align:left;
  /* if needed here too *//*overflow: auto;*/
}
.main-pane {
  text-align:left;
  background-color: red;
  flex: 1;
  overflow: auto;
}
<div class="top-nav">
  Fixed Top Navigation
</div>
<div class="content">
  <div class="side-bar">
    Fixed Side Bar
  </div>
  <div class="main-pane">
    main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>...last line
  </div>
</div>
G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129