Tooltip | Element Plus

Skip to contentMenu Back to top

Sponsored by

JNPFCRMEB

Overview

Overview

Basic

Button

Border

Color

Layout Container

Icon

Layout

Link

Text

2.3.0

Scrollbar

Space

Splitter

2.10.0

Typography

Configuration

Config Provider

Form

Autocomplete

Cascader

Checkbox

Color Picker Panel

2.11.0

Color Picker

Date Picker Panel

2.11.0

Date Picker

DateTime Picker

Form

Input

Input Number

Input Tag

2.9.0

Mention

2.8.0

Radio

Rate

Select

Virtualized Select

Slider

Switch

Time Picker

Time Select

Transfer

TreeSelect

2.1.8

Upload

Data

Avatar

Badge

Calendar

Card

Carousel

Collapse

Descriptions

Empty

Image

Infinite Scroll

Pagination

Progress

Result

Skeleton

Table

Virtualized Table

2.2.0

Tag

Timeline

Tour

2.5.0

Tree

Virtualized Tree

Statistic

2.2.30

Segmented

2.7.0

Navigation

Affix

Anchor

2.6.0

Backtop

Breadcrumb

Dropdown

Menu

Page Header

Steps

Tabs

Feedback

Alert

Dialog

Drawer

Loading

Message

Message Box

Notification

Popconfirm

Popover

Tooltip

Others

Divider

Watermark

2.4.0Tooltip ​

Display prompt information for mouse hover.

Basic usage ​

Tooltip has 9 placements.

Use attribute content to set the display content when hover. The attribute placement determines the position of the tooltip. Its value is [orientation]-[alignment] with four orientations top, left, right, bottom and three alignments start, end, null, and the default alignment is null. Take placement="left-end" for example, Tooltip will display on the left of the element which you are hovering and the bottom of the tooltip aligns with the bottom of the element.

top-starttoptop-endleft-startright-startleftrightleft-endright-endbottom-startbottombottom-endvue<template> <div class="tooltip-base-box"> <div class="row center"> <el-tooltip class="box-item" effect="dark" content="Top Left prompts info" placement="top-start" > <el-button>top-start</el-button> </el-tooltip> <el-tooltip class="box-item" effect="dark" content="Top Center prompts info" placement="top" > <el-button>top</el-button> </el-tooltip> <el-tooltip class="box-item" effect="dark" content="Top Right prompts info" placement="top-end" > <el-button>top-end</el-button> </el-tooltip> </div> <div class="row"> <el-tooltip class="box-item" effect="dark" content="Left Top prompts info" placement="left-start" > <el-button>left-start</el-button> </el-tooltip> <el-tooltip class="box-item" effect="dark" content="Right Top prompts info" placement="right-start" > <el-button>right-start</el-button> </el-tooltip> </div> <div class="row"> <el-tooltip class="box-item" effect="dark" content="Left Center prompts info" placement="left" > <el-button class="mt-3 mb-3">left</el-button> </el-tooltip> <el-tooltip class="box-item" effect="dark" content="Right Center prompts info" placement="right" > <el-button>right</el-button> </el-tooltip> </div> <div class="row"> <el-tooltip class="box-item" effect="dark" content="Left Bottom prompts info" placement="left-end" > <el-button>left-end</el-button> </el-tooltip> <el-tooltip class="box-item" effect="dark" content="Right Bottom prompts info" placement="right-end" > <el-button>right-end</el-button> </el-tooltip> </div> <div class="row center"> <el-tooltip class="box-item" effect="dark" content="Bottom Left prompts info" placement="bottom-start" > <el-button>bottom-start</el-button> </el-tooltip> <el-tooltip class="box-item" effect="dark" content="Bottom Center prompts info" placement="bottom" > <el-button>bottom</el-button> </el-tooltip> <el-tooltip class="box-item" effect="dark" content="Bottom Right prompts info" placement="bottom-end" > <el-button>bottom-end</el-button> </el-tooltip> </div> </div> </template> <style> .tooltip-base-box { width: 600px; } .tooltip-base-box .row { display: flex; align-items: center; justify-content: space-between; } .tooltip-base-box .center { justify-content: center; } .tooltip-base-box .box-item { width: 110px; margin-top: 10px; } </style> Hide source

Theme ​

Tooltip has two built-in themes: dark and light.

TIP

To use customized theme, you will have to known where your tooltip is rendered into, if your tooltip is rendered into the root element, you will need to set the css rule globally.

It is recommended that not using linear gradient background color when you using customized theme and showing the arrow at the same time, because the popup arrow and the content are two different elements, the popup arrow's style needs to be set individually, and when it comes to the gradient background color, it might seem a little bit weird.

Set effect to modify theme, and the default value is dark.

DarkLightCustomized themevue<template> <el-tooltip content="Top center" placement="top"> <el-button>Dark</el-button> </el-tooltip> <el-tooltip content="Bottom center" placement="bottom" effect="light"> <el-button>Light</el-button> </el-tooltip> <el-tooltip content="Bottom center" effect="customized"> <el-button>Customized theme</el-button> </el-tooltip> </template> <style> .el-popper.is-customized { /* Set padding to ensure the height is 32px */ padding: 6px 12px; background: linear-gradient(90deg, rgb(159, 229, 151), rgb(204, 229, 129)); } .el-popper.is-customized .el-popper__arrow::before { background: linear-gradient(45deg, #b2e68d, #bce689); right: 0; } </style> Hide source

More Content ​

Display multiple lines of text and set their format.

Override attribute content of el-tooltip by adding a slot named content.

Top centervue<template> <el-tooltip placement="top"> <template #content> multiple lines<br />second line </template> <el-button>Top center</el-button> </el-tooltip> </template> Hide source

Advanced usage ​

In addition to basic usages, there are some attributes that allow you to customize your own:

transition attribute allows you to customize the animation in which the tooltip shows or hides, and the default value is el-fade-in-linear.

disabled attribute allows you to disable tooltip. You just need set it to true.

In fact, Tooltip is an extension based on ElPopper, you can use any attribute that are allowed in ElPopper.

click to close tooltip function vue<template> <el-tooltip :disabled="disabled" content="click to close tooltip function" placement="bottom" effect="light" > <el-button @click="disabled = !disabled"> click to {{ disabled ? 'active' : 'close' }} tooltip function </el-button> </el-tooltip> </template> <script lang="ts" setup> import { ref } from 'vue' const disabled = ref(false) </script> Hide source

TIP

The router-link component is not supported in tooltip, please use vm.$router.push.

Disabled form elements are not supported for Tooltip, more information can be found at MDN. You need to wrap the disabled form element with a container element for Tooltip to work.

HTML as content ​

The content attribute can be set to HTML string.

WARNING

Although content property supports HTML strings, dynamically rendering arbitrary HTML on your website can be very dangerous because it can easily lead to XSS attacks. So when raw-content is on, please make sure content is trusted, and never assign user-provided content.

hover mevue<template> <el-tooltip content="<span>The content can be <strong>HTML</strong></span>" raw-content > <el-button>hover me</el-button> </el-tooltip> </template> Hide source

Virtual triggering ​

Sometimes we want to render the tooltip on some other trigger element, we can separate the trigger and the content.

TIP

Virtual triggering tooltip is controlled component, so that you will have to control the visibility of the tooltip your own when this happens, YOU WILL NOT be able to close the tooltip by clicking somewhere else.

testvue<template> <el-tooltip v-model:visible="visible" content="Bottom center" placement="bottom" effect="light" trigger="click" virtual-triggering :virtual-ref="triggerRef" /> <el-button @click="visible = !visible">test</el-button> </template> <script setup lang="ts"> import { onMounted, onUnmounted, ref } from 'vue' const visible = ref(false) const position = ref({ top: 0, left: 0, bottom: 0, right: 0, } as DOMRect) const triggerRef = ref({ getBoundingClientRect: () => position.value, }) const mousemoveHandler = ({ clientX, clientY }: MouseEvent) => { position.value = DOMRect.fromRect({ x: clientX, y: clientY, }) } onMounted(() => { document.addEventListener('mousemove', mousemoveHandler) }) onUnmounted(() => { document.removeEventListener('mousemove', mousemoveHandler) }) </script> Hide source

Singleton ​

Tooltip can also be singleton, which means you can have multiple trigger with only one tooltip instance, this function is implemented based on Virtual triggering

TIP

Known issue: when using singleton, the popup will be bouncing out from unexpected places

Click to open tooltipClick to open tooltipClick to open tooltipvue<template> <div> <el-button v-for="i in 3" :key="i" @mouseover="(e) => (buttonRef = e.currentTarget)" @click="visible = !visible" >Click to open tooltip</el-button > </div> <el-tooltip ref="tooltipRef" :visible="visible" :popper-options="{ modifiers: [ { name: 'computeStyles', options: { adaptive: false, enabled: false, }, }, ], }" :virtual-ref="buttonRef" virtual-triggering popper-class="singleton-tooltip" > <template #content> <span> Some content </span> </template> </el-tooltip> </template> <script setup lang="ts"> import { ref } from 'vue' const buttonRef = ref() const tooltipRef = ref() const visible = ref(false) </script> <style> .singleton-tooltip { transition: transform 0.3s var(--el-transition-function-fast-bezier); } </style> Hide source

Controlled ​

Tooltip can be controlled by the parent component, by using :visible you can implement two way binding.

Hover me vue<template> <el-tooltip :visible="visible"> <template #content> <span>Content</span> </template> <el-button @mouseenter="visible = true" @mouseleave="visible = false"> Hover me </el-button> </el-tooltip> </template> <script setup lang="ts"> import { ref } from 'vue' const visible = ref(false) </script> Hide source

Animations ​

Tooltip can be customized animated, you can set the desired animation use transition.

TIP

Transition Classes, more information can be found at Vue Transition.

trigger mevue<template> <el-tooltip content="I am an el-tooltip" transition="slide-fade"> <el-button>trigger me</el-button> </el-tooltip> </template> <script lang="ts" setup></script> <style> .slide-fade-enter-active { transition: all 0.3s ease-out; } .slide-fade-leave-active { transition: all 0.8s cubic-bezier(1, 0.5, 0.8, 1); } .slide-fade-enter-from, .slide-fade-leave-to { transform: translateX(120px); opacity: 0; } </style> Hide source

Use the append-to ​

You must wait for the DOM to be mounted before using targetElement.

Click to open tooltipvue<template> <el-tooltip :append-to="targetElement" trigger="click" content="Append to .target" placement="top" > <el-button class="target">Click to open tooltip</el-button> </el-tooltip> </template> <script lang="ts" setup> import { onMounted, ref } from 'vue' const targetElement = ref('') onMounted(() => { targetElement.value = '.target' }) </script> <style scoped> .target { position: relative; margin-top: 20px; } </style> Hide source

API ​

Attributes ​

NameDescriptionTypeDefault
append-towhich element the tooltip CONTENT appends toCSSSelector / HTMLElement
effectTooltip theme, built-in theme: dark / lightenumdark
contentdisplay content, can be overridden by slot#contentstring''
raw-contentwhether content is treated as HTML stringbooleanfalse
placementposition of Tooltipenumbottom
fallback-placementslist of possible positions for Tooltip popper.jsarray
visible / v-model:visiblevisibility of Tooltipboolean
disabledwhether Tooltip is disabledboolean
offsetoffset of the Tooltipnumber12
transitionanimation namestring
popper-optionspopper.js parametersobjectrefer to popper.js doc{}
arrow-offset 2.9.10Controls the offset (padding) of the tooltip’s arrow relative to the popper.number5
show-afterdelay of appearance, in millisecond, not valid in controlled modenumber0
show-arrowwhether the tooltip content has an arrowbooleantrue
hide-afterdelay of disappear, in millisecond, not valid in controlled modenumber200
auto-closetimeout in milliseconds to hide tooltip, not valid in controlled modenumber0
popper-classcustom class name for Tooltip's popperstring
popper-stylecustom style for Tooltip's popperstring / object
enterablewhether the mouse can enter the tooltipbooleantrue
teleportedwhether tooltip content is teleported, if true it will be teleported to where append-to setsbooleantrue
triggerHow should the tooltip be triggered (to show), not valid in controlled modeenum / arrayhover
virtual-triggeringIndicates whether virtual triggering is enabledboolean
virtual-refIndicates the reference element to which the tooltip is attachedHTMLElement
trigger-keysWhen you click the mouse to focus on the trigger element, you can define a set of keyboard codes to control the display of tooltip through the keyboard, not valid in controlled modeArray['Enter','Space']
persistentwhen tooltip inactive and persistent is false , tooltip will be destroyedboolean
aria-label a11ysame as aria-labelstring
focus-on-target 2.11.2when triggering tooltips through hover, whether to focus the trigger element, which improves accessibilitybooleanfalse

Events ​

NameDescriptionType
before-showTriggers before tooltip is shown. Passes trigger reason as argument.Function
showTriggers when tooltip is shown. Passes trigger reason as argument.Function
before-hideTriggers before tooltip is hidden. Passes trigger reason as argument.Function
hideTriggers when tooltip is hidden. Passes trigger reason as argument.Function

Slots ​

NameDescription
defaultTooltip triggering & reference element, only a single root element is accepted
contentcustomize content

Exposes ​

NameDescriptionType
popperRefel-popper component instanceobject
contentRefel-tooltip-content component instanceobject
isFocusInsideContentvalidate current focus event is trigger inside el-tooltip-contentFunction
updatePopperupdate el-popper component instanceFunction
onOpenexpose onOpen function to mange el-tooltip open stateFunction
onCloseexpose onClose function to mange el-tooltip open stateFunction
hideexpose hide functionFunction

FAQ ​

How to allow spaces in the input box when tooltip is nested? ​

Typical issue: #20907

vue<template> <el-tooltip content="tooltip content" placement="top" :trigger-keys="[]"> <el-input v-model="value" placeholder="" /> </el-tooltip> </template>

Source ​

Component • Style • Docs

Contributors ​

PopoverDivider

Contents

Basic usageThemeMore ContentAdvanced usageHTML as contentVirtual triggeringSingletonControlledAnimationsUse the append-toAPIAttributesEventsSlotsExposesFAQHow to allow spaces in the input box when tooltip is nested?SourceContributors

Sponsored by

Become a Sponsor!frsimplebit

Từ khóa » Html I Element Tooltip