我正在尝试找到http://codepen.io/derekpcollins/pen/JCLhG/这个CSS popover。这是popover的代码。
.popover {
background-color: rgba(0,0,0,0.85);
border-radius: 5px;
bottom: 42px;
box-shadow: 0 0 5px rgba(0,0,0,0.4);
color: #fff;
display: none;
font-size: 12px;
font-family: 'Helvetica',sans-serif;
left: -95px;
padding: 7px 10px;
position: absolute;
width: 200px;
z-index: 4;
}
并且在悬停时,popover看起来像这样
我想让弹片更宽,但是当我将宽度改为600px时,它会变得不对齐。
如果我希望弹出窗口的箭头指向按钮,无论它有多宽,我该怎么办?
由于
答案 0 :(得分:3)
如果您希望popover为任何动态宽度并居中,请移除width
,设置white-space:nowrap
并将定位更改为
left: 50%;
transform:translateX(-50%);
white-space: nowrap;
* {
box-sizing: border-box;
}
body {
background-color: #e3fbff;
}
/* Just to center things */
.center {
margin: 75px auto;
width: 30px;
}
/* The element to hover over */
.qs {
background-color: #02bdda;
border-radius: 16px;
color: #e3fbff;
cursor: default;
display: inline-block;
font-family: 'Helvetica',sans-serif;
font-size: 18px;
font-weight: bold;
height: 30px;
line-height: 30px;
position: relative;
text-align: center;
width: 30px;
}
.qs .popover {
background-color: rgba(0, 0, 0, 0.85);
border-radius: 5px;
bottom: 42px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.4);
color: #fff;
display: none;
font-size: 12px;
font-family: 'Helvetica',sans-serif;
padding: 7px 10px;
position: absolute;
white-space: nowrap;
z-index: 4;
left: 50%;
transform: translateX(-50%);
}
.qs .popover:before {
border-top: 7px solid rgba(0, 0, 0, 0.85);
border-right: 7px solid transparent;
border-left: 7px solid transparent;
bottom: -7px;
content: '';
display: block;
left: 50%;
margin-left: -7px;
position: absolute;
}
.qs:hover .popover {
display: block;
-webkit-animation: fade-in .3s linear 1, move-up .3s linear 1;
-moz-animation: fade-in .3s linear 1, move-up .3s linear 1;
-ms-animation: fade-in .3s linear 1, move-up .3s linear 1;
}
@-webkit-keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@-moz-keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@-ms-keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@-webkit-keyframes move-up {
from {
bottom: 30px;
}
to {
bottom: 42px;
}
}
@-moz-keyframes move-up {
from {
bottom: 30px;
}
to {
bottom: 42px;
}
}
@-ms-keyframes move-up {
from {
bottom: 30px;
}
to {
bottom: 42px;
}
}
<div class="center">
<span class="qs">? <span class="popover above">Hey bro, cool popover!</span></span>
</div>
<div class="center">
<span class="qs">? <span class="popover above">Short popover!</span></span>
</div>
答案 1 :(得分:2)
它不起作用,因为该位置使用px
值指定,因此您必须调整width
和position
(在这种情况下为left
属性)。
如果您想要一个可以动态的解决方案(对于所有可能的宽度,意味着),我建议如下。只需在课程.popover
上添加/更改以下属性:
width: 600px; /* whatever you want */
left: 50%;
transform: translateX(-50%);
请参阅Codepen