CSS:悬浮提示文本
程序员文章站
2024-03-25 16:55:04
...
Way 1⃣️
<div class="wrap">
我是一个元素
<div class="tips">
这是悬浮提示文字
</div>
</div>
.wrap {
position: relative;
display: inline-block;
margin: 4em;
}
.tips {
position: absolute;
top: -2em;
left: 50%;
display: none;
white-space: nowrap;
transform: translate(-50%, 0);
}
.wrap:hover .tips {
display: block;
}
Way 2⃣️
content: attr()
:获取自定义标签的内容。
<div class="tips-demo" data-tips="提示文本">演示文本</div>
.tips-demo {
position: relative;
}
.tips-demo:after {
content: attr(data-tips);
position: absolute;
top: 0;
left: 0;
right: 0;
margin: 0 auto;
white-space: nowrap;
opacity: 0;
transform: translateY(-150%);
transition: .1s;
}
.tips-demo:hover:after {
opacity: 1;
transform: translateY(-100%);
}
Way 3⃣️
<div class="haveTips" tips='hello world'>Hi</div>
div.haveTips {
width: 150px;
text-align: center;
background-color: #F7F5F1;
color: #BBA985;
font-family: sans-serif;
padding: 1em;
border-radius: 10px;
position: relative;
cursor: pointer;
}
div.haveTips::after {
content: attr(tips);
padding: 5px;
background-color: #B4A078;
box-shadow: 0 0 5px #B4A078;
color: white;
min-width: 5em;
max-width: 100%;
text-align: center;
border-radius: 5px;
position: absolute;
bottom: 100%;
left: 50%;
transform: translate(-50%, -10px);
opacity: 0;
transition: all 0.5s;
}
div.haveTips:hover::after {
opacity: 1;
}
div.haveTips::before {
content: '';
width: 0;
height: 0;
border: 6px solid transparent;
border-top-color: #B4A078;
position: absolute;
top: -10px;
left: calc(50% - 6px);
opacity: 0;
transition: all 0.5s;
}
div.haveTips:hover::before {
opacity: 1;
}
上一篇: OpenGl3.3初识00
下一篇: RESTful