带有%-height和100%内容部分的粘性标题

时间:2014-08-06 06:14:12

标签: html css html5 css3

我想要一个带有%-height属性的粘性标头。标题下方的部分应占用页面的剩余高度,例如:header = 10%所有其他部分至少为90%。这类似于一个相关的问题:CSS Sticky Header/Footer and Fully Stretched Middle Area?,但他使用固定的px-height,而我想要%-height。我尝试在我的部分使用保证金,但这似乎不起作用。在我的部分上使用边距和90%高度似乎并不奏效。

enter image description here

目前我能够提出:http://jsfiddle.net/K9m63/。但是有一些问题:

  1. 第一部分在标题下方消失。
  2. 由于第1点,部分div太高,因此不会占用剩余的尺寸。
  3. HTML

    <header>
        <nav>Test</nav>
    </header>
    <section>
        <div class="container yellow">1</div>
    </section>
    <section>
        <div class="container pink">2</div>
    </section>
    <section>
        <div class="container purple">3</div>
    </section>
    

    CSS

    body, html {
        height: 100%;
    }
    header {
        height: 10%;
        background-color: green;
        position: fixed;
        top: 0px;
        width: 100%;
    }
    .helper {
        display: inline-block;
        height: 100%;
        vertical-align: middle;
    }
    .nav-image {
        vertical-align: middle;
    }
    section {
        height: 100%;
        width: 100%;
        background-color: red;
    }
    .container {
        width: 72.8125%;
        background-color: blue;
        margin: 0px auto;
        height: 100%;
    }
    .yellow {
        background-color: yellow;
    }
    .pink {
        background-color: pink;
    }
    .purple {
        background-color: purple;
    }
    

    谢谢!

2 个答案:

答案 0 :(得分:1)

可能的解决方案:

我已将所有部分包装成2个div。

<div class="wrapper">//rest 90% of the page
    <div class="wrapper2">//100% of parent
        <section>
            <div class="container yellow">1</div>
        </section>
        <section>...
    </div>
</div>

CSS:

.wrapper {
    min-height:90%;
    height:auto !important;
    position:relative;
    top:10%;
}
.wrapper2 {
    height:100%;
    width:100%;
    position:absolute;
}

另外,将z-index:1;添加到header

此处更新了fiddle

答案 1 :(得分:0)

根据您的绘图,这是您可以*的方式。 -但也有“固定” /或“固定”定位。 -这种布局将迫使您在页面内容下方实施自己的滚动,这很麻烦。

html, body {
  height: 100vh; /* you can use vh or % - but either way... */
  height: 100%; /* they don't know their own height... how would they? */
}

body {
  margin: 0;
}

.site-header {
  background: #ff6666;
  height: 10%;
}

.page-content {
  background: #6666ff;
  height: 90%;
}
<header class="site-header">
  header
</header>

<main class="page-content">
  main
</main>

相关问题