最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Make navigation bar take up entire page height in css - Stack Overflow

programmeradmin2浏览0评论

I am designing a website using css and html. I have managed to get a navigation bar on the left side of my page using this css, however when the screen is scrolled down the navigation bar no longer continues.

#navbar {
  background: #a8a599;
  float: left;
  width: 20%;
  height: 100%;
}

However i would like to make the height of the navigation bar the height of the document. I feel like i might need java script for this, however i am new to java-script, so i am not sure how i would accomplish this. I thought making the height 100% would make it take up the whole page, owever it only takes up the visible part of the page.

Here it is on fiddle if you want to look at the rest of the page /

I am designing a website using css and html. I have managed to get a navigation bar on the left side of my page using this css, however when the screen is scrolled down the navigation bar no longer continues.

#navbar {
  background: #a8a599;
  float: left;
  width: 20%;
  height: 100%;
}

However i would like to make the height of the navigation bar the height of the document. I feel like i might need java script for this, however i am new to java-script, so i am not sure how i would accomplish this. I thought making the height 100% would make it take up the whole page, owever it only takes up the visible part of the page.

Here it is on fiddle if you want to look at the rest of the page http://jsfiddle.net/HRpXV/3/embedded/result/

Share Improve this question edited Jul 2, 2014 at 19:41 JasonMArcher 15k22 gold badges59 silver badges53 bronze badges asked Nov 16, 2013 at 12:58 PopgalopPopgalop 7572 gold badges10 silver badges26 bronze badges 1
  • 1 best approach depends on css of parents up to highest level. Need more structure shown thatn one element...create a live demo in jsfifddle.net – charlietfl Commented Nov 16, 2013 at 13:04
Add a comment  | 

5 Answers 5

Reset to default 9 +50

100% does not apply because it is floated. Change the parent container to position: relative and the navbar to position: absolute will solve the problem.

#container{
    position: relative;
}

#navbar {
    background: #a8a599;
    /*float: left; Instead of float, use absolute position*/
    position: absolute;
    width: 20%;
    height: 100%;
}

Demo

Using only HTML and CSS you can usually fix this 100% height issue by using:

body,html{
   height:100%;
}

If you want to use the JavaScript method then this should work

document.getElementById("navbar").style.height=window.innerHeight;

That will set the height of the navBar element to whatever the height of the browser window is

It took my like 2 hours but I finally found solution to this! Accepted answer by Derek almost work but as my comment suggests, not entirely. When your left bar has more content then main bar, left menu wont stretch up to the height of its content. You maybe never get so many options in that menu but I am building new administration wich will have many modules in future and you never know if it can happen or not.

Solutions are old good tables! Dont worry, not quite. Only css display property with value of table, table-row and table-column:

Basic idea:

#page {
    display: table;
    min-height: 100%;
    width: 100%;
}
#pageRow {
    display: table-row;
}
#leftMenu {
    display: table-cell;
    width: 300px;
    background-color: red;
}
#content {
    display: table-cell;
    background-color: blue;
}
  1. little content in both sections http://jsfiddle.net/85w56gkx/1/

  2. more content in left menu section http://jsfiddle.net/1o92r7ao/

  3. more content in main content section http://jsfiddle.net/Ldav83vn/

If you plan to have the navBar fixed you can do something along the lines of Fiddle

*{
    margin:0;padding:0;
}
#navBar {
    background: red;
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: 100px;
}
#content {
    background: yellow;
    min-height:1000px;
    display: block;
    margin-left: 100px; /*width of nav bar*/
}

You could just modify your #navbar css and set position to fixed

#navbar {
  background: #a8a599;

  /* removed css */
  /* float: left; */

  /* new css */
  position:fixed;
  top:0;
  left:0;
  /***********/

  width: 20%;
  height: 100%;
}

float: left; is positioned in relation to the document.

Removing float and adding position: fixed; will position the element in relation to the window.

Then you can add top, bottom, left or right to suit your needs

I forked your fiddle for an example Here

Some more information on float W3 CSS Float

Some more information on position W3 CSS Position

发布评论

评论列表(0)

  1. 暂无评论