Articles in this section
Category / Section

How to Implement CSS Isolation in Blazor Dialog Component?

2 mins read

CSS isolation is a technique used in Blazor applications to scope CSS styles to a specific component, preventing styles from affecting other parts of the application. This article provides a guide on how to achieve CSS isolation when using the Blazor Dialog component in a Blazor application.

Wrapping the Dialog Component

To isolate the CSS for the Dialog component, you can wrap it within another HTML <div> element. This allows you to target the Dialog component specifically with your styles.

Here’s an example of how to wrap the Dialog component:

[Index.razor]

<div>
<div id="target">
    <div>
        <button class="e-btn" @onclick="@OnBtnClick">Open</button>
    </div>
    <SfDialog Target="#target" Width="300px" ShowCloseIcon="true" @bind-Visible="Visibility" AllowPrerender="true" IsModal="true">
        <DialogTemplates>
            <Header> Dialog </Header>
            <Content> This is a dialog with header </Content>
        </DialogTemplates>
    </SfDialog>
</div>
</div>

<style>
    #target {
        height: 500px;
    }
</style>

@code {
    private bool Visibility { get; set; } = true;
    private void OnBtnClick()
    {
        this.Visibility = true;
    }
}

Applying CSS Isolation

To apply CSS isolation, use the ::deep selector in your component-specific CSS file. This selector allows targeting styles within the shadow DOM of the component.

Here’s an example of how to use the ::deep selector:

[Index.razor.css]

::deep .e-dialog .e-dlg-header {
    text-align: center;
}

By using the ::deep selector, the styles for the Dialog’s header are applied only to the Dialog component within the #target element.

Note

It’s important to note that CSS selectors might not directly match the dialog elements within the Blazor component. This occurs due to the default rendering behavior of the dialog, which utilizes the body as its target.

Additional References

For more information on CSS isolation and how to work with child components, please refer to the following documentation:

By following the steps outlined in this article, CSS isolation can be effectively implemented in Blazor applications, ensuring that the Dialog component and its styles are encapsulated and do not affect other elements in your application.

Conclusion
We hope you enjoyed learning how to implement CSS isolation in Blazor Dialog component.
You can refer to our Blazor Dialog feature tour page to know about its other groundbreaking features, documentation, and how to quickly get started with configuration specifications. You can also explore our Blazor Dialog example to understand how to create and manipulate data.
For current customers, our Blazor components are available on the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to evaluate our Blazor Dialog and other Blazor components.
If you have any questions or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied