html - CSS - 标题留在容器顶部

标签 html css

我有这个可以滚动内容的容器。我希望此容器中的 header 始终位于顶部。

http://jsfiddle.net/z9ze5/

容器:

.lists {
    width: 300px;
    height: 250px;  
    margin: 30px auto;
    background: #39C;
    overflow: scroll;
    position: relative;
}

标题:

.box_header {
    width: 100%;
    height:30px;
    overflow:hidden;
    position: absolute;
    margin: 0;
    background: #DDD;
    z-index: 999;
}

最佳答案

如果您愿意更改标记,可以采用以下一种方法:

<div class="lists">
    <header class="box_header">
        <h1>HEADER 2</h1>
        <div class="setting" id="btn2"></div>
    </header>
    <section class="content">
        <p>Lorem Ipsum ....</p>
    </section>
</div>

将滚动区域包裹在 <section> 中(或其他 block 级元素)。

对于你的 CSS:

.lists {
width: 300px;
height: 250px;
margin: 30px auto;
background: #39C;
position: relative;
}
section.content {
width: 300px;
height: 220px;
margin: 0 auto;
background: #39C;
position: relative;
top: 30px;
overflow: scroll;
}

请参阅 fiddle :http://jsfiddle.net/audetwebdesign/nGGXx/

更高级的例子

如果你研究下面的例子:

http://jsfiddle.net/audetwebdesign/fBNTP/

您可以看到如何在半灵活布局中应用您的滚动框。 我并排排列了两个滚动框,并使它们的宽度与页面的宽度成比例。

调整高度比较棘手。我固定了父容器的高度,规则如下:

.contentWrapper {
    border: 1px solid red;
    margin-top: 1.00em;
    padding: 30px 0;
    overflow: auto;
    height: 400px;
}

如果您将高度从 400 像素更改为其他值,滚动框将自行调整。

希望这些示例能让您和其他人更深入地了解如何构建这些更高级的布局设计。

关于html - CSS - 标题留在容器顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15388857/

相关文章:

jquery - jscrollpane 在 firefox 中不起作用,在 chrome 中起作用

javascript - html 4中的可编辑div内容

animation - CSS3 链式动画

css - CSS中的minmax()和clamp()有什么区别?

javascript - 如何通过jquery获取canvas元素?

javascript - 连接相邻的矩形?

javascript - 更改 url 位置的最佳方法

html - HTML/CSS 的最佳实践

php - 如何向 php 循环中的每个第 n 个元素添加一个类(wordpress)

jquery - 菜单在特定高度后固定