Skip to content

Instantly share code, notes, and snippets.

@nshimiye
Last active May 30, 2019 23:58
Show Gist options
  • Save nshimiye/ea9ba367c6163a45c8d6fa6ec13d9c48 to your computer and use it in GitHub Desktop.
Save nshimiye/ea9ba367c6163a45c8d6fa6ec13d9c48 to your computer and use it in GitHub Desktop.
Tooltip styling
:host {
--tip-content-padding: 15px;
--tip-content-width: 250px;
--tip-content-half-width: calc(var(--tip-content-width) / 2);
--tip-content-view-spacing: 5px;
--tip-pointer-height: 44px;
--tip-pointer-half-height: calc(var(--tip-pointer-height) / 2);
--tip-pointer-top: calc(100% + 0.414 * var(--tip-pointer-half-height)); /* 0.414 = trigonometric factor cause by the rotation rotate(-45deg) */
}
.tooltip {
position: relative;
width: fit-content; /* takes the width of the wrapped content, NOTE that this does not take into account content with absolute position */
display: table; /* fixes IE "missing fit-content" issue */
}
/* START tip-pointer */
.tooltip:hover::before {
position: absolute;
top: 100%;
left: calc(50% - var(--tip-pointer-half-height));
content: " ";
height: 0;
width: 0;
border: var(--tip-pointer-half-height) solid transparent;
}
.tooltip:hover::after {
top: var(--tip-pointer-top);
left: calc(50% - var(--tip-pointer-half-height));
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
border: var(--tip-pointer-half-height) solid transparent;
border-top-color: #fff;
border-right-color: #fff;
box-shadow: 1px -1px 0 0 rgba(0, 0, 0, 0.1);
transform-origin: center;
transform: rotate(-45deg);
}
/* END tip-pointer */
.tooltip__label {
width: fit-content;
display: table;
cursor: help;
}
.tooltip__tip {
width: 50%;
display: none;
position: absolute;
top: calc(var(--tip-pointer-top) + var(--tip-pointer-half-height));
left: 50%;
}
.tooltip:hover .tooltip__tip {
display: block;
}
.tooltip__tip__content {
background-color: white;
box-shadow: 0 2px 9px 0 rgba(0, 0, 0, 0.3);
width: var(--tip-content-width);
padding: var(--tip-content-padding);
white-space: normal; /* allow for paragraphs to take multiple lines */
position: absolute;
left: calc(0px - var(--tip-content-half-width) - var(--tip-content-padding));
}
.tooltip__tip__content--fixto-left {
position: fixed;
left: var(--tip-content-view-spacing);
right: auto;
}
.tooltip__tip__content--fixto-right {
position: fixed;
left: auto;
right: var(--tip-content-view-spacing);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment