CSS布局垂直对齐的滚动容器

时间:2015-10-25 11:26:54

标签: html css css3

我想知道如何在CSS3中设计以下布局:

蓝色容器的填充量应为1%至
html / body的顶部,右侧,底部和左侧(此处涂成黑色) 橙色容器应可滚动 绿色容器的高度与所有其他容器一样,也是如此。 enter image description here

到目前为止,我的CSS代码看起来像这样:

<style>
.html,body {
    width: 100%;
    height: 100%;
}

.containerbox {
    width: 98%;
    height: 98%;
    paddding: 1% 1% 1% 1%; // not working yet
    // todo: horizontal alignment
    // todo: vertical alignment
}

.header {
    width: 100%;
    height: 35%%;
}
.content {
    width: 100%;
    height: 50%;
}
.footer {
    width: 100%;
    height: 15%%;
}
</style>


这是关于我上面的CSS的html代码:

<div class="containerbox">

<div class="header"></div>
<div class="content"></div>
<div class="footer"></div>

</div>

3 个答案:

答案 0 :(得分:1)

我做过一些事情。你可以找到它here

body, html{
    margin: 0;
    height: 100%;
}

.blue-container{
    height: 100%;
    background-color: aqua;
    padding: 1%;
    box-sizing: border-box;
}

.green{
    background-color: green;
}
    
.green.big{
    height: 15%;
}
    
.green.small{
    height: 10%;
}
    
.orange{
    height: 72%;
    overflow-y: scroll;
    background-color: orange;
    margin: 1% 0;
}
<div class="blue-container">
    <div class="green big"></div>
    <div class="orange"></div>
    <div class="green small"></div>
</div>

P.S。你只需找到正确的颜色值。

答案 1 :(得分:1)

我已经制作了Fiddle

HTML

<div>
    <header>
    </header>
    <main>
        Lorem ipsum
    </main>
    <footer>
    </footer>
</div>

CSS

*,*:before,*:after{
    box-sizing: border-box;
    position: relative;
}

html, body{
    height: 100%;
}

body{
    margin: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #3F47CA;
    border: 10px solid #000;
}

div{
    width: 98%;
    height: 98%;
}

header{
    height: 12%;
    background-color: #B5E51D;
}

main{
    height: calc(80% - 20px);
    padding: 10px;
    color: #FFF;
    margin: 10px 0;
    background-color: #FFC90D;
    overflow-y: scroll;
}

footer{
    height: 8%;
    background-color: #B5E51D;
}

希望这有帮助

答案 2 :(得分:0)

html{
    height: 100%;
    min-height: 100%;
}
body{
    min-height: 100%;
}
.blue{
    height: 100%;
    padding: 1%;
}
.green{
    height: 20%; /*of the screen height*/
}
.orange{
    height: 30%; /*of the screen height*/
    overflow-y: scroll; /*scroll when content's height is more that container's height*/
}