style.css 样式
/* START TOOLTIP STYLES */
[tooltip] {
border-radius: 3px;
position: relative; /* opinion 1 */
}
/* Applies to all tooltips */
[tooltip]::before,
[tooltip]::after {
text-transform: none; /* opinion 2 */
font-size: .9em; /* opinion 3 */
line-height: 1;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
pointer-events: none;
position: absolute;
display: none;
opacity: 0;
}
[tooltip]::before {
content: '';
border: 5px solid transparent; /* opinion 4 */
z-index: 1001; /* absurdity 1 */
}
[tooltip]::after {
content: attr(tooltip); /* magic! */
/* most of the rest of this is opinion */
font-family: 'Microsoft YaHei','Lantinghei SC','Open Sans',Arial,'Hiragino Sans GB','STHeiti','WenQuanYi Micro Hei','SimSun',sans-serif;
text-align: center;
/*
Let the content set the size of the tooltips
but this will also keep them from being obnoxious
*/
min-width: 3em;
max-width: 21em;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding: 1ch 1.5ch;
border-radius: .3ch;
box-shadow: 0 1em 1em -.5em rgba(0, 0, 0, 0.15);
color: #fff;
z-index: 1000; /* absurdity 2 */
}
/* Make the tooltips respond to hover */
[tooltip]:hover::before,
[tooltip]:hover::after {
display: block;
}
/* don't show empty tooltips */
[tooltip='']::before,
[tooltip='']::after {
display: none !important;
}
/* FLOW: UP */
[tooltip][flow^="up"] {
background-color: #5bc0de;
border: 1px solid #46b8da;
}
[tooltip]:not([flow])::before,
[tooltip][flow^="up"]::before {
bottom: 100%;
border-bottom-width: 0;
border-top-color: #46b8da;
}
[tooltip]:not([flow])::after,
[tooltip][flow^="up"]::after {
background-color: #46b8da;
bottom: calc(100% + 5px);
}
[tooltip]:not([flow])::before,
[tooltip]:not([flow])::after,
[tooltip][flow^="up"]::before,
[tooltip][flow^="up"]::after {
left: 50%;
-webkit-transform: translate(-50%, -.5em);
transform: translate(-50%, -.5em);
}
/* FLOW: DOWN */
[tooltip][flow^="down"] {
background-color: #00d46a;
border: 1px solid #00c161;
}
[tooltip][flow^="down"]::before {
top: 100%;
border-top-width: 0;
border-bottom-color: #00c161;
}
[tooltip][flow^="down"]::after {
background-color: #00c161;
top: calc(100% + 5px);
}
[tooltip][flow^="down"]::before,
[tooltip][flow^="down"]::after {
left: 50%;
-webkit-transform: translate(-50%, .5em);
transform: translate(-50%, .5em);
}
/* FLOW: LEFT */
[tooltip][flow^="left"] {
background-color: #ff6055;
border: 1px solid #ff483a;
}
[tooltip][flow^="left"]::before {
top: 50%;
border-right-width: 0;
border-left-color: #ff483a;
left: calc(0em - 5px);
-webkit-transform: translate(-.5em, -50%);
transform: translate(-.5em, -50%);
}
[tooltip][flow^="left"]::after {
background-color: #ff483a;
top: 50%;
right: calc(100% + 5px);
-webkit-transform: translate(-.5em, -50%);
transform: translate(-.5em, -50%);
}
/* FLOW: RIGHT */
[tooltip][flow^="right"] {
background-color: #ffc928;
border: 1px solid #ffc000;
}
[tooltip][flow^="right"]::before {
top: 50%;
border-left-width: 0;
border-right-color: #ffc000;
right: calc(0em - 5px);
-webkit-transform: translate(.5em, -50%);
transform: translate(.5em, -50%);
}
[tooltip][flow^="right"]::after {
background-color: #ffc000;
top: 50%;
left: calc(100% + 5px);
-webkit-transform: translate(.5em, -50%);
transform: translate(.5em, -50%);
}
/* KEYFRAMES */
@-webkit-keyframes tooltips-vert {
to {
opacity: .9;
-webkit-transform: translate(-50%, 0);
transform: translate(-50%, 0);
}
}
@keyframes tooltips-vert {
to {
opacity: .9;
-webkit-transform: translate(-50%, 0);
transform: translate(-50%, 0);
}
}
@-webkit-keyframes tooltips-horz {
to {
opacity: .9;
-webkit-transform: translate(0, -50%);
transform: translate(0, -50%);
}
}
@keyframes tooltips-horz {
to {
opacity: .9;
-webkit-transform: translate(0, -50%);
transform: translate(0, -50%);
}
}
/* FX All The Things */
[tooltip]:not([flow]):hover::before,
[tooltip]:not([flow]):hover::after,
[tooltip][flow^="up"]:hover::before,
[tooltip][flow^="up"]:hover::after,
[tooltip][flow^="down"]:hover::before,
[tooltip][flow^="down"]:hover::after {
-webkit-animation: tooltips-vert 300ms ease-out forwards;
animation: tooltips-vert 300ms ease-out forwards;
}
[tooltip][flow^="left"]:hover::before,
[tooltip][flow^="left"]:hover::after,
[tooltip][flow^="right"]:hover::before,
[tooltip][flow^="right"]:hover::after {
-webkit-animation: tooltips-horz 300ms ease-out forwards;
animation: tooltips-horz 300ms ease-out forwards;
}
/* UNRELATED to tooltips */
body {
margin: 0;
min-height: 100vh;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
font-family:'Microsoft YaHei','Lantinghei SC','Open Sans',Arial,'Hiragino Sans GB','STHeiti','WenQuanYi Micro Hei','SimSun',sans-serif;
}
main {
-webkit-box-flex: 1;
-ms-flex: 1 1 100vh;
flex: 1 1 100vh;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
aside {
-webkit-box-flex: 0;
-ms-flex: none;
flex: none;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
background: #49b293;
color: #fff;
padding: 1em;
}
main div {
text-align: center;
color: #353539;
}
main span {
padding: .5em 1em;
margin: .5em;
display: inline-block;
color: #fff;
}
aside a {
color: inherit;
text-decoration: none;
font-weight: bold;
display: inline-block;
padding: .4em 1em;
}
html:
<html>
<head>
<meta charset="utf-8">
<title>纯CSS的Tooltip消息提示</title>
<link type="text/css" rel="stylesheet" href="style.css">
</head>
<body>
<main>
<div>
<span tooltip="从上面滑入提示信息" flow="up">上面滑入提示</span>
</div>
<div>
<span tooltip="从左侧滑入提示信息" flow="left">左侧滑入提示</span>
<span tooltip="从右侧滑入提示信息" flow="right">右侧滑入提示</span>
</div>
<div>
<span tooltip="从下面滑入提示信息" flow="down">下面滑入提示</span>
</div>
</main>
</body>
</html>