简体   繁体   English

将侧边栏拉伸到页面的整个高度

[英]Stretch the sidebar to full height of the page

I have this: http://jsfiddle.net/s75ew662/7/ 我有这个: http//jsfiddle.net/s75ew662/7/

How can I stretch the left sidebar (gray) to the full height of the page? 如何将左侧边栏(灰色)拉伸到页面的整个高度?

 .container { width: 750px; margin: 0 auto; } .sidebar { width: 200px; background: lightgrey; float: left; } .content { background: lightblue; float: left; width: 550px; } 
 <div class="container"> <div class="sidebar"> <div>1</div> <div>2</div> </div> <div class="content"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> </div> 

The simplest way based on your current code is to use viewport units . 基于当前代码的最简单方法是使用视口单元

.sidebar {
    height: 100vh;
}

 body { margin: 0; } .container { width: 750px; margin: 0 auto; } .sidebar { width: 200px; background: lightgrey; float: left; height: 100vh; } .content { background: lightblue; float: left; width: 550px; } 
 <div class="container"> <div class="sidebar"> <div>1</div> <div>2</div> </div> <div class="content"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> </div> 

If you need to support older browsers, you could do this instead. 如果您需要支持旧浏览器,则可以执行此操作。

html, body, .container{
    height: 100%;
}
.sidebar {
    min-height: 100%;
}

 html, body, .container{ height: 100%; margin: 0; } .container { width: 750px; margin: 0 auto; } .sidebar { width: 200px; background: lightgrey; float: left; min-height: 100%; } .content { background: lightblue; float: left; width: 550px; } 
 <div class="container"> <div class="sidebar"> <div>1</div> <div>2</div> </div> <div class="content"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> </div> 

In order to fix the the scrolling + overflow issues when the content is taller than the sidebar. 当内容比侧边栏高时,为了解决滚动+溢出问题。 Some slight markup changes would be needed, see the JsFiddle demo. 需要进行一些轻微的标记更改,请参阅JsFiddle演示。

 html, body, .container { height: 100%; margin: 0; } .container { width: 750px; margin: 0 auto; display: table; } .sidebar { display: table-cell; width: 200px; } .content { display: table-cell; width: 550px; } .sidebar .inner { height: 100%; background: lightgrey; } .content .inner { background: lightblue; } .content .inner div { height: 100px; /*testing*/ } 
 <div class="container"> <div class="sidebar"> <div class="inner"> <div>1</div> <div>2</div> </div> </div> <div class="content"> <div class="inner"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> </div> </div> 

As a very simple solution, you can use the vh unit of measure, which corresponds to percentage of viewport height. 作为一个非常简单的解决方案,您可以使用vh测量单位,该单位对应于视口高度的百分比。

.sidebar {
    width: 200px;
    height: 100vh;
    background: lightgrey;
    float: left;
}

Keep in mind that when doing this you need to account for the margins that are in place. 请记住,在执行此操作时,您需要考虑到位的边距。 I've posted a working example as a fork to your jsfiddle here: https://jsfiddle.net/01ubpbfg/ 我在这里发布了一个工作示例作为你的jsfiddle的分支: https ://jsfiddle.net/01ubpbfg/

在css文件中,将以下内容添加到侧边栏类:

height:100vh;

Updated to solve overflow issue 更新以解决溢出问题

adding these four css styles would accomplish this: 添加这四种CSS样式将实现此目的:

html {
height: 100%;
}
body {
height: 100%;
}
.container {
height: 100%;
overflow: hidden;
}
.sidebar {
height: 100%;
}

http://jsfiddle.net/s75ew662/17/ http://jsfiddle.net/s75ew662/17/

.sidebar {
    width: 200px;
    background: lightgrey;
    float: left;
    position: absolute;
    height: 100%;
}

A very late reply, none of the above helped me. 一个非常迟到的回复,以上都没有帮助我。 I have a working solution, may help someone. 我有一个有效的解决方案,可以帮助别人。 using a flex along with view port for min-height helps to cover the sidebar to the whole page. 使用flex和视图端口进行最小高度有助于将侧边栏覆盖到整个页面。

this code assumes having a top-bar of height 51px; 此代码假设顶部高度为51px;

.user-content {
  display: flex;
 }

.user-content .side-bar-content {
    flex      : 1;
    flex      : 0 0 220px;
    background: #f1f1f1;
    min-height: calc(100vh - 51px);
    //min-height: 100vh; // without topbar
  }

 .user-content .app-user {
    flex   : 2;
    padding: 0 0 15px 15px;
  }

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

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