# Tooltip Positions

> Accessible tooltips with arrows that open above, below, left or right, set with a single data attribute.

- Category: [CSS Effects](https://copy-css.com/category/effects)
- Tags: tooltip, top, bottom, left, right, arrow, aria-describedby
- Live preview: https://copy-css.com/snippets/tooltip-positions
- License: Free for personal and commercial use. No signup, no attribution required.

## HTML

```html
<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>
```

## CSS

```css
.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;
  }
}
```

## Notes

- 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.
- Customize with: `--tip-bg`.
- In JSX use `className` instead of `class`. In Tailwind projects put the CSS in the global stylesheet.
