CssClass Attribute

Define CssClassAttribute for component class or parameter

For Component

The class string always applied when component is initialized.

Alert.razor
@inherits BlazorComponentBase
@attribute [CssClass("alert")]

<div @attributes="AdditionalAttributes">
    @ChildContent
</div>

@code{    
    [Parameter]public RenderFragment? ChildContent { get; set; }
}
[CssClass("alert")]
public class Alert : BlazorComponentBase, IHasChildContent
{
    [Parameter]public RenderFragment? ChildContent { get; set; }
}
Index.razor
@page "/"
<Alert>Alert</Alert>

<div class="alert">Alert</div>

For Parameter

The CSS value can apply when parameter has value

  • For bool type

Only true value to apply the css string

  • For Enum type

The value of parameter could be appended behind the css string with enum member

  • For other value types

The parameter value is appended to the css value

NOTE: The value should be Nullable because default value of int type is 0

Even though you didn't assign a value to the parameter, you still have a default value

Pre-Definition

Define CssClassAttribute in interface to reuse in component

  • Override pre-definition

Order

Set Order property in CssClassAttribute to sort css string

Disabled

Set Disabled=true to disable css string apply when parameter set

Concat

Set Concat=true to concat CssClassAttribute value from derived class

Last updated