我遇到了无法解决的问题。我试图把这个黑盒子放在有绝对位置的红盒子里面。我尝试将黑匣子放到相对位置,但我觉得我错过了一些东西。
最终,我试图制作顶级标题。
这是一张图片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>
&#13;
答案 0 :(得分:2)
您可以将position: absolute
与top
和transform
结合使用。
诀窍在于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;
}
&#13;
<div class="outer">
<div class="inner"></div>
</div>
&#13;
答案 1 :(得分:0)
对于中心div,使用弹性盒很容易。
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>
不要忘记使用webkit进行safari和chrome,在你的情况下,我认为最好设置margin:0 <p>
以便更好地控制
p.captions{margin:0;}
答案 2 :(得分:0)
以绝对元素为中心,内部元素需要绝对给出宽度和高度。
<div class="red-box">
<div class="black-box"> </div>
</div>
}
{{1}}