Skip to content

Toggler Button Demos

Four Bootstrap 6 Toggler demos on a dark panel: a favorite heart, a read-more expander, a light/dark theme preview and a grid/list view switch, all with data attributes.

Also known as: toggle button, favorite heart button, read more expander, grid list view switch, theme preview toggle

Toggler Button Demos live demo

Open in new tab

Bootstrap 6 code (HTML, CSS, JS)

Four small demos of the new Bootstrap 6 Toggler component on a dark panel: a favorite heart button, a "Read more" text expander, a light/dark theme preview and a grid/list layout switch. Each one flips a class or an attribute through data attributes only, so it is a handy reference for the Toggler API and a set of patterns to copy into cards, lists and profile widgets.

How it works

Every trigger has data-bs-toggle="toggler". The Bootstrap 6 bundle handles the click and toggles the value set in data-bs-attribute (default class) and data-bs-value:

  • 01 Favorite: the .tgd-fav button toggles tgd-fav--on on itself. CSS swaps the outline heart .tgd-fav-off for the filled .tgd-fav-on and plays a small pop animation.
  • 02 Expand: the button has data-bs-target="#tgd-story", so Toggler reads its config from the paragraph. tgd-story--open removes the two-line clamp.
  • 03 Theme: #tgd-preview uses data-bs-attribute="data-bs-theme" and data-bs-value="light". The first click sets the attribute and the card switches to Bootstrap's light tokens (--bs-bg-body, --bs-fg-body). The second click removes it, so the card inherits dark mode from .tgd-root again.
  • 04 Layout: tgd-files--list on #tgd-files turns the four-column file grid into a compact list.

Toggler does not touch ARIA, so script.js listens to the bubbling toggled.bs.toggler event on .tgd-root. It finds the triggers of the toggled element and syncs aria-pressed or aria-expanded. It swaps button text from data-tgd-on and data-tgd-off on .tgd-label, and updates the .tgd-fav-count save counter.

Customization

  • --tgd-highlight on .tgd-root: changes the lime color of the demo numbers and code chips.
  • --tgd-bg, --tgd-card and --tgd-line on .tgd-root: adjust the page, card and border colors of the dark panel.
  • data-bs-value on any trigger or target: set it to your own class name, for example is-active, and style that class.
  • data-bs-attribute on a target: toggle any attribute instead of a class, as the theme demo does with data-bs-theme.
  • data-tgd-on / data-tgd-off on a .tgd-label: the button text for the on and off states.
  • -webkit-line-clamp in .tgd-story: how many lines the collapsed text shows (2 by default).
  • Remove tgd-fav--on from the heart button and set aria-pressed="false" to start unsaved.

Use cases

  • Product and content cards: a heart toggle marks items as favorites
  • Articles and descriptions: a read-more expander shows or hides extra text
  • Settings and theme pickers: a light and dark preview switch needs only data attributes
  • Catalog and gallery pages: a grid and list view switch changes how items are laid out

Accessibility

All triggers are native <button> elements, so they work with Enter and Space. State toggles expose aria-pressed, and the expander uses aria-expanded with aria-controls. The heart button's aria-label switches between "Save to favorites" and "Remove from favorites". Icons are aria-hidden. The pop animation, chevron rotation and theme fade are disabled under prefers-reduced-motion.

Bootstrap 6 notes

Toggler is new in Bootstrap 6. In Bootstrap 5 you needed custom JavaScript, or a Collapse workaround, for class toggles like these. Buttons use the new standalone variants (btn-outline theme-danger, btn-subtle theme-secondary, btn-text theme-accent), and badges use badge theme-success. Built on 6.0.0-alpha.1, so the API may still change. The IDs (#tgd-story, #tgd-preview, #tgd-files) must be unique if you use the demos more than once on a page.

Toggler Button Demos at a glance

Framework
Bootstrap 6
Code
HTML, CSS, JavaScript
JavaScript
Vanilla JavaScript, no jQuery
Dependencies
Bootstrap 6, Bootstrap Icons
Layout
Responsive, works on mobile, dark design
Category
Toggles & Switches
License
Free for personal and commercial use

Questions about this template

Can I use the Toggler Button Demos code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

Load Bootstrap 6 and Bootstrap Icons from a CDN (the links are under Requirements). Paste the HTML where the component should appear, add the CSS to your stylesheet and load the JavaScript before the closing </body> tag. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. The script is plain vanilla JavaScript, so it works without jQuery, React or Vue. You can still use the markup inside those frameworks.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

Add these to your page (already included in the download):

  • <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@6.0.0-alpha.1/dist/css/bootstrap.min.css">
  • <script type="module" src="https://cdn.jsdelivr.net/npm/bootstrap@6.0.0-alpha.1/dist/js/bootstrap.bundle.min.js"></script>
  • <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">

Built for Bootstrap 6.0.0-alpha.1. Bootstrap 6 ships as an ES module, so its script and this component's JavaScript load with type="module". Alpha releases can still change before the stable version.

Toggler Button Demos – free Bootstrap 6 toggle & switch template
Toggler Button Demos · free Bootstrap 6 toggle & switch template

More Toggles & Switches

iOS Style Toggle Switch – free Pure CSS toggle & switch template

iOS Style Toggle Switch

Pure CSS iOS style toggle switches built on real checkboxes, in a grouped settings list with three sizes, five colors and a disabled state.

Settings Toggles with Description – free Tailwind CSS toggle & switch template

Settings Toggles with Description

Tailwind CSS settings panel with accessible switches, label descriptions, a locked required option, restore defaults and an auto-save status message.

Day/Night Toggle – free Pure CSS toggle & switch template

Day/Night Toggle

Animated pure CSS day and night toggle switch where the sun turns into a cratered moon, clouds drift away and stars appear.

Settings Panel with Switches – free Bootstrap 6 toggle & switch template

Settings Panel with Switches

Bootstrap 6 notification settings panel with switches grouped in cards, master switches per group, a pause-all toggle, unsaved-change tracking and a simulated save.

Neumorphic Toggle Switch – free Pure CSS toggle & switch template

Neumorphic Toggle Switch

Pure CSS neumorphic toggle switches with inset shadow tracks, raised knobs and glowing indicators, built on accessible native checkboxes.

Toggle Switches with Labels – free Bootstrap 6 toggle & switch template

Toggle Switches with Labels

Dark Bootstrap 6 settings panel with .switch toggles in three sizes, labels with descriptions, disabled states and a simulated auto-save status.