垂直和水平对齐页面的内部DIV中心

时间:2012-06-07 04:51:33

标签: css html css3

  

可能重复:
  Best way to center a <div> on a page vertically and horizontally?

我的网站着陆页有Container DIV,100%高度&amp;宽度。我可以将InnerContainer div水平对齐,但我无法将其置于垂直中心。我改变了几个属性,结果大致相同。

我有http://jsfiddle.net/cwkzq/12/的相同例子,我不确定我做错了什么。我很感激你的帮助。

CSS代码

html, body,form { height: 100%;  }
body { margin: 0;  padding: 0; border: 1; background-color:Aqua; }
.Container { width: 100%;height: 100%; padding: 0; font-size: 12px; border:1px solid green;
 vertical-align: middle;     }
.InnerContainer
{  vertical-align: middle; width: 400px;height: 350px; border-left:1px solid;
    border-right:1px solid; border-color:#f5f5f5;margin: 0 auto;  padding: 0;
    font-size: 12px;  background-color:Red;
}

HTML CODE

<!--  Container    -->
<div class="Container">
<div class="InnerContainer">
    <!--  TopMenu Bar    -->
    <div class="colorBar">

    </div>
    <!--  TopMenu Bar   -->
    <!--  Middle Part    -->
    <div class="MiddleWrapper">
        <!--  Left Title    -->
            <div class="Title">

            </div>
        <!--   Left Title   -->
        <!--   Large Image   -->
            <div class="ImageLeftWrapper">

            </div>
        <!--   Large Image   -->
        <!--  Logo Wrapper    -->
            <div class="LogoWrapper">

            </div>
        <!--   Logo Wrapper   -->
        <!--   Page Text Area  -->
            <div class="PageText">

            </div>
        <!--   Page Text Area   -->
        <!--  Search Bar    -->
            <div class="SearchBar">

            </div>
        <!--   Search Bar    -->
        <!--   Banner Images -->
            <div class="BannerImageWrapper">

            </div>
        <!--  Banner Images   -->
    </div>
    <!--  Middle Part    -->
    <!--   Menu Wrapper    -->
    <div class="MenuWrapper">

    </div>
    <!--   Menu Wrapper    -->
    <!--   Footer Section  -->
    <div class="FooterWrapper">

    </div>
    <!--  Footer Section  -->
</div>
</div>
<!--  Container   -->

网上的所有例子大多都有固定的宽度和宽度。高度容器DIV,而在我的情况下,我的容器div有100%的宽度&amp;高度

3 个答案:

答案 0 :(得分:22)

The answer移至原始问题

我做到了:(demo on dabblet.com

此演示中的主要技巧是在正常的元素流中从上到下,因此margin-top: auto设置为零。但是,对于绝对定位的元素,可以使用相同的自由空间分布,同样可以在指定的topbottom处垂直居中(在IE7中不起作用)。

此技巧适用于任何大小的div

HTML:

<div></div>

CSS:

div {
    width: 100px;
    height: 100px;
    background-color: red;

    position: absolute;
    top:0;
    bottom: 0;
    left: 0;
    right: 0;

    margin: auto;
}

答案 1 :(得分:6)

如果您知道要对齐的块的确切大小,则最简单的方法是执行此操作:jsfiddle

将此样式添加到.InnerContainer

.InnerContainer{
  width: 400px;
  height: 350px;
  border-left: 1px solid;
  border-right: 1px solid;
  border-color: #f5f5f5;
  margin: 0 auto;
  padding: 0;
  font-size: 12px;
  background-color: red;
  position: relative;
  left: 50%;
  top: 50%;
  margin-left: -200px;
  margin-top: -175px;
}

答案 2 :(得分:1)

您好现在可以使用此解决方案的许多方法,就像这样

Css

.wraptocenter {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
    width: ...;
    height: ...;
}
.wraptocenter * {
    vertical-align: middle;
}

.wraptocenter {
    display: block;
    background:green;
    height:500px;
}
.wraptocenter span {
    display: inline-block;
    height: 100%;
    width: 1px;
}
.child{
background:pink;
    min-height:100px;
    width:100px;
    display:inline-block;

}

<强> HTML

<div class="wraptocenter">
    <span></span>
    <div class="child"> Hello I am child and ver and horz....</div>
</div>

现场演示http://jsfiddle.net/w9dxv/1/

更多信息http://www.brunildo.org/test/img_center.html


如果您想要居中水平和垂直对齐

<强>的CSS

.chv{
width:200px;
    height:100px;
    position:absolute;
    left:50%;
    top:50%;
margin-left:-100px;
    margin-top:-50px;
    border:solid 1px red;
}

<强> HTML

<div class="chv">
    Center horizontal and vertical align
</div>

现场演示http://jsfiddle.net/HkEVZ/1/


如果你想要水平和垂直居中一行文字

<强>的CSS

.alldiv{
width:400px;
    height:200px;
    text-align:center;
    line-height:200px;
    border:solid 1px red;
}

<强> HTML

<div class="alldiv">
Center horizontally and vertically a single line of text
</div> 

现场演示http://jsfiddle.net/SVEtH/1/


如果你想要中心水平和垂直对齐用于表格属性,就像这样..

<强>的CSS

.parent{
display:table;
    height:300px;
    text-align:center;
    border:solid 1px red;
    width:100%;
}
.child{
display:table-cell;
    vertical-align:middle;
    border:solid 1px green;
}

<强> HTML

<div class="parent">
<div class="child">
Center horizontal and vertical align
</div>
</div>

现场演示http://jsfiddle.net/J3Zc6/2/