CSS Custom Properties

jengfad

Jennifer Fadriquela

Posted on May 29, 2021

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
💖 💪 🙅 🚩
jengfad
Jennifer Fadriquela

Posted on May 29, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related