Represent tags, filters, and selections with compact chips that take icons, avatars, a remove button, and a toggle state.
Introduction
Chips are compact elements that represent a discrete piece of information, such as a filter, a tag, or a selection. They share the states and style variants of buttons, take their sizes from the chip design tokens, and can hold an icon, an avatar, and a remove button. The optional Chip plugin adds toggling and dismissal.
Basic structure
Apply .chip to an element. The class sets the layout, typography, padding, radius, and interactive states; alone it renders a chip on the body colors, and a context class such as .default or .primary sets its color.
<span class="chip">Foundation class</span>
<span class="chip default">Default chip</span>
<span class="chip primary">Primary chip</span>Element compatibility
Chips are usually <span> elements, and the .chip class also applies to <div>, <button>, and <a> elements. Use a <button> or an <a> for a chip that responds to interaction.
Accessibility: Add role="button" to an <a> chip that triggers in-page functionality instead of navigating, so assistive technologies announce it as a button.
<span class="chip primary">Span</span>
<div class="chip primary">Div</div>
<button class="chip primary" type="button">Button</button>
<a class="chip primary" href="#">Link</a>Color variants
Add a context class to .chip to color it. A chip without a style modifier is solid.
<span class="chip default">Default</span>
<span class="chip alternate">Alternate</span>
<span class="chip primary">Primary</span>
<span class="chip secondary">Secondary</span>
<span class="chip neutral">Neutral</span>
<span class="chip danger">Danger</span>
<span class="chip success">Success</span>
<span class="chip warning">Warning</span>
<span class="chip info">Info</span>
<span class="chip black">Black</span>
<span class="chip white">White</span>Accessibility tip: Color alone shouldn't convey meaning as it's not perceived by assistive technology users. Ensure clarity through text with adequate contrast, ARIA attributes where needed, and consider using .visually-hidden for screen reader content.
Outline style
The .outline modifier renders a chip with a border and a transparent background in the colors of its context.
<span class="chip default outline">Default</span>
<span class="chip alternate outline">Alternate</span>
<span class="chip primary outline">Primary</span>
<span class="chip secondary outline">Secondary</span>
<span class="chip neutral outline">Neutral</span>
<span class="chip danger outline">Danger</span>
<span class="chip success outline">Success</span>
<span class="chip warning outline">Warning</span>
<span class="chip info outline">Info</span>
<span class="chip black outline">Black</span>
<span class="chip white outline">White</span>Some outline variants use a light foreground color and need a dark background to keep enough contrast.
Smooth style
The .smooth modifier renders a chip with a subdued background in the colors of its context, which suits less prominent tags and filters.
<span class="chip default smooth">Default</span>
<span class="chip alternate smooth">Alternate</span>
<span class="chip primary smooth">Primary</span>
<span class="chip secondary smooth">Secondary</span>
<span class="chip neutral smooth">Neutral</span>
<span class="chip danger smooth">Danger</span>
<span class="chip success smooth">Success</span>
<span class="chip warning smooth">Warning</span>
<span class="chip info smooth">Info</span>
<span class="chip black smooth">Black</span>
<span class="chip white smooth">White</span>Icons
Place an icon before or after the chip text. Both SVG icons and icon fonts work.
<span class="chip default">
<svg class="icon" aria-hidden="true"><use href="#info-circle-solid"></use></svg>
<span>Default</span>
</span>
<span class="chip primary">
<svg class="icon" aria-hidden="true"><use href="#info-circle-solid"></use></svg>
<span>Primary</span>
</span>
<span class="chip default">
<span>Default</span>
<span class="icon cx-info-circle-solid" aria-hidden="true"></span>
</span>
<span class="chip primary">
<span>Primary</span>
<span class="icon cx-info-circle-solid" aria-hidden="true"></span>
</span>Icon spacing relies on the :first-child and :last-child pseudo-classes. Wrap the chip text in a <span> element so the icon is matched as the first or last child.
Dismissible chips
Add a .close-button inside a chip as its remove button. It takes the foreground color of the chip, and with data-cx-dismiss="chip" the Chip plugin removes the chip on click. Give the button an aria-label, since it has no text.
<span class="chip default">
Default
<button type="button" class="close-button" data-cx-dismiss="chip" aria-label="Remove"></button>
</span>
<span class="chip primary">
Primary
<button type="button" class="close-button" data-cx-dismiss="chip" aria-label="Remove"></button>
</span>
Size variants
Chips come in three sizes. Add .lg or .sm for the large or small size; the medium size applies without a modifier.
<span class="chip default">
Medium
</span>
<span class="chip default">
<svg class="icon" aria-hidden="true"><use href="#info-circle-solid"></use></svg>
<span>Medium</span>
</span>
<span class="chip default lg">
Large
</span>
<span class="chip default lg">
<svg class="icon" aria-hidden="true"><use href="#info-circle-solid"></use></svg>
<span>Large</span>
</span>
<span class="chip default sm">
Small
</span>
<span class="chip default sm">
<svg class="icon" aria-hidden="true"><use href="#info-circle-solid"></use></svg>
<span>Small</span>
</span>Avatars
Place an .avatar at the start of a chip to show a user picture. The chip sets the avatar size for each of its sizes and pulls the avatar toward its edge.
<span class="chip default">
<span class="avatar">
<img class="avatar-image" src="https://i.pravatar.cc/256" alt="Profile picture" />
</span>
Medium
<button type="button" class="close-button" aria-label="Remove"></button>
</span>
<span class="chip lg default">
<span class="avatar">
<img class="avatar-image" src="https://i.pravatar.cc/256" alt="Profile picture" />
</span>
Large
<button type="button" class="close-button" aria-label="Remove"></button>
</span>
<span class="chip sm default">
<span class="avatar">
<img class="avatar-image" src="https://i.pravatar.cc/256" alt="Profile picture" />
</span>
Small
<button type="button" class="close-button" aria-label="Remove"></button>
</span>Disabled chips
Add the disabled attribute to a <button> chip, or the .disabled class to any other element, to disable it. A disabled chip takes the disabled colors and has pointer-events: none, so its hover and press states do not trigger.
<span class="chip default disabled">
<span class="avatar">
<img class="avatar-image" src="https://i.pravatar.cc/256" alt="Profile picture" />
</span>
Medium
<button type="button" class="close-button" aria-label="Remove"></button>
</span>
<span class="chip lg default disabled">
<span class="avatar">
<img class="avatar-image" src="https://i.pravatar.cc/256" alt="Profile picture" />
</span>
Large
<button type="button" class="close-button" aria-label="Remove"></button>
</span>
<span class="chip sm default disabled">
<span class="avatar">
<img class="avatar-image" src="https://i.pravatar.cc/256" alt="Profile picture" />
</span>
Small
<button type="button" class="close-button" aria-label="Remove"></button>
</span>Toggle functionality
Add data-cx-toggle="chip" to make a chip a selection control. Each click toggles the .active class and keeps aria-pressed in sync. For a chip that starts selected, add both the .active class and aria-pressed="true".
<!-- Buttons -->
<p class="d-inline-flex gap-xs">
<button type="button" class="chip default" data-cx-toggle="chip" aria-pressed="false">Toggle button</button>
<button type="button" class="chip default active" data-cx-toggle="chip" aria-pressed="true">Active toggle</button>
<button type="button" class="chip default" disabled data-cx-toggle="chip" aria-pressed="false">Disabled toggle</button>
<button type="button" class="chip default active" disabled data-cx-toggle="chip" aria-pressed="true">Disabled active</button>
</p>
<!-- Links -->
<p class="d-inline-flex gap-xs">
<a href="#" class="chip primary" role="button" data-cx-toggle="chip" aria-pressed="false">Toggle link</a>
<a href="#" class="chip primary active" role="button" data-cx-toggle="chip" aria-pressed="true">Active toggle</a>
<a href="#" class="chip primary disabled" aria-disabled="true" role="button" data-cx-toggle="chip" aria-pressed="false">Disabled toggle</a>
<a href="#" class="chip primary active disabled" aria-disabled="true" role="button" data-cx-toggle="chip" aria-pressed="true">Disabled active</a>
</p>JavaScript API
The Chip plugin toggles and dismisses chips, and fires events for both. Chassis JS ships as an ES module — import the Chip class:
import { Chip } from '@chassis-ui/css'Triggers
The data-attribute API needs no JavaScript beyond the plugin itself. A click on a chip with data-cx-toggle="chip" toggles it, as shown in Toggle functionality, and data-cx-dismiss="chip" on a button removes the chip.
Enable dismissal with the data-cx-dismiss attribute on a button within the chip:
<button type="button" class="close-button" data-cx-dismiss="chip" aria-label="Dismiss"></button>Or on a button outside the chip using the additional data-cx-target attribute:
<button type="button" class="close-button" data-cx-dismiss="chip" data-cx-target="#my-chip" aria-label="Dismiss chip"></button>close.cx.chip event.
Initialization
For programmatic access — calling methods or listening to events — create an instance of each element with the Chip class:
const chips = [...document.querySelectorAll('.chip')]
.map(element => new Chip(element))Methods
The plugin exposes instance methods for toggling and dismissal, next to the methods every Chassis plugin has:
| Method | Description |
|---|---|
toggle | Toggles the .active class of the chip and sets aria-pressed to match. |
close | Removes the chip from the DOM and disposes the instance. |
dispose | Destroys the instance and removes its stored data from the element. |
getInstance | Static — returns the instance bound to a DOM element, or null: Chip.getInstance(element). |
getOrCreateInstance | Static — returns the instance bound to a DOM element, or creates one: Chip.getOrCreateInstance(element). |
getOrCreateInstance acts on a set of chips without tracking their instances:
// Deselect every selected chip
document.querySelectorAll('.chip.active').forEach(element => {
Chip.getOrCreateInstance(element).toggle()
})
// Remove every chip with a given tag
document.querySelectorAll('.chip[data-tag="outdated"]').forEach(element => {
Chip.getOrCreateInstance(element).close()
})Events
Both events fire on the .chip element and bubble:
| Event | Description |
|---|---|
toggle.cx.chip | Fires after the chip is toggled. The active property of the event holds the new state. |
close.cx.chip | Fires when the close method is called, before the chip is removed. Calling event.preventDefault() keeps the chip. |
const myChip = document.getElementById('myChip')
myChip.addEventListener('toggle.cx.chip', event => {
console.log(`Chip toggled to ${event.active ? 'active' : 'inactive'} state`)
})
// Move focus before the chip leaves the DOM
myChip.addEventListener('close.cx.chip', () => {
document.getElementById('chipContainer').focus()
})CSS
This component can be customized through Sass variables at compile time.
Custom properties
This component exposes CSS custom properties to control its appearance at runtime.
--border-width: var(--chip-border-width, #{$chip-border-width});
--box-shadow: var(--chip-box-shadow, #{$chip-box-shadow});
// Sizing
--min-size: var(--chip-min-size, #{$chip-medium-min-size});
--padding-y: var(--chip-padding-y, #{$chip-medium-padding-y});
--padding-x: var(--chip-padding-x, #{$chip-medium-padding-x + $chip-medium-gap});
--gap: var(--chip-gap, #{$chip-medium-gap});
--nudge: var(--chip-nudge, #{$chip-medium-nudge});
--border-radius: var(--chip-border-radius, #{$chip-medium-border-radius});
--close-button-icon: var(--chip-remove-icon, #{svg-icon($chip-remove-icon)});
--close-button-fg-color: var(--chip-remove-color, #{$chip-remove-color});
--close-button-fg-hover: var(--chip-remove-hover, #{$chip-remove-hover});
--close-button-idle-opacity: var(--chip-remove-idle-opacity, #{$chip-remove-idle-opacity});
--close-button-hover-opacity: var(--chip-remove-hover-opacity, #{$chip-remove-hover-opacity});
--icon-size: var(--chip-icons-size, #{$chip-medium-icon-size});
--avatar-size: var(--chip-avatar-size, #{$chip-medium-avatar-size});
--close-button-size: var(--icon-size);
--caret-spacing: var(--nudge) calc(-1 * var(--nudge));
@include map-font($chip-medium-font, chip);Chip variants use the same placeholder selectors and color map as buttons. See the CSS section of the button docs for both.
Sass variables
This component uses Sass variables in scss/config/_defaults.scss to define its defaults.
$chip-box-shadow: none; // $box-shadow-emboss; //
$chip-remove-color: var(--fg-color);
$chip-remove-hover: var(--fg-color);
$chip-remove-idle-opacity: .75;
$chip-remove-hover-opacity: 1;Design tokens
This component consumes design tokens from Chassis Tokens with the $cx- prefix. — See the design tokens page.
$chip-medium-font: $cx-font-chip-medium;
$chip-large-font: $cx-font-chip-large;
$chip-small-font: $cx-font-chip-small;
$chip-medium-min-size: $cx-size-chip-medium-main;
$chip-medium-padding-y: $cx-space-chip-medium-padding-y;
$chip-medium-padding-x: $cx-space-chip-medium-padding-x;
$chip-medium-gap: $cx-space-chip-medium-gap;
$chip-medium-nudge: $cx-space-chip-medium-nudge;
$chip-large-min-size: $cx-size-chip-large-main;
$chip-large-padding-y: $cx-space-chip-large-padding-y;
$chip-large-padding-x: $cx-space-chip-large-padding-x;
$chip-large-gap: $cx-space-chip-large-gap;
$chip-large-nudge: $cx-space-chip-large-nudge;
$chip-small-min-size: $cx-size-chip-small-main;
$chip-small-padding-y: $cx-space-chip-small-padding-y;
$chip-small-padding-x: $cx-space-chip-small-padding-x;
$chip-small-gap: $cx-space-chip-small-gap;
$chip-small-nudge: $cx-space-chip-small-nudge;
$chip-medium-icon-size: $cx-size-chip-medium-icon;
$chip-large-icon-size: $cx-size-chip-large-icon;
$chip-small-icon-size: $cx-size-chip-small-icon;
$chip-medium-avatar-size: $cx-size-chip-medium-avatar;
$chip-large-avatar-size: $cx-size-chip-large-avatar;
$chip-small-avatar-size: $cx-size-chip-small-avatar;
$chip-border-width: $cx-border-width-chip-main;
$chip-medium-border-radius: $cx-border-radius-chip-medium;
$chip-large-border-radius: $cx-border-radius-chip-large;
$chip-small-border-radius: $cx-border-radius-chip-small;
$chip-remove-icon: $cx-icon-chip-remove;