Home  >  Q&A  >  body text

Error with @media rule in <style> tag in .cshtml file

<p>Why put the following code in the "style" tag</p> <pre class="brush:php;toolbar:false;"><style type="text/css"> .on-the-fly-behavior { background-image: url('particular_ad.png'); } @media (max-width: 300px) { .on-the-fly-behavior { background-image: url('particular_ad_small.png'); } } </style></pre> <p>When displayed in a "style" tag in a .cshtml file, an error is thrown: </p> <pre class="brush:php;toolbar:false;">CS0103: The name 'media' does not exist in the current context</pre>
P粉769413355P粉769413355431 days ago521

reply all(2)I'll reply

  • P粉502608799

    P粉5026087992023-08-17 14:03:56

    try:

    <style type="text/css">
        .on-the-fly-behavior {
        background-image: url('particular_ad.png'); 
    }
    @media screen and (max-width: 300px) { /* 添加screen and */
        .on-the-fly-behavior {
            background-image: url('particular_ad_small.png');
        }
    }
    </style>

    reply
    0
  • P粉214176639

    P粉2141766392023-08-17 12:38:07

    In a .cshtml file, you cannot use it directly because it will be treated as a class or object. When you need to add content starting with @ in an html file, you need to add an additional @. Try using the following code:

    <style type="text/css">
        .on-the-fly-behavior {
            background-image: url('particular_ad.png'); 
        }
        @@media (max-width: 300px) {
            .on-the-fly-behavior {
                background-image: url('particular_ad_small.png');
            }
        }
    </style>

    reply
    0
  • Cancelreply