简体   繁体   English

使浮动元素的高度为100%以填充页面

[英]Make floated element have height 100% to fill page

I am trying to get the left side bar to have a height of 100% and fill the page no matter how big the "main" div is made. 我试图使左侧栏的高度为100%,并且不管“主要” div的大小如何填充页面。

At the moment it stops at normal page height and doesn't increase height. 目前,它停止在正常的页面高度,并且不会增加高度。

Is there any way I can achieve this? 有什么办法可以实现?

JFiddle: https://jsfiddle.net/hjnheonk/ JFiddle: https ://jsfiddle.net/hjnheonk/

HTML: HTML:

<div class="container">
<div class="navbar-left">
    <div id="top">
        <h2><b>Admin</b>Panel</h2>
    </div>
    <div id="navigation">
        <ul>
            <li class="nav-header">Main Pages: </li>
            <li>
                <a href="index.php">Home</a>
            etc ...
        </ul>

    </div>
</div>

<div id="navbar-top">
    <div id="user">
        <?php echo'<p id="user_greeting">'.$username. '<span class="fa fa-caret-down"></span>'.'</p>'?>
    </div>
    <div id="icon">
        <span>
            <hr><hr><hr>
        </span>
    </div>

  <div class="main">

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

**CSS: ** ** CSS:**

html,body {
    height: 100%;
}

.container {
    margin-left: 230px;
    height: 100%;
    position:relative;
}

.navbar-left {
    background-color:rgb(26, 34, 38);
    color:white;
    width: 230px;
    margin-left: -230px;
    height: 100%;
    float:left;
}

.navbar-left #top {
    background-color:#367fa9;
    min-height: 50px;
    text-align: center;
}

.navbar-left #top h2 {
    font-size: 20px;
    padding: 15px 0px;
}
#navbar-top {
    float:right;
    width: 100%;
    position:relative;
    background-color:#3c8dbc;
    width: 100% !important;
    margin:0 auto;
    border: none;
    min-height: 51px;
}


#navbar-top #icon {
    width: 20px;
    padding: 18px 10px !important;
}

#navbar-top #icon hr {
    margin:0 auto;
    padding: 0px;
    border: 1px solid white;
    border-radius: 5px;
}

#navbar-top #icon hr:not(:first-child) {
    margin-top: 5px;
}

#navbar-top > div:hover:not(#userDropdown) {
    background-color:#47a0d3;
    cursor: pointer;
}

#brand {
    float:left;
}

#navigation .nav-header {
    background-color: #272f33;
    padding: 12px 30px;     
    text-align: left;
    margin-top: 40px;
    margin-bottom: 25px;
}

#navigation ul li a:hover {
    background-color: #273136;
}

#navigation ul li a {
    width: 100%;
    display: block;
    padding: 12px 0px;
    background-color: #1a2226;
    text-align: center;
    color:white;
    text-decoration: none;
}

.main {
    float:left;
    width: 100%;
  background-color:pink;
  height: 1000px; /*Used as an example to show */
}

There's no way to do this by pure CSS, they way you coded-sliced it. 纯CSS不可能做到这一点,它们会以编码方式对其进行切片。 If you want it to make work with the current layout - calculate the height via JS, based on the contents and height of the right column. 如果希望它与当前布局配合使用,请根据右列的内容和高度,通过JS计算高度。

Basically in your case there different ways to proceed: 基本上,在您的情况下,有不同的处理方式:

  1. calculate the height via JS, based on the contents and height of the right column. 根据右列的内容和高度,通过JS计算高度。
  2. to nest DIVs. 嵌套DIV。 So one div will stretch it's parent. 因此,一个div会拉伸它的父级。 Then it will be possible to use purely CSS solution. 这样就有可能使用纯CSS解决方案。 Read more here one of the possible solutions. 在这里阅读更多可能的解决方案之一。
  3. to "override" the standard behavior of divs with "display:table-cell;" 使用“ display:table-cell”“覆盖” div的标准行为; (table, table-row, etc), or even to use modern features of CSS alike flexboxes (表格,表格行等),甚至使用CSS的现代功能(如flexbox)

Which way to go, is up to you. 哪种方法取决于您。

Does the container need to be defined as percentage? 是否需要将容器定义为百分比? If not then you could do something like this: 如果没有,那么您可以执行以下操作:

$('.navbar-left').css('height', $('.container').height()+'px');

Using Farside's method and updating a little bit here is my code: 使用Farside的方法并在此处进行一些更新是我的代码:

var column = $(".column_left").height() + "px";
$(".column_right").css({"height": column});

$(window).on('resize', function(){ //accounts for if the user resizes the window, column stays in place.
   var column = $(".column_left").height() + "px";
   $(".column_right").css({"height": column});
});

Here is a Pure CSS way to acheive the same. 这是达到相同目的的纯CSS方法。

JS Filddle: https://jsfiddle.net/cx6nu8sw/ JS Filddle: https ://jsfiddle.net/cx6nu8sw/

Following are the classes from your code which are changed 以下是您的代码中已更改的类

#navbar-top {

    width: 100%;
    position:relative;
    background-color:#3c8dbc;

    margin:0 auto;
    border: none;
    min-height: 51px;
    display:table-cell;
    vertical-align:top;
}
.navbar-left {
    background-color:rgb(26, 34, 38);
    color:white;
    display:table-cell;
    vertical-align:top;

}
//newly addition
#navigation{
  width:230px;
}

As mentioned by @Farside in his 3rd point, I have used "display:table-cell;" 正如@Farside在他的第三点中提到的那样,我使用了“ display:table-cell;”。 on your Div's. 在您的Div上。 Its same as creating table, where the height of row is decided by the longest content in the entire row. 它与创建表相同,其中行的高度由整行中最长的内容决定。 But, be aware that width & height of elements with "display:table-cell;" 但是,请注意带有“ display:table-cell;”元素的宽度和高度。 cannot be forced, it will adjust according to the content inside them. 不能强制,它将根据其中的内容进行调整。 So you can set width and height of elements inside them it will automatically take the same height and width. 因此,您可以在其中设置元素的宽度和高度,它将自动采用相同的高度和宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM