Movatterモバイル変換


[0]ホーム

URL:


Framework:


Hire Us Get CoreUI PRO all-access

Bootstrap 5 Popovers

Popovers

Documentation and examples for adding Bootstrap popovers, like those found in iOS, to any element on your site.

🤖 Looking for the LLM-optimized version?View llm.md

Other frameworks

CoreUI components are available as native Angular, React, and Vue components. To learn more please visit the following pages.

On this page

Overview

Things to know when using the popover plugin:

  • Popovers rely on the 3rd party libraryPopper for positioning. You must includepopper.min.js before coreui.js or usecoreui.bundle.min.js /coreui.bundle.js which contains Popper in order for popovers to work!
  • Popovers require thetooltip plugin as a dependency.
  • Popovers are opt-in for performance reasons, soyou must initialize them yourself.
  • Zero-lengthtitle andcontent values will never show a popover.
  • Specifycontainer: 'body' to avoid rendering problems in more complex components (like our input groups, button groups, etc).
  • Triggering popovers on hidden elements will not work.
  • Popovers for.disabled ordisabled elements must be triggered on a wrapper element.
  • When triggered from anchors that wrap across multiple lines, popovers will be centered between the anchors’ overall width. Use.text-nowrap on your<a>s to avoid this behavior.
  • Popovers must be hidden before their corresponding elements have been removed from the DOM.
  • Popovers can be triggered thanks to an element inside a shadow DOM.
By default, this component utilizes the built-in content sanitizer, which removes any HTML elements that are not explicitly allowed. Refer to thesanitizer section in our JavaScript documentation for more details.
This component's animation effect relies on theprefers-reduced-motion media query. For more information, refer to thereduced motion section of our accessibility documentation.

Keep reading to see how popovers work with some examples.

Examples

Enable popovers everywhere

One way to initialize all popovers on a page would be to select them by theirdata-coreui-toggle attribute:

constpopoverTriggerList=document.querySelectorAll('[data-coreui-toggle="popover"]')constpopoverList=[...popoverTriggerList].map(popoverTriggerEl=>newcoreui.Popover(popoverTriggerEl))

Live demo

We use JavaScript similar to the snippet above to render the following live popover. Titles are set viadata-coreui-title and body content is set viadata-coreui-content.

You can choose to use eithertitle ordata-coreui-title in your HTML. If you opt fortitle, Popper will automatically change it todata-coreui-title upon rendering the element.
html
<buttontype="button"class="btn btn-lg btn-danger"data-coreui-toggle="popover"title="Popover title"data-coreui-content="And here's some amazing content. It's very engaging. Right?">Click to toggle popover</button>

Four directions

Four options are available: top, right, bottom, and left aligned. Directions are mirrored when using CoreUI in RTL.

html
<buttontype="button"class="btn btn-secondary"data-coreui-container="body"data-coreui-toggle="popover"data-coreui-placement="top"data-coreui-content="Top popover">  Popover on top</button><buttontype="button"class="btn btn-secondary"data-coreui-container="body"data-coreui-toggle="popover"data-coreui-placement="right"data-coreui-content="Right popover">  Popover on right</button><buttontype="button"class="btn btn-secondary"data-coreui-container="body"data-coreui-toggle="popover"data-coreui-placement="bottom"data-coreui-content="Bottom popover">  Popover on bottom</button><buttontype="button"class="btn btn-secondary"data-coreui-container="body"data-coreui-toggle="popover"data-coreui-placement="left"data-coreui-content="Left popover">  Popover on left</button>

Using thecontainer option

When you have some styles on a parent element that interfere with a popover, you’ll want to specify a customcontainer so that the popover’s HTML appears within that element instead.

constpopover=newcoreui.Popover(document.querySelector('.example-popover'),{container:'body'})

Custom popovers

Added in v4.2.6

You can customize the appearance of popovers usingCSS variables. We set a custom class withdata-coreui-custom-class="custom-popover" to scope our custom appearance and use it to override some of the local CSS variables.

docs/assets/scss/_component-examples.scss
.custom-popover{--cui-popover-max-width:200px;--cui-popover-border-color:var(--cui-primary);--cui-popover-header-bg:var(--cui-primary);--cui-popover-header-color:var(--cui-white);--cui-popover-body-padding-x:1rem;--cui-popover-body-padding-y:.5rem;}
html
<buttontype="button"class="btn btn-secondary"data-coreui-toggle="popover"data-coreui-placement="right"data-coreui-custom-class="custom-popover"data-coreui-title="Custom popover"data-coreui-content="This popover is themed via CSS variables.">  Custom popover</button>

Dismiss on next click

Use thefocus trigger to dismiss popovers on the user’s next click of a different element than the toggle element.

Specific markup required for dismiss-on-next-click

For proper cross-browser and cross-platform behavior, you must use the<a> tag,not the<button> tag, and you also must include atabindex attribute.

html
<atabindex="0"class="btn btn-lg btn-danger"role="button"data-coreui-toggle="popover"data-coreui-trigger="focus"title="Dismissible popover"data-coreui-content="And here's some amazing content. It's very engaging. Right?">Dismissible popover</a>
constpopover=newcoreui.Popover(document.querySelector('.popover-dismiss'),{trigger:'focus'})

Disabled elements

Elements with thedisabled attribute aren’t interactive, meaning users cannot hover or click them to trigger a popover (or tooltip). As a workaround, you’ll want to trigger the popover from a wrapper<div> or<span>, ideally made keyboard-focusable usingtabindex="0".

For disabled popover triggers, you may also preferdata-coreui-trigger="hover focus" so that the popover appears as immediate visual feedback to your users as they may not expect toclick on a disabled element.

html
<spanclass="d-inline-block"tabindex="0"data-coreui-toggle="popover"data-coreui-trigger="hover focus"data-coreui-content="Disabled popover"><buttonclass="btn btn-primary"type="button"disabled>Disabled button</button></span>

Usage

Heads up! In our documentation, all examples showstandard CoreUI implementation. If you are using aBootstrap-compatible version of CoreUI, remember to use the following changes:

  • In the constructor, please usebootstrap instead ofcoreui. For example,new bootstrap.Alert(...) instead ofnew coreui.Alert(...)
  • In events, please usebs instead ofcoreui, for exampleclose.bs.alert instead ofclose.coreui.alert
  • In data attributes, please usebs instead ofcoreui. For example,data-bs-toggle="..." instead ofdata-coreui-toggle="..."

Enable popovers via #"0">constexampleEl=document.getElementById('example')constpopover=newcoreui.Popover(exampleEl,options)

Making popovers work for keyboard and assistive technology users

To allow keyboard users to activate your popovers, you should only add them to HTML elements that are traditionally keyboard-focusable and interactive (such as links or form controls). Although arbitrary HTML elements (such as<span>s) can be made focusable by adding thetabindex="0" attribute, this will add potentially annoying and confusing tab stops on non-interactive elements for keyboard users, and most assistive technologies currently do not announce the popover’s content in this situation. Additionally, do not rely solely onhover as the trigger for your popovers, as this will make them impossible to trigger for keyboard users.

While you can insert rich, structured HTML in popovers with thehtml option, we strongly recommend that you avoid adding an excessive amount of content. The way popovers currently work is that, once displayed, their content is tied to the trigger element with thearia-describedby attribute. As a result, the entirety of the popover’s content will be announced to assistive technology users as one long, uninterrupted stream.

Additionally, while it is possible to also include interactive controls (such as form elements or links) in your popover (by adding these elements to theallowList of allowed attributes and tags), be aware that currently the popover does not manage keyboard focus order. When a keyboard user opens a popover, focus remains on the triggering element, and as the popover usually does not immediately follow the trigger in the document’s structure, there is no guarantee that moving forward/pressingTAB will move a keyboard user into the popover itself. In short, simply adding interactive controls to a popover is likely to make these controls unreachable/unusable for keyboard users and users of assistive technologies, or at the very least make for an illogical overall focus order. In these cases, consider using a modal dialog instead.

Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name todata-coreui-, as indata-coreui-animation="". Make sure to change the case type of the option name from camelCase to kebab-case when passing via data attributes. For example: instead of usingdata-coreui-customClass="beautifier", usedata-coreui-custom-class="beautifier".

Note that for security reasons thesanitize,sanitizeFn, andallowList options cannot be supplied using data attributes.
NameTypeDefaultDescription
allowListobjectDefault valueObject which contains allowed attributes and tags.
animationbooleantrueApply a CSS fade transition to the popover
boundarystring, element'clippingParents'Overflow constraint boundary of the popover (applies only to Popper’s preventOverflow modifier). By default, it’s'clippingParents' and can accept an HTMLElement reference (via JavaScript only). For more information refer to Popper’sdetectOverflow docs.
containerstring, element, falsefalseAppends the popover to a specific element. Example:container: 'body'. This option is particularly useful in that it allows you to position the popover in the flow of the document near the triggering element - which will prevent the popover from floating away from the triggering element during a window resize.
contentstring, element, function''Default content value ifdata-coreui-content attribute isn’t present. If a function is given, it will be called with itsthis reference set to the element that the popover is attached to.
customClassstring, function''Add classes to the popover when it is shown. Note that these classes will be added in addition to any classes specified in the template. To add multiple classes, separate them with spaces:'class-1 class-2'. You can also pass a function that should return a single string containing additional class names.
delaynumber, object0Delay showing and hiding the popover (ms)—doesn’t apply to manual trigger type. If a number is supplied, delay is applied to both hide/show. Object structure is:delay: { "show": 500, "hide": 100 }.
fallbackPlacementsstring, array['top', 'right', 'bottom', 'left']Define fallback placements by providing a list of placements in array (in order of preference). For more information refer to Popper’sbehavior docs.
htmlbooleanfalseAllow HTML in the popover. If true, HTML tags in the popover’stitle will be rendered in the popover. If false,innerText property will be used to insert content into the DOM. Use text if you’re worried about XSS attacks.
offsetnumber, string, function[0, 8]Offset of the popover relative to its target. You can pass a string in data attributes with comma separated values like:data-coreui-offset="10,20". When a function is used to determine the offset, it is called with an object containing the popper placement, the reference, and popper rects as its first argument. The triggering element DOM node is passed as the second argument. The function must return an array with two numbers:skidding,distance. For more information refer to Popper’soffset docs.
placementstring, function'right'How to position the popover: auto, top, bottom, left, right. Whenauto is specified, it will dynamically reorient the popover. When a function is used to determine the placement, it is called with the popover DOM node as its first argument and the triggering element DOM node as its second. Thethis context is set to the popover instance.
popperConfignull, object, functionnullTo change CoreUI for Bootstrap’s default Popper config, seePopper’s configuration. When a function is used to create the Popper configuration, it’s called with an object that contains the CoreUI for Bootstrap’s default Popper configuration. It helps you use and merge the default with your own configuration. The function must return a configuration object for Popper.
sanitizebooleantrueEnable or disable the sanitization. If activated'template','content' and'title' options will be sanitized.
sanitizeFnnull, functionnullHere you can supply your own sanitize function. This can be useful if you prefer to use a dedicated library to perform sanitization.
selectorstring, falsefalseIf a selector is provided, popover objects will be delegated to the specified targets. In practice, this is used to also apply popovers to dynamically added DOM elements (jQuery.on support). Seethis issue andan informative example.Note:title attribute must not be used as a selector.
templatestring'<div class="popover" role="popover"><div class="popover-arrow"></div><div class="popover-body"></div></div>'Base HTML to use when creating the popover. The popover’stitle will be injected into the.popover-body..popover-arrow will become the popover’s arrow. The outermost wrapper element should have the.popover class androle="popover".
titlestring, element, function''Default title value iftitle attribute isn’t present. If a function is given, it will be called with itsthis reference set to the element that the popover is attached to.
triggerstring'click'How popover is triggered: click, hover, focus, manual. You may pass multiple triggers; separate them with a space.'manual' indicates that the popover will be triggered programmatically via the.popover('show'),.popover('hide') and.popover('toggle') methods; this value cannot be combined with any other trigger.'hover' on its own will result in popovers that cannot be triggered via the keyboard, and should only be used if alternative methods for conveying the same information for keyboard users is present.

Data attributes for individual popovers

Options for individual popovers can alternatively be specified through the use of data attributes, as explained above.

Using function withpopperConfig

constpopover=newcoreui.Popover(element,{popperConfig(defaultBsPopperConfig){// const newPopperConfig = {...}// use defaultBsPopperConfig if needed...// return newPopperConfig}})

Methods

Asynchronous methods and transitions

All our API methods areasynchronous and initiate atransition. They return to the caller as soon as the transition begins butbefore it concludes. Furthermore, a method call on atransitioning component will be ignored.

Refer to our JavaScript documentation for further details.

MethodDescription
disableRemoves the ability for an element’s popover to be shown. The popover will only be able to be shown if it is re-enabled.
disposeHides and destroys an element’s popover (Removes stored data on the DOM element). Popovers that use delegation (which are created usingtheselector option) cannot be individually destroyed on descendant trigger elements.
enableGives an element’s popover the ability to be shown.Popovers are enabled by default.
getInstanceStatic method which allows you to get the popover instance associated with a DOM element.
getOrCreateInstanceStatic method which allows you to get the popover instance associated with a DOM element, or create a new one in case it wasn’t initialized
hideHides an element’s popover.Returns to the caller before the popover has actually been hidden (i.e. before thehidden.coreui.popover event occurs). This is considered a “manual” triggering of the popover.
setContentGives a way to change the popover’s content after its initialization.
showReveals an element’s popover.Returns to the caller before the popover has actually been shown (i.e. before theshown.coreui.popover event occurs). This is considered a “manual” triggering of the popover. Popovers whose title and content are both zero-length are never displayed.
toggleToggles an element’s popover.Returns to the caller before the popover has actually been shown or hidden (i.e. before theshown.coreui.popover orhidden.coreui.popover event occurs). This is considered a “manual” triggering of the popover.
toggleEnabledToggles the ability for an element’s popover to be shown or hidden.
updateUpdates the position of an element’s popover.
// getOrCreateInstance exampleconstpopover=coreui.Popover.getOrCreateInstance('#example')// Returns a Bootstrap popover instance// setContent examplemyPopover.setContent({'.popover-header':'another title','.popover-body':'another content'})
ThesetContent method accepts anobject argument, where each property-key is a validstring selector within the popover template, and each related property-value can bestring |element |function |null

Events

EventDescription
show.coreui.popoverThis event fires immediately when theshow instance method is called.
shown.coreui.popoverThis event is fired when the popover has been made visible to the user (will wait for CSS transitions to complete).
hide.coreui.popoverThis event is fired immediately when thehide instance method has been called.
hidden.coreui.popoverThis event is fired when the popover has finished being hidden from the user (will wait for CSS transitions to complete).
inserted.coreui.popoverThis event is fired after theshow.coreui.popover event when the popover template has been added to the DOM.
constmyPopoverTrigger=document.getElementById('myPopover')myPopoverTrigger.addEventListener('hidden.coreui.popover',()=>{// do something...})

Customizing

CSS variables

Popovers use local CSS variables on.popover for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.

scss/_popover.scss
--cui-popover-zindex:#{$zindex-popover};--cui-popover-max-width:#{$popover-max-width};@include rfs($popover-font-size,--cui-popover-font-size);--cui-popover-bg:#{$popover-bg};--cui-popover-border-width:#{$popover-border-width};--cui-popover-border-color:#{$popover-border-color};--cui-popover-border-radius:#{$popover-border-radius};--cui-popover-inner-border-radius:#{$popover-inner-border-radius};--cui-popover-box-shadow:#{$popover-box-shadow};--cui-popover-header-padding-x:#{$popover-header-padding-x};--cui-popover-header-padding-y:#{$popover-header-padding-y};@include rfs($popover-header-font-size,--cui-popover-header-font-size);--cui-popover-header-color:#{$popover-header-color};--cui-popover-header-bg:#{$popover-header-bg};--cui-popover-body-padding-x:#{$popover-body-padding-x};--cui-popover-body-padding-y:#{$popover-body-padding-y};--cui-popover-body-color:#{$popover-body-color};--cui-popover-arrow-width:#{$popover-arrow-width};--cui-popover-arrow-height:#{$popover-arrow-height};--cui-popover-arrow-border:var(--cui-popover-border-color);

SASS variables

scss/_variables.scss
$popover-font-size:$font-size-sm;$popover-bg:var(--#{$prefix}body-bg);$popover-max-width:276px;$popover-border-width:var(--#{$prefix}border-width);$popover-border-color:var(--#{$prefix}border-color-translucent);$popover-border-radius:var(--#{$prefix}border-radius-lg);$popover-inner-border-radius:calc(#{$popover-border-radius}-#{$popover-border-width});// stylelint-disable-line function-disallowed-list$popover-box-shadow:var(--#{$prefix}box-shadow);$popover-header-font-size:$font-size-base;$popover-header-bg:var(--#{$prefix}secondary-bg);$popover-header-color:$headings-color;$popover-header-padding-y:.5rem;$popover-header-padding-x:$spacer;$popover-body-color:var(--#{$prefix}body-color);$popover-body-padding-y:$spacer;$popover-body-padding-x:$spacer;$popover-arrow-width:1rem;$popover-arrow-height:.5rem;

CoreUI vs Bootstrap

While this Popover component is fully compatible with Bootstrap and follows its core principles, CoreUI delivers a more complete solution for modern app development.

What sets CoreUI apart from Bootstrap?

  • Fully compatible with Bootstrap – Built directly on Bootstrap, all classes and behaviors work as expected.
  • 🧠Framework-native versions – CoreUI provides dedicated libraries forReact.js,Vue.js, andAngular, unlike Bootstrap which relies on third-party plugins for JavaScript frameworks.
  • 👨‍💻Maintained by a full-time team – CoreUI is developed as a professional product, not a volunteer-driven project.
  • 📦More built-in components – Includes additional ready-to-use components like range sliders, multi-selects, steppers, etc.
  • 🛠️Sass Modules support today – CoreUI already supports Sass Modules, which are planned for Bootstrap 6.
  • 🌍Better LTR/RTL support – Uses modern CSS logical properties for seamless bidirectional layout support.
  • 🔒LTS (Long-Term Support) – Bootstrap now offers LTS only via paid third parties like HeroDevs, while CoreUI continues to offer long-term support natively and for free.

Whether you’re building internal tools, dashboards, or SaaS platforms — CoreUI combines the familiarity of Bootstrap with a more powerful, scalable, and production-ready ecosystem.

👉Explore CoreUI Bootstrap Components
👉Compare CoreUI vs Bootstrap


[8]ページ先頭

©2009-2025 Movatter.jp