A small weekly bar chart where the hovered bar turns solid and a value label pops out of its top. Each bar's height is one --h value, which also places the label.
Also known as: bar chart hover css, chart tooltip on hover, column chart hover effect.
<ul class="hfx-chart-bar-tooltip" aria-label="Sales this week"> <li style="--h: 45%" data-value="$1.2k"><span>Mon</span></li> <li style="--h: 62%" data-value="$1.6k"><span>Tue</span></li> <li style="--h: 38%" data-value="$980"><span>Wed</span></li> <li style="--h: 80%" data-value="$2.1k"><span>Thu</span></li> <li style="--h: 95%" data-value="$2.5k"><span>Fri</span></li> <li style="--h: 58%" data-value="$1.5k"><span>Sat</span></li> <li style="--h: 30%" data-value="$790"><span>Sun</span></li> </ul>
.hfx-chart-bar-tooltip {
--c: var(--hfx-color, #3b82f6);
font-size: var(--hfx-size, 16px);
display: grid;
grid-template-columns: repeat(7, 1.9em);
gap: 0.55em;
height: 7.5em;
margin: 0 0 1.3em;
padding: 0 0.4em;
list-style: none;
border-bottom: 1px solid color-mix(in srgb, var(--c) 30%, transparent);
font-family: system-ui, sans-serif;
}
.hfx-chart-bar-tooltip li {
position: relative;
display: flex;
align-items: flex-end;
cursor: pointer;
}
.hfx-chart-bar-tooltip li::before {
content: "";
width: 100%;
height: var(--h);
border-radius: 0.35em 0.35em 0.1em 0.1em;
background: color-mix(in srgb, var(--c) 28%, transparent);
transition: background-color 0.2s ease, box-shadow 0.25s ease;
}
.hfx-chart-bar-tooltip li:nth-child(5)::before { background: color-mix(in srgb, var(--c) 60%, transparent); }
.hfx-chart-bar-tooltip li::after {
content: attr(data-value);
position: absolute;
left: 50%;
bottom: calc(var(--h) + 0.45em);
padding: 0.25em 0.5em;
border-radius: 0.4em;
background: var(--c);
color: #fff;
font-size: 0.75em;
font-weight: 700;
white-space: nowrap;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 0.5em) scale(0.7);
transform-origin: 50% 100%;
transition: opacity 0.2s ease, transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.hfx-chart-bar-tooltip span {
position: absolute;
top: calc(100% + 0.5em);
left: 50%;
color: color-mix(in srgb, currentColor 55%, transparent);
font-size: 0.72em;
font-weight: 600;
transform: translateX(-50%);
transition: color 0.2s ease;
}
.hfx-chart-bar-tooltip li:hover::before {
background: var(--c);
box-shadow: 0 0 1em -0.2em color-mix(in srgb, var(--c) 70%, transparent);
}
.hfx-chart-bar-tooltip li:hover::after {
opacity: 1;
transform: translate(-50%, 0) scale(1);
}
.hfx-chart-bar-tooltip li:hover span { color: var(--c); }
@media (prefers-reduced-motion: reduce) {
.hfx-chart-bar-tooltip, .hfx-chart-bar-tooltip *, .hfx-chart-bar-tooltip::before, .hfx-chart-bar-tooltip::after, .hfx-chart-bar-tooltip *::before, .hfx-chart-bar-tooltip *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}--hfx-color on the element or a parent.
Currently previewing #3b82f6.--hfx-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 16px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.