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

    EXAMPLE
    MODULES
    RAZOR
    CSS

    Usage

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

    // in Program.cs file
    
    builder.Services.AddIgniteUIBlazor(typeof(IgbButtonModule));
    razor

    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" />
    razor
    <IgbButton />
    razor

    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.

    <IgbButton Variant="@ButtonVariant.Contained">
        <span slot="prefix">+</span>Click me<span slot="suffix">-</span>
    </IgbButton>
    razor

    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" />
    razor

    EXAMPLE
    MODULES
    RAZOR
    CSS

    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" />
    razor

    EXAMPLE
    MODULES
    RAZOR
    CSS

    Flat Button

    Analogically, we can switch to flat variant.

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

    EXAMPLE
    MODULES
    RAZOR
    CSS

    Floating Action Button

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

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

    EXAMPLE
    MODULES
    RAZOR
    CSS

    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;
        }
    }
    razor

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

    EXAMPLE
    MODULES
    RAZOR
    CSS

    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://ko.infragistics.com/" Target="@ButtonBaseTarget._blank">
        Download
    </IgbButton>
    razor

    EXAMPLE
    MODULES
    RAZOR
    CSS

    Styling

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

    Name Description
    base The native button element of the igc-button component.
    prefix The prefix container of the igc-button component.
    suffix The 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;
    }
    css

    EXAMPLE

    API References

    Additional Resources