DEV Community

Jennifer Fadriquela
Jennifer Fadriquela

Posted on

CSS Custom Properties

I've been working on a Razor project and was finding a way to dynamically assign style values to ::before elements. Best way to do this is via CSS Custom Properties

Here's what I came up with Razor code:

css

.wrapper::before {
  background-color: var(--label-color);
  content: var(--label-text);
}
Enter fullscreen mode Exit fullscreen mode

razor

@{
  var labelText = "Some Label";
  var labelColor = "blue";
}
<div class="wrapper" style="--label-color: @labelColor; --label-text: '@labelText'">
  Hello
</div>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)