我在想也许我可以用
calc()
具有
max-content
但显然这不是它的工作方式。
*[tooltip] {
position: relative;
&:hover {
&:after {
content: attr(tooltip);
display: block;
position: absolute;
transform: translateX(-50%);
background: rgba(0, 0, 0, 0.8);
color: white;
padding: 6px 12px;
border-radius: 4px;
position: absolute;
top: 50px;
left: 0px;
width: max-content;
font-weight:bold;
font-size:0.9em;
text-align:center;
margin-left: calc(max-content / 2);
}
}
}
我总是可以使用100%作为宽度,但它总是父对象的宽度,有时太小,有时太多;因此我使用
最大含量
.
<div tooltip="Hello World">Hover me.</div>
不使用JS
编辑:
这是它应该是什么样子的图像。
使用“最大内容”时,宽度总是与其他工具提示不同,这就是为什么我很难将其居中的原因。