html - 具有可变内容的固定宽度绝对侧边栏

标签 html css

我正在尝试创建一个流体布局网站,该网站也有一个具有以下属性的左侧边栏:

  1. 当主要内容滚动时它不滚动
  2. 它占据了整个页面的高度

我见过的唯一布局是这个:http://stugreenham.com/demos/fluid-width-with-a-fixed-sidebar/

HTML

<div id="page"> 
    <header id="sidebar"> 
        //SIDEBAR CONTENT HERE
    </header> 

    <article id="contentWrapper"> 
        <section id="content"> 
            //CONTENT HERE
        </section> 
    </article> 
</div>

CSS

html {
    overflow: hidden;
}

#sidebar { 
    background: #eee;
    float: left;
    left: 300px;
    margin-left: -300px;
    position: relative;
    width: 300px;
    overflow-y: auto;
}

#contentWrapper { 
    float: left;
    width: 100%;
}

#content {
    background: #ccc;
    margin-left: 300px;
    overflow-y: auto;
}

但是我认为这是一个糟糕的解决方案,因为它不仅需要负边距,而且还需要 javascript。

有没有更好的方法来实现这一点?

最佳答案

Demo

CSS

#sidebar {
    position:fixed;
    height:100%;
    width: 300px;
    background-color: #ccc;
    overflow-y: auto;                   
}
#contentWrapper { /* not using margin */
    box-sizing:border-box;
    background-color:#eee;
    position:absolute;
    left:300px;
    width:calc(100% - 300px);
    min-height: 100%;
}
#contentWrapper { /* using margin */
    box-sizing:border-box;
    background-color:#eee;
    margin-left: 300px;
    /*position:absolute;
    left:300px;
    width:calc(100% - 300px);*/
    min-height: 100%;
}



html,body,#page {
    width: 100%;
    height: 100%;
}

关于html - 具有可变内容的固定宽度绝对侧边栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24508763/

相关文章:

javascript - 调整窗口大小时如何阻止 HTML 元素被覆盖?

php - 如何显示多个搜索结果

javascript - 让 ng-view 只显示它的内容,而不是它本身

javascript - 尝试使用 jQuery、javascript、click 函数将图像更改为相关的 id

html - 跨页的表格单元格丢失其顶部和底部边框

javascript - 我可以在我的应用程序启动之前加载闪存吗?

html - 使用 CSS 制作箭头而不是圆 Angular

css - IE9 渲染 sprite hover image 1px off/blurred

html - 如何更改语义 ui 中的输入大小?

html - 使用网格系统堆叠在一起的卡片