HTML/CSS 如何将DIV居中在绝对定位的DIV内部

3

我有一个问题无法解决。我试图将这个黑色盒子居中在绝对定位的红色盒子内。我尝试将黑色盒子相对定位,但感觉好像还缺少一些东西。

最终,我想制作顶部标题。

这是一张图片header-image.jpg

能帮忙吗?

body.esc-layout {
    min-width: 960px;
    margin: 0 auto;    
}
.promo-bar {
    display: block;        
}
.promo-bar .customer-care-wrapper {
    float: left;
    max-width: 50%;
}
.promo-bar .customer-care {
    font-size: 11px;
    color: #000;
    margin-left: 15px;
    display: block;
}
.promo-bar {
    width: 100%;
    min-height: 32px;
    position: relative;
    height: auto;
    overflow: visible;
    z-index: 5;
    background-color: #EEE;
    overflow: hidden;
  }
.promo-bar .service-message-wrapper {
    padding-top: 2px;
    max-width: 50%;
    margin: 0 auto;
    position: relative;
   
    
}

.service-message-wrapper .service-banner{
    position: absolute;
    left: 0px;
    right: 0px;
    text-align: center;
    background: red;
}
.caption-wrapper{
    position: relative;
    background: black;
}

.service-message-wrapper .captions{
  
    font-family: inherit;
    font-style: italic;
    font-size: 14px;
    color: white;    
}
<body class="esc-layout">
        <div class="promo-bar">
            <div class="customer-care-wrapper promo-block">
                <div class="customer-care" style="padding-top:10px; padding-bottoms:12px;">
                    "      Contact us 24/7: "
                </div>
        
            </div>
            <div class="service-message-wrapper promo-block" style="height: 28px;">
                <div class="service-banner service-message-1" style="margin-top: 0px;">
                    <div class="caption-wrapper">
                        <p class="captions">
                        
                            <span> Same-day delivery to New York  </span>   
                        </p>
                    </div>    
                </div>
            
            </div>
        
        </div>
       
        
    </body>

3个回答

9
你可以使用position: absolutetoptransform的组合。
技巧在于,在top: 50%中,50%指的是父元素的高度。而在transform中,50%指的是元素本身的高度。

.outer {
  height: 50px;
  width: 50%;
  position: absolute;
  top: 0;
  right: 0;
  background: red;
 }

.inner {
  position: absolute;
  left: 0;
  

  /* make the top edge of .inner appear in the vertical center of .outer */
  top: 50%;
 
  /* move .inner up by half of its height so that its middle is in the middle of .outer */
  transform: translateY(-50%);

  height: 20px;
  width: 100%;
  background: black;
 }
<div class="outer">
  <div class="inner"></div>
</div>

更多信息: http://howtocenterincss.com/

3
在绝对定位的元素内居中,内部元素需要设置为绝对定位并给出宽度和高度。
.red-box{
background-color:red;
width:400px;
height:400px;
position:absolute;
left:0;
right:0;
top:0;
bottom:0;
margin:auto;
}

.black-box{
background-color:black;
width:200px;
height:200px;
position:absolute;
top:0;
bottom:0;
left:0;
right:0;
margin:auto;

}

<div class="red-box">
        <div class="black-box"> </div>
</div>

工作示例(点击运行按钮)


0

对于居中的 div,使用 flex box 非常容易。

div.outer {
  align-items: center;
  background: red none repeat scroll 0 0;
  display: flex;
  height: 50px;
  position: absolute;
  right: 0;
  top: 0;
  width: 50%;
}

div.inner {
  background: black none repeat scroll 0 0;
  height: 20px;
  left: 0;
  width: 100%;
}

<html><head>
    </head>

<body>
    <div class="outer">
       <div class="inner"></div>
    </div>

</body>
</html>

不要忘记在Safari和Chrome中使用webkit,在您的情况下,我认为最好设置<p>的margin:0以获得更好的控制。

p.captions{margin:0;}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接