Angular Button Component

Button

Angular button directive for actions in tables, forms, cards, and more. CoreUI for Angular provides various styles, states, and size. Ready to use and easy to customize.

Available in Other JavaScript Frameworks

CoreUI Angular Button Component is also available for Bootstrap, React, and Vue. Explore framework-specific implementations below:

Examples

CoreUI includes a bunch of predefined buttons components, each serving its own semantic purpose. Buttons show what action will happen when the user clicks or touches it. CoreUI buttons are used to initialize operations, both in the background or foreground of an experience.

import { Component } from '@angular/core';
import { ButtonDirective } from '@coreui/angular';

@Component({
  selector: 'docs-button',
  templateUrl: './button.component.html',
  imports: [ButtonDirective]
})
export class ButtonComponent {}
<button cButton color="primary">Primary</button>
<button cButton color="secondary">Secondary</button>
<button cButton color="success">Success</button>
<button cButton color="danger">Danger</button>
<button cButton color="warning">Warning</button>
<button cButton color="info">Info</button>
<button cButton color="light">Light</button>
<button cButton color="dark">Dark</button>

<button cButton color="link">Link</button>
Conveying meaning to assistive technologies

Using color to add meaning only provides a visual indication, which will not be conveyed to users of assistive technologies – such as screen readers. Ensure that information denoted by the color is either obvious from the content itself (e.g. the visible text), or is included through alternative means, such as additional text hidden with the .visually-hidden class.

Disable text wrapping

If you don’t want the button text to wrap, you can add the .text-nowrap class to the <button>. In Sass, you can set $btn-white-space: nowrap to disable text wrapping for each button.

Button directive

The cButton directive are designed for <button> , <a> or <input> elements (though some browsers may apply a slightly different rendering).

If you’re using cButton directive as <a> elements that are used to trigger functionality ex. collapsing content, these links should be given a role="button" to adequately communicate their meaning to assistive technologies such as screen readers.

import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';
import { ButtonDirective } from '@coreui/angular';

@Component({
  selector: 'docs-button-directive',
  templateUrl: './button-directive.component.html',
  imports: [ButtonDirective, RouterLink]
})
export class ButtonDirectiveComponent {}
<a cButton color="primary" [routerLink] role="button">Link</a>
<button cButton color="primary" color="primary">Button</button>
<input cButton color="primary" type="button" value="Input"/>
<input cButton color="primary" type="submit" value="Submit"/>
<input cButton color="primary" type="reset" value="Reset"/>

Outline buttons

If you need a button, but without the strong background colors. Set variant="outline" prop to remove all background colors.

import { Component } from '@angular/core';
import { ButtonDirective } from '@coreui/angular';

@Component({
  selector: 'docs-button-outline-buttons',
  templateUrl: './button-outline-buttons.component.html',
  imports: [ButtonDirective]
})
export class ButtonOutlineButtonsComponent {}
<button cButton color="primary" variant="outline">Primary</button>
<button cButton color="secondary" variant="outline">Secondary</button>
<button cButton color="success" variant="outline">Success</button>
<button cButton color="danger" variant="outline">Danger</button>
<button cButton color="warning" variant="outline">Warning</button>
<button cButton color="info" variant="outline">Info</button>
<button cButton color="light" variant="outline">Light</button>
<button cButton color="dark" variant="outline">Dark</button>

Ghost buttons

If you need a ghost variant of button, set variant="ghost" prop to remove all background colors.

import { Component } from '@angular/core';
import { ButtonDirective } from '@coreui/angular';

@Component({
  selector: 'docs-button-ghost-buttons',
  templateUrl: './button-ghost-buttons.component.html',
  imports: [ButtonDirective]
})
export class ButtonGhostButtonsComponent {}
<button cButton color="primary" variant="ghost">Primary</button>
<button cButton color="secondary" variant="ghost">Secondary</button>
<button cButton color="success" variant="ghost">Success</button>
<button cButton color="danger" variant="ghost">Danger</button>
<button cButton color="warning" variant="ghost">Warning</button>
<button cButton color="info" variant="ghost">Info</button>
<button cButton color="light" variant="ghost">Light</button>
<button cButton color="dark" variant="ghost">Dark</button>

