Search code examples
c#cssasp.netfont-face

Setting @Font-Face From C# Code Behind


In my stylesheet I have this declared

@font-face {
    font-family: 'light';
    src: url('Fonts/HelveticaNeueLTStd-Lt.otf') format('opentype');
}

Now in my C# code behind I try to set the class like this

row.CssClass = light;

But I get a compile error of

The name 'light' does not exist in the current context

Is it possible to reference a css class @font-face from C# codebehind?

EDIT
This is how I am attempting to use this - I have tried the below

.lightfontface {
    font-family: 'light';
}


@font-face {
    font-family: 'light';
    src: url('Fonts/HelveticaNeueLTStd-Lt.otf') format('opentype');
}

row.CssClass = lightfontface;

Solution

  • I believe your

    row.CssClass = light;
    

    needs to be

    row.CssClass = "className";
    

    and your css will need a .className entry.

    Imagine your html row:

    <tr class="className">stuff</tr>
    

    Your CssClass assignment is assigning the value to class, and your css can use a class selector to format that row.

    To sum up some of the comments, the style sheet entry should simply be:

    .className { font-family: 'light'; }
    

    Ordering on your style sheet is important. Put the font-face definition above the .className style entry. See: https://www.w3.org/TR/CSS2/cascade.html#cascading-order