CSS Tooltip Positions
Accessible tooltips with arrows that open above, below, left or right, set with a single data attribute.
<div class="tooltip-pos-grid">
<span class="tooltip-pos" data-pos="top">
<button type="button" aria-describedby="tooltip-pos-top">Top</button>
<span role="tooltip" id="tooltip-pos-top">Shown above</span>
</span>
<span class="tooltip-pos" data-pos="bottom">
<button type="button" aria-describedby="tooltip-pos-bottom">Bottom</button>
<span role="tooltip" id="tooltip-pos-bottom">Shown below</span>
</span>
<span class="tooltip-pos" data-pos="right">
<button type="button" aria-describedby="tooltip-pos-right">Right</button>
<span role="tooltip" id="tooltip-pos-right">To the right</span>
</span>
<span class="tooltip-pos" data-pos="left">
<button type="button" aria-describedby="tooltip-pos-left">Left</button>
<span role="tooltip" id="tooltip-pos-left">To the left</span>
</span>
</div>
.tooltip-pos-grid {
display: grid;
grid-template-columns: auto auto;
gap: 44px 40px;
font-family: system-ui, sans-serif;
}
.tooltip-pos {
--tip-bg: #1e293b;
position: relative;
display: inline-block;
}
.tooltip-pos button {
padding: 8px 14px;
border: 1px solid #cbd5e1;
border-radius: 8px;
background: #fff;
color: #0f172a;
font: 500 14px system-ui, sans-serif;
cursor: pointer;
}
.tooltip-pos button:focus-visible {
outline: 2px solid #2563eb;
outline-offset: 2px;
}
.tooltip-pos [role="tooltip"] {
position: absolute;
z-index: 10;
padding: 6px 10px;
border-radius: 6px;
background: var(--tip-bg);
color: #fff;
font-size: 13px;
white-space: nowrap;
opacity: 0;
visibility: hidden;
pointer-events: none;
transition: opacity 0.15s ease, visibility 0.15s;
}
.tooltip-pos [role="tooltip"]::after {
content: "";
position: absolute;
border: 5px solid transparent;
}
.tooltip-pos:hover [role="tooltip"],
.tooltip-pos:focus-within [role="tooltip"] {
opacity: 1;
visibility: visible;
}
.tooltip-pos[data-pos="top"] [role="tooltip"] {
bottom: calc(100% + 8px);
left: 50%;
translate: -50% 0;
}
.tooltip-pos[data-pos="top"] [role="tooltip"]::after {
top: 100%;
left: 50%;
margin-left: -5px;
border-top-color: var(--tip-bg);
}
.tooltip-pos[data-pos="bottom"] [role="tooltip"] {
top: calc(100% + 8px);
left: 50%;
translate: -50% 0;
}
.tooltip-pos[data-pos="bottom"] [role="tooltip"]::after {
bottom: 100%;
left: 50%;
margin-left: -5px;
border-bottom-color: var(--tip-bg);
}
.tooltip-pos[data-pos="right"] [role="tooltip"] {
top: 50%;
left: calc(100% + 8px);
translate: 0 -50%;
}
.tooltip-pos[data-pos="right"] [role="tooltip"]::after {
top: 50%;
right: 100%;
margin-top: -5px;
border-right-color: var(--tip-bg);
}
.tooltip-pos[data-pos="left"] [role="tooltip"] {
top: 50%;
right: calc(100% + 8px);
translate: 0 -50%;
}
.tooltip-pos[data-pos="left"] [role="tooltip"]::after {
top: 50%;
left: 100%;
margin-top: -5px;
border-left-color: var(--tip-bg);
}
@media (prefers-reduced-motion: reduce) {
.tooltip-pos [role="tooltip"] {
transition: none;
}
}
About this CSS Tooltip Positions
This CSS Tooltip Positions is 112 lines of pure CSS (2.1 KB) with no JavaScript and no dependencies. It uses transitions, CSS Grid, keyboard focus styles and custom properties. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.
CSS features used
Customize it
Change these custom properties to restyle the tooltip positions without touching the rest of the CSS:
| Property | Default |
|---|---|
--tip-bg | #1e293b |
How to use this tooltip positions
- Copy the HTML and paste it where you want the element to appear.
- Copy the CSS into your stylesheet, or inside a
<style>tag. - Keep the
.tooltip-pos-gridclass name, or rename it in both places. Then tweak colors, sizes and timing to match your design. - Using React or Next.js? Change
classtoclassNamein the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).
Building with an AI tool?
“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.