如何让我的包装器<div>垂直居中并响应?</div>

时间:2014-01-14 16:20:36

标签: html css

我希望包装器滚动部分内的图像浮动在屏幕中间(宽),如果屏幕改变大小,则放大/缩小。目前它位于标题下方,但远高于页脚。如何让它垂直居中?

CSS:

/* main content
------------------------------------------------------------------- */  

#wrapper {
   float:left;
   margin:110px 0 0 0;
   padding:0 0 0 250px;
   background:#fff;
   position:relative;
   z-index:2;
   border-bottom:solid 20px #fff;
}   
.post {
    padding:0 5px 0 0;
    background:#fff;
    height:100%;
    }
#wrapper img {
 color:#fff;
 width:auto;
 }

HTML:

<!-- section that contains all pics -->
<section id="wrapper">
    <article class="post">
    <p><img src="img/scroll/001_scroll.jpg" alt="test image 1" title="test image" width="994" height="620" class="alignnone size-full wp-image-240" /></p>
    </article>

    <article class="post">
    <p><img src="img/scroll/002_scroll.jpg" alt="test image 1" title="test image" width="994" height="620" class="alignnone size-full wp-image-240" /></p>
    </article>

    <article class="post">
    <p><img src="img/scroll/003_scroll.jpg" alt="test image 1" title="test image" width="994" height="620" class="alignnone size-full wp-image-240" /></p>
    </article>

    <article class="post">
    <p><img src="img/scroll/004_scroll.jpg" alt="test image 1" title="test image" width="994" height="620" class="alignnone size-full wp-image-240" /></p>
    </article>

    <article class="post">
    <p><img src="img/scroll/005_scroll.jpg" alt="test image 1" title="test image" width="994" height="620" class="alignnone size-full wp-image-240" /></p>
    </article>

</section>
<!-- close section -->

先谢谢你们!

2 个答案:

答案 0 :(得分:1)

如果你想使用CSS表,我建议:

.post {
    display: table;
}
.post p {
    display: table-cell;
    vertical-align: middle;
    height: inherit; /* may not be needed */
}

新版浏览器非常支持display: table{-cell}属性,所以这应该可以解决问题。

答案 1 :(得分:0)

有些解决方案会让人联想到如何垂直居中。

#wrapper {
 float:left; /**REMOVE THIS**/
 margin:110px 0 0 0; /** REMOVE THIS **/
 padding:0 0 0 250px; /** REMOVE THIS **/
 width: 100%; /** ADD THIS **/
 height: 100% /** ADD THIS **/
 background:#fff;
 position:relative; /** REMOVE THIS **/
 z-index:2;  /** REMOVE THIS **/
 border-bottom:solid 20px #fff; /** REMOVE THIS **/
}
#wrapper {
 width: 100%; 
 height: 100%;
 min-height: 100%;
 background:#fff;
 position: absolute;
}   
.post {
  padding:0 5px 0 0;
  background:#fff;
  height:100%;
  }
 #wrapper img {
  color:#fff;
  width:auto;
 }
#content {
  position: relative;
  width: 60%;
  height: 60%;
  top: 50%;
  left: 50%;
}

HTML

<section id="wrapper">
  <div id="content"> <!-- ADD THIS This will be used to position vertically-->
  <article class="post">
<p><img src="img/scroll/001_scroll.jpg" alt="test image 1" title="test image" width="994" height="620" class="alignnone size-full wp-image-240" /></p>
</article>

<article class="post">
<p><img src="img/scroll/002_scroll.jpg" alt="test image 1" title="test image" width="994" height="620" class="alignnone size-full wp-image-240" /></p>
</article>

<article class="post">
<p><img src="img/scroll/003_scroll.jpg" alt="test image 1" title="test image" width="994" height="620" class="alignnone size-full wp-image-240" /></p>
</article>

<article class="post">
<p><img src="img/scroll/004_scroll.jpg" alt="test image 1" title="test image" width="994" height="620" class="alignnone size-full wp-image-240" /></p>
</article>

<article class="post">
<p><img src="img/scroll/005_scroll.jpg" alt="test image 1" title="test image" width="994" height="620" class="alignnone size-full wp-image-240" /></p>
</article>

还需要添加更多内容才能使其更好,但这会让您开始朝着正确的方向前进。 http://jsfiddle.net/cornelas/qCa3J/