--- meta: title: Tooltip description: Tooltips display additional information based on a specific action. layout: component --- A tooltip's target is its _first child element_, so you should only wrap one element inside of the tooltip. If you need the tooltip to show up for multiple elements, nest them inside a container first. Tooltips use `display: contents` so they won't interfere with how elements are positioned in a flex or grid layout. ```html:preview Hover Me ``` ```jsx:react import SlButton from '@shoelace-style/shoelace/dist/react/button'; import SlTooltip from '@shoelace-style/shoelace/dist/react/tooltip'; const App = () => ( Hover Me ); ``` ## Examples ### Placement Use the `placement` attribute to set the preferred placement of the tooltip. ```html:preview
``` ```jsx:react import SlButton from '@shoelace-style/shoelace/dist/react/button'; import SlTooltip from '@shoelace-style/shoelace/dist/react/tooltip'; const css = ` .tooltip-placement-example { width: 250px; } .tooltip-placement-example-row:after { content: ''; display: table; clear: both; } .tooltip-placement-example sl-button { float: left; width: 2.5rem; margin-right: 0.25rem; margin-bottom: 0.25rem; } .tooltip-placement-example-row:nth-child(1) sl-tooltip:first-child sl-button, .tooltip-placement-example-row:nth-child(5) sl-tooltip:first-child sl-button { margin-left: calc(40px + 0.25rem); } .tooltip-placement-example-row:nth-child(2) sl-tooltip:nth-child(2) sl-button, .tooltip-placement-example-row:nth-child(3) sl-tooltip:nth-child(2) sl-button, .tooltip-placement-example-row:nth-child(4) sl-tooltip:nth-child(2) sl-button { margin-left: calc((40px * 3) + (0.25rem * 3)); } `; const App = () => ( <>
); ``` ### Click Trigger Set the `trigger` attribute to `click` to toggle the tooltip on click instead of hover. ```html:preview Click to Toggle ``` ```jsx:react import SlButton from '@shoelace-style/shoelace/dist/react/button'; import SlTooltip from '@shoelace-style/shoelace/dist/react/tooltip'; const App = () => ( Click to Toggle ); ``` ### Manual Trigger Tooltips can be controlled programmatically by setting the `trigger` attribute to `manual`. Use the `open` attribute to control when the tooltip is shown. ```html:preview Toggle Manually ``` {% raw %} ```jsx:react import { useState } from 'react'; import SlAvatar from '@shoelace-style/shoelace/dist/react/avatar'; import SlButton from '@shoelace-style/shoelace/dist/react/button'; import SlTooltip from '@shoelace-style/shoelace/dist/react/tooltip'; const App = () => { const [open, setOpen] = useState(false); return ( <> setOpen(!open)}> Toggle Manually ); }; ``` {% endraw %} ### Removing Arrows You can control the size of tooltip arrows by overriding the `--sl-tooltip-arrow-size` design token. To remove them, set the value to `0` as shown below. ```html:preview No Arrow ``` {% raw %} ```jsx:react import SlButton from '@shoelace-style/shoelace/dist/react/button'; import SlTooltip from '@shoelace-style/shoelace/dist/react/tooltip'; const App = () => (
Above Below
); ``` {% endraw %} To override it globally, set it in a root block in your stylesheet after the Shoelace stylesheet is loaded. ```css :root { --sl-tooltip-arrow-size: 0; } ``` ### HTML in Tooltips Use the `content` slot to create tooltips with HTML content. Tooltips are designed only for text and presentational elements. Avoid placing interactive content, such as buttons, links, and form controls, in a tooltip. ```html:preview
I'm not just a tooltip, I'm a tooltip with HTML!
Hover me
``` ```jsx:react import SlButton from '@shoelace-style/shoelace/dist/react/button'; import SlTooltip from '@shoelace-style/shoelace/dist/react/tooltip'; const App = () => (
I'm not just a tooltip, I'm a tooltip with HTML!
Hover Me
); ``` ### Setting a Maximum Width Use the `--max-width` custom property to change the width the tooltip can grow to before wrapping occurs. ```html:preview Hover me ``` {% raw %} ```jsx:react import SlButton from '@shoelace-style/shoelace/dist/react/button'; import SlTooltip from '@shoelace-style/shoelace/dist/react/tooltip'; const App = () => ( Hover Me ); ``` {% endraw %} ### Hoisting Tooltips will be clipped if they're inside a container that has `overflow: auto|hidden|scroll`. The `hoist` attribute forces the tooltip to use a fixed positioning strategy, allowing it to break out of the container. In this case, the tooltip will be positioned relative to its [containing block](https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#Identifying_the_containing_block), which is usually the viewport unless an ancestor uses a `transform`, `perspective`, or `filter`. [Refer to this page](https://developer.mozilla.org/en-US/docs/Web/CSS/position#fixed) for more details. ```html:preview
No Hoist Hoist
``` ```jsx:react import SlButton from '@shoelace-style/shoelace/dist/react/button'; import SlTooltip from '@shoelace-style/shoelace/dist/react/tooltip'; const css = ` .tooltip-hoist { border: solid 2px var(--sl-panel-border-color); overflow: hidden; padding: var(--sl-spacing-medium); position: relative; } `; const App = () => ( <>
No Hoist Hoist
); ```