html - 将 div 水平和垂直居中对齐后

标签 html css

我看到很多问题How to align a div horizo​​ntal and vertically center,我也得到了答案。

我得到的代码: HTML-

<div class="outer">
   <div class="middle">
      <div class="inner">
         <div align="center">
            <h1>The Content</h1>

            <p>Once upon a midnight dreary...</p>
         </div>
      </div>
   </div>
</div>

CSS-

.outer {
   display: table;
   position: absolute;
   height: 100%;
   width: 100%;
}

.middle {
   display: table-cell;
   vertical-align: middle;
}

.inner {
   margin-left: auto;
   margin-right: auto;
   background: blue; /*gave blue to know the height and width*/ 
}

我猜对了。
但我无法为其指定默认高度。

当我给 width: 50%;height: 50%;class="inner" 只有宽度被接受了。
我什至无法在顶部和底部之间提供填充空间。
请帮忙。
请不要给我另一个代码来使 div 垂直和水平居中对齐

最佳答案

你想要的(我想)是这个:

body {
    margin : 0;
}

.outer {
   display: table;
   position: absolute;
   height: 100%;
   width: 100%;
}

.middle {
   display: table-cell;
   vertical-align: middle;
   text-align: center;
}

.inner {
   display: inline-block;
   width: 50%;
   height: 50%;
   background: blue;
}
<div class="outer">
   <div class="middle">
      <div class="inner">
          <h1>The Content</h1>
          <p>Once upon a midnight dreary...</p>
      </div>
   </div>
</div>

另见 this Fiddle !

关于html - 将 div 水平和垂直居中对齐后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37314831/

相关文章:

html - 单击背景上的任意位置后转到另一个站点?

javascript - 在 onclick 属性中使用 javascript

javascript - OO Javascript 调试 - 带过渡的图像交换

php - 土耳其语字符显示不正确

html - 灵活的高度滚动div

css - 如何增加 GWT celltable : Issue with IE? 的单元格高度

javascript - jQuery 宽度();不能正常工作

html - 如何移动图片下方的内容?

html - 如何更改页眉背景颜色?

css - 如何将 CSS 应用于 Here Maps API 标记