Sizes

Larger or smaller buttons? Add size="lg" or size="sm" for additional sizes.

import { Component } from '@angular/core';
import { ButtonDirective } from '@coreui/angular';

@Component({
  selector: 'docs-button-sizes',
  templateUrl: './button-sizes.component.html',
  imports: [ButtonDirective]
})
export class ButtonSizesComponent {}
<button cButton color="primary" size="lg">Large button</button>
<button cButton color="secondary" size="lg">Large button</button>
import { Component } from '@angular/core';
import { ButtonDirective } from '@coreui/angular';

@Component({
  selector: 'docs-button-sizes-2',
  templateUrl: './button-sizes-2.component.html',
  imports: [ButtonDirective]
})
export class ButtonSizes2Component {}
<button cButton color="primary" size="sm">Small button</button>
<button cButton color="secondary" size="sm">Small button</button>

Shapes

Pill buttons

import { Component } from '@angular/core';
import { ButtonDirective } from '@coreui/angular';

@Component({
  selector: 'docs-button-pill-buttons',
  templateUrl: './button-pill-buttons.component.html',
  imports: [ButtonDirective]
})
export class ButtonPillButtonsComponent {}
<button cButton color="primary" shape="rounded-pill">Primary</button>
<button cButton color="secondary" shape="rounded-pill">Secondary</button>
<button cButton color="success" shape="rounded-pill">Success</button>
<button cButton color="danger" shape="rounded-pill">Danger</button>
<button cButton color="warning" shape="rounded-pill">Warning</button>
<button cButton color="info" shape="rounded-pill">Info</button>
<button cButton color="light" shape="rounded-pill">Light</button>
<button cButton color="dark" shape="rounded-pill">Dark</button>

Square buttons

import { Component } from '@angular/core';
import { ButtonDirective } from '@coreui/angular';

@Component({
  selector: 'docs-button-square-buttons',
  templateUrl: './button-square-buttons.component.html',
  imports: [ButtonDirective]
})
export class ButtonSquareButtonsComponent {}
<button cButton color="primary" shape="rounded-0">Primary</button>
<button cButton color="secondary" shape="rounded-0">Secondary</button>
<button cButton color="success" shape="rounded-0">Success</button>
<button cButton color="danger" shape="rounded-0">Danger</button>
<button cButton color="warning" shape="rounded-0">Warning</button>
<button cButton color="info" shape="rounded-0">Info</button>
<button cButton color="light" shape="rounded-0">Light</button>
<button cButton color="dark" shape="rounded-0">Dark</button>

Disabled state

Add the disabled boolean prop to any <button> component to make buttons look inactive. Disabled button has pointer-events: none applied to, disabling hover and active states from triggering.

import { Component } from '@angular/core';
import { ButtonDirective } from '@coreui/angular';

@Component({
  selector: 'docs-button-disabled-state',
  templateUrl: './button-disabled-state.component.html',
  imports: [ButtonDirective]
})
export class ButtonDisabledStateComponent {}
<button cButton color="primary" size="lg" disabled>Primary button</button>
<button cButton color="secondary" size="lg" disabled>Button</button>

Disabled buttons using the <a> component act a little different:

<a>s don’t support the disabled attribute, so CoreUI has to add .disabled class to make buttons look inactive. CoreUI also has to add to the disabled button component aria-disabled="true" attribute to show the state of the component to assistive technologies.

import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';
import { ButtonDirective } from '@coreui/angular';

@Component({
  selector: 'docs-button-disabled-state-2',
  templateUrl: './button-disabled-state-2.component.html',
  imports: [ButtonDirective, RouterLink]
})
export class ButtonDisabledState2Component {}
<a cButton [routerLink] color="primary" role="button" size="lg" disabled>Primary link</a>
<a cButton [routerLink] color="secondary" size="lg" disabled>Link</a>
Link functionality caveat

