268

I've got a large site that runs in ASP.NET MVC using the Razor view engine.

I have a base stylesheet which contains all of the generic styling for the whole site. On occasion, however, I have page specific styles which in the <head> of the page - usually this is one or 2 lines.

I don't particularly like putting the CSS in <head> as its not strictly separation of concerns, but for one or two lines, that really is specific to that page, I prefer not have to attach another file and add to the bandwidth.

I've got an instance though where I would like to put a page specific media query into the <head>, but because a media query uses the @ symbol and brackets {} it's clashing with the razor syntax:

@section cphPageHead{
     <style>
        /* PAGE SPECIFIC CSS */
        ...

        @media only screen and (max-width : 960px) <-- the @ symbol here is clashing!
            {
               ... }
            } 
    </style>
}  

Is there a way I can get around this?

5
  • 3
    I still think that, css styles should be in the CSS file, especially for a "Large site" Linear css on the page is not the best practice. PS: My opinion Commented Aug 11, 2011 at 14:22
  • 2
    I agree with @AlexC, but for those curious about a valid use case, critical CSS loads faster inline than externally. It's a pretty handy trick for those sites that rely on a super fast first meaningful paint. Commented May 11, 2018 at 15:07
  • 8
    Another use case is rendering emails Commented Oct 25, 2018 at 11:46
  • For people who using a code analyzer tool like sonar double @ can be marked as a major bug by tool. If you have a chance to change or disable the rule it is ok otherwise you have to find another way to escape @. Commented Oct 24, 2020 at 18:46
  • 1
    When using @media with grid you may want the style sheet in the page because each page layout may be different, you just want to control THAT specific page and packing away the css into a file is over-engineering and just doesn't make sense. Keep code that goes together close together in this case. So any case where the css is ABSOLUTELY just for a single page it is best in that page. Otherwise ALWAYS in a seperate css file Commented May 21, 2021 at 22:26

3 Answers 3

638

use double @@ symbols. That will escape @ symbol and render @media correctly on client side

Sign up to request clarification or add additional context in comments.

3 Comments

Using double @@ did not work for me. I had to use one @ with 2 different style elements! See my answer bellow, and maybe add it to this.
Thanks for this. I really wish people would not give their opinions unless asked and just answer the question as asked. I'm sure you have valid reasons for doing this and they should respect that.
Using double @@ works, BUT it appears that it will not. VS syntax turns the first @ symbol white and the second one blue, with a squiggly under the blue one saying "Unrecognized '@' block type.", but works!
48

Also remember to add a space after double @@:

 @@ media only screen and (max-width : 960px)

@@media with no space did not work for me.

4 Comments

Had a similar issue to this when using compressed CSS; I think the @@ was surrounded by text and so Razor struggled to interpret it. I opted to add the space before the @@. Thanks for the tip.
Even with the space, this did not work for me. Daut's answer with 2 different style elements did work though!
worked without space
@Dhruvil21_04 probably in the newer versions they have resolved this then.
20

I realize this is old question, but this is the only solution that worked for me:

@section cphPageHead{
    <style type="text/css" media="screen and (max-width:959px)">
    </style>


    <style type="text/css" media="screen and (min-width:960px)">
    </style>
}

2 Comments

As David stated: use double @@ symbols. That will escape @ symbol and render @media correctly on client side
This was the only one that worked for me too. I just needed a single <style> section, but the point of declaring the media attribute in the <style> tag itself instead of within the CSS stands. The double @ symbol did not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.