html - 保持绝对定位的 DIV 内的内容居中(在父容器内)

标签 html css css-position

我有一个宽度固定且居中的容器。在容器内部,我有两个相对于窗口并排放置的 DIV。在这些 Div 中,我还有其他我想要居中的内容(最好使用容器)。

这里是简化代码:http://jsfiddle.net/jTpGs/

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<style>
 #container {
 width: 960px;
 height: 1000px;
 background: red;
 margin: 0 auto;
 }  

#windowdiv {
height: 200px;
background: purple;
padding-top: 20px;
position: absolute;
left: 0;
right: 30%;
top: 25px;
}
#windowdiv2 {
height: 200px;
background: blue;
padding-top: 20px;
position: absolute;
left: 60%;
right: 0;
top: 10px;

}

</style>
</head>
<body>
  <div id='container'>

     <div id='windowdiv'>
        <p>some content</p>
     </div>
     <div id='windowdiv2'>
       <p>some content</p>
     </div>
 </div>
</body>
</html>

最佳答案

将此添加到您的 CSS 中,使您的文本在这些框的中间居中:

​#windowdiv p, #windowdiv2 p {
    ​text-align: center;
    ​height: 100%;
    ​line-height: 180px;   
}​

这是假设你的盒子是固定高度的,因为我们设置了行高。 (200px - 20px (padding-top) = 180px)

关于html - 保持绝对定位的 DIV 内的内容居中(在父容器内),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13944575/

相关文章:

html - 当悬停在图像或文本上时,两者都会缩放

html - 消除 CSS 中隐藏 html 的空白区域

html - CSS - 将 div 固定到顶部中心

html - Bootstrap 导航栏对齐(3 列,带有响应式菜单)

html - CSS 下拉菜单在 Chrome 中在按钮单击和悬停时向下滚动页面

html - 如何缩放和居中内联 SVG?

javascript - 调整图像组的大小以适应容器,所有图像高度相同

javascript - 休息,白色间距?如何在不丢失 text-align :justify 的情况下打断超长单词

css - 在 Safari 的 flexbox 中拉伸(stretch)图像

php - 有没有像必应图像搜索那样在悬停时扩展图像?