Close
Angular React Web Components Blazor Blazor
Open Source

Blazor Button Overview

The Blazor Button Component lets you enable clickable elements that trigger actions in your Blazor app. You get full control over how you set button variants, configure styles for the wrapped element, and define sizes. The Button Component also gives flexibility through the Blazor Button OnClick event, toggle the Blazor button, disable the Blazor button, and more.

Blazor Button Example

Usage

Before using the IgbButton, you need to register it as follows:

// in Program.cs file

builder.Services.AddIgniteUIBlazor(typeof(IgbButtonModule));

You will also need to link an additional CSS file to apply the styling to the IgbButton component. The following needs to be placed in the wwwroot/index.html file in a Blazor Web Assembly project or the Pages/_Host.cshtml file in a Blazor Server project:

<link href="_content/IgniteUI.Blazor/themes/light/bootstrap.css" rel="stylesheet" />

The simplest way to start using the IgbButton is as follows:

<IgbButton />

Prefix / Suffix

With prefix and suffix slots of the IgbButton component, we can add different content before and after the main content of the button.

We recommend using a <span> element when adding simple text, symbols, or emojis, and an <igc-icon> component when adding icons to the prefix and suffix slots.

<IgbButton Variant="@ButtonVariant.Contained">
    <span slot="prefix">Download</span>
    <IgbIcon slot="suffix" IconName="download"></IgbIcon>
</IgbButton>

Type

The button component will change its internal structure from a <button> to an <a> type element when the Href attribute is set. In that case the button can be thought of as a regular link. Setting the Href attribute will allow you to also set the Rel, Target and Download attributes. In the case when the button component uses an actual <button> element internally, we can specify its DisplayType by setting the property to any of the following values:

  • Submit - when we want to submit the form data
  • reset - when we want to reset form data to its initial values
  • button - when we want to add button with a custom functionality anywhere on a webpage

Button Variants

Contained Button

Use the Variant attribute to add a simple contained button in your component template. Note that if you do not set variant, by default it will be set to contained.

<IgbButton Variant="@ButtonVariant.Contained" />

Outlined Button

All you have to do to create an outlined button is to change the value of the Variant property:

<IgbButton Variant="@ButtonVariant.Outlined" />

Flat Button

Analogically, we can switch to flat variant.

<IgbButton Variant="@ButtonVariant.Flat" />

Floating Action Button

We can create a floating action button by setting the Variant property to fab:

<IgbButton Variant="@ButtonVariant.Fab" />

Button Sizing

Users can change the size of the IgbButton using the --ig-size CSS variable. In the following example, we will add some radio buttons to display all size values. This way whenever one gets selected, we will change the size of the button.

<IgbRadioGroup id="radioGroup" Alignment="ContentOrientation.Horizontal" >
    <IgbRadio Value="small" LabelPosition="RadioLabelPosition.After" @onclick="OnSmallClick">Small</IgbRadio>
    <IgbRadio Value="medium" LabelPosition="RadioLabelPosition.After" @onclick="OnMediumClick">Medium</IgbRadio>
    <IgbRadio Value="large" LabelPosition="RadioLabelPosition.After" Checked="true" @onclick="OnLargeClick">Large</IgbRadio>
</IgbRadioGroup>

@code {
    private SizableComponentSize SizableComponentSize = SizableComponentSize.Large;

    protected override void OnInitialized()
    {
    }

    public void OnSmallClick(EventArgs e)
    {
        SizableComponentSize = SizableComponentSize.Small;
    }

    public void OnMediumClick(EventArgs e)
    {
        SizableComponentSize = SizableComponentSize.Medium;
    }

    public void OnLargeClick(EventArgs e)
    {
        SizableComponentSize = SizableComponentSize.Large;
    }
}

The result of implementing the above code should look like the following:

Download

Setting the Download property will prompt the user to save the linked URL instead of navigating to it.

<IgbButton Variant="@ButtonVariant.Contained" Download="Url" Href="https://es.infragistics.com/" Target="@ButtonBaseTarget._blank">
    Download
</IgbButton>

Styling

The IgbButton exposes three CSS parts which we can use for styling:

NameDescription
baseThe native button element of the igc-button component.
prefixThe prefix container of the igc-button component.
suffixThe suffix container of the igc-button component.

The base CSS part allows us to style the wrapped element (<button> or <a>).

igc-button::part(base) {
  background-color: var(--ig-primary-500);
  color: var(--ig-primary-500-contrast);
  padding: 18px;
}

API References

Additional Resources