The .disabled class uses pointer-events: none to try to disable the link functionality of <a>s, but that CSS property is not yet standardized. In addition, even in browsers that do support pointer-events: none, keyboard navigation remains unaffected, meaning that sighted keyboard users and users of assistive technologies will still be able to activate these links. So to be safe, in addition to aria-disabled=“true”, also include a tabindex=“-1” attribute on these links to prevent them from receiving keyboard focus, and use custom JavaScript to disable their functionality altogether.

Block buttons

Create buttons that span the full width of a parent—by using utilities.

import { Component } from '@angular/core';
import { ButtonDirective } from '@coreui/angular';

@Component({
  selector: 'docs-button-block-buttons',
  templateUrl: './button-block-buttons.component.html',
  imports: [ButtonDirective]
})
export class ButtonBlockButtonsComponent {}
<div class="d-grid gap-2">
  <button cButton color="primary">Button</button>
  <button cButton color="primary">Button</button>
</div>

Here we create a responsive variation, starting with vertically stacked buttons until the md breakpoint, where .d-md-block replaces the .d-grid class, thus nullifying the gap-2 utility. Resize your browser to see them change.

import { Component } from '@angular/core';
import { ButtonDirective } from '@coreui/angular';

@Component({
  selector: 'docs-button-block-buttons-2',
  templateUrl: './button-block-buttons-2.component.html',
  imports: [ButtonDirective]
})
export class ButtonBlockButtons2Component {}
<div class="d-grid gap-2 d-md-block">
  <button cButton color="primary">Button</button>
  <button cButton color="primary">Button</button>
</div>

You can adjust the width of your block buttons with grid column width classes. For example, for a half-width “block button”, use .col-6. Center it horizontally with .mx-auto, too.

import { Component } from '@angular/core';
import { ButtonDirective } from '@coreui/angular';

@Component({
  selector: 'docs-button-block-buttons-3',
  templateUrl: './button-block-buttons-3.component.html',
  imports: [ButtonDirective]
})
export class ButtonBlockButtons3Component {}
<div class="d-grid gap-2 col-6 mx-auto">
  <button cButton color="primary">Button</button>
  <button cButton color="primary">Button</button>
</div>

Additional utilities can be used to adjust the alignment of buttons when horizontal. Here we’ve taken our previous responsive example and added some flex utilities and a margin utility on the button to right align the buttons when they’re no longer stacked.

import { Component } from '@angular/core';
import { ButtonDirective } from '@coreui/angular';

@Component({
  selector: 'docs-button-block-buttons-4',
  templateUrl: './button-block-buttons-4.component.html',
  imports: [ButtonDirective]
})
export class ButtonBlockButtons4Component {}
<div class="d-grid gap-2 d-md-flex justify-content-md-end">
  <button cButton color="primary" class="me-md-2">Button</button>
  <button cButton color="primary">Button</button>
</div>

API

Button Module

import { ButtonModule } from '@coreui/angular';

@NgModule({
  imports: [ButtonModule,]
})
export class AppModule(){}

cButton

directive

jsx
import { ButtonDirective } from '@coreui/angular'
PropertyDefaultType
activefalseboolean

Toggle the active state for the component

color'primary'string

Sets the color context of the component to one of CoreUI’s themed colors

disabledfalseboolean

Toggle the disabled state for the component.

shape-string

Select the shape of the component.

size'''', 'sm', 'lg'

Size the component small or large.

tabindexundefinednumber

The tabindex attribute specifies the tab order of an element (when the "tab" button is used for navigating).

type'button'ButtonType

Specifies the type of button. Always specify the type attribute for the <button> element. Different browsers may use different default types for the <button> element.

variant-'ghost', 'outline'

Set the button variant to an outlined button or a ghost button.