gpt4 book ai didi

css - 保持 Popover 相对于按钮居中

转载 作者:太空宇宙 更新时间:2023-11-03 20:32:40 24 4
gpt4 key购买 nike

我正在试验发现的这个 CSS 弹出框 http://codepen.io/derekpcollins/pen/JCLhG/ .这是弹出窗口的代码。

.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;
}

悬停时,弹出框看起来像这样

enter image description here

我想让弹出窗口更宽,但是当我将宽度更改为 600 像素时,它会变得错位。

enter image description here

无论按钮有多宽,我都希望弹出框的箭头指向按钮怎么办?

谢谢

最佳答案

如果您希望弹出框为任意动态宽度并居中,请移除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>

关于css - 保持 Popover 相对于按钮居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38771715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com