简体   繁体   English

CSS基本布局-3格+ 1格,另一格居中

[英]CSS Basic Layout - 3 divs + 1 div inside another, centered

I got a problem that i just can't get my head around. 我遇到一个问题,就是我无法理解。 I want three different divs, filling the whole page (no scrollbars) and the middle div with another div centered horizontally and vertically inside of it. 我想要三个不同的div,将整个页面(无滚动条)和中间的div填充为另一个div,并将其水平和垂直地置于其中。 I tried so much, but something is always destroying the layout. 我做了很多尝试,但总是有损于布局。

So far, this is my code: 到目前为止,这是我的代码:

html, body {
background-image: url(images/bg_tile.gif);
background-repeat: repeat;
margin: 0;
padding: 0;
resize:none;
width: 100%;
height: 100%;
}

#header {
width: 100%;
height: 14%;
background-color: #09F;
top: 0px;
}

#body_con {
width: 100%;
height: 80%;
background-color: #0CF;
}

#footer {
width: 100%;
height: 6%;
background-color: #09F;
bottom: 0px;
}

#body_image {   
width: 90%;
height: 90%;
margin:0px auto;
background-color: white;
}

I know, somehow it's possible but i just can't get it to work. 我知道,有可能,但是我无法使它正常工作。 Any ideas ? 有任何想法吗 ?

EDIT: http://jsfiddle.net/w774g/ 编辑: http : //jsfiddle.net/w774g/

Thank you, Leo 谢谢狮子座

You can set the positions of the edges of an absolutely positioned div, like this: 您可以设置绝对定位的div的边缘位置,如下所示:

http://jsfiddle.net/w774g/1/ http://jsfiddle.net/w774g/1/

#header {
    position: absolute;
    width: 100%;
    top: 0;
    bottom: 86%;
    background-color: #09F;
}

#body_con {
    position: absolute;
    width: 100%;
    top: 14%;
    bottom: 6%;
    background-color: #0CF;
}

#footer {
    position: absolute;
    width: 100%;
    top: 94%;
    bottom: 0;
    background-color: #09F;
}

#body_image { 
    position: absolute;  
    top: 5%;
    bottom: 5%;
    left: 5%;
    right: 5%;
    background: black;
}

Try this: 尝试这个:

#body_con {
    width: 100%;
    height: 80%;
    background-color: #0CF;
    /* added */
    position: relative;
}
#body_image {
    width: 90%;
    height: 90%;
    margin:0px auto;
    background-color: white;
    /* added */
    position: absolute;
    transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
    -moz-transform: translate(-50%, -50%);
    -o-transform: translate(-50%, -50%);
    top: 50%;
    left: 50%;
}

Working Fiddle 工作小提琴

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

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