Skip to content

Using animation with CSS isolation requires special treatment #35050

@mayur-ekbote

Description

@mayur-ekbote

Description

If you have an animation defined using keyframes then it will not work if it is specified as inline style.

Assume that the following styles are defined in an isolated css. Index.razor.css

.isolation-animated {
    width: 100px;
    height: 100px;
    background: blue;
    position: relative;
}

@keyframes isolation-keyframes {
    from {
        top: 0px;
    }

    to {
        top: 200px;
    }
}

And the razor component is:

<div class="isolation-animated" style="animation: isolation-keyframes 5s infinite;"></div>

Then this animation will not work. This is because the isolated keyframe gets a suffix like the following during the build process.

@keyframes isolation-keyframes-b-ir289k6iqg {
    from {
        top: 0px;
    }

    to {
        top: 200px;
    }
}

This peculiar situation arises because unlike css classes, keyframe names can be used on the right hand side of the css value defined in style. There are two solutions to this

  1. Use a custom css scope

<None Update="Components/Pages/Index.razor.css" CssScope="custom-scope-identifier" />

So the razor becomes:

<div class="isolation-animated" style="animation: isolation-keyframes-custom-scope-identifier 5s infinite;"></div>

  1. Define keyframes globally.

I think adding this to the documentation will be useful for the devs who are learning about css isolation in blazor.

See the git repro for the original issue.

Page URL

https://learn.microsoft.com/en-us/aspnet/core/blazor/components/css-isolation?view=aspnetcore-9.0

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/components/css-isolation.md

Document ID

39d464d9-aca6-34fa-7e52-5fde2e393ba7

Platform Id

518ba73d-7d61-b839-0b97-f417b8aa24a3

Article author

@guardrex

Metadata

  • ID: 2703a48e-8b35-53fc-ac36-cfb395833077
  • PlatformId: 518ba73d-7d61-b839-0b97-f417b8aa24a3
  • Service: aspnet-core
  • Sub-service: blazor

Related Issues

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions