.cshtml文件中<style>标记中的@media规则引发错误

use*_*544 1 css razor

为什么要低于线

<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>
Run Code Online (Sandbox Code Playgroud)

在浏览器中显示时,.cshtml文件中的“样式”标签中的抛出错误:

CS0103: The name 'media' does not exist in the current context
Run Code Online (Sandbox Code Playgroud)

Zai*_*tab 6

在.cshtml中,不能按原样使用它,因为它将被视为类或对象。@每当您需要添加@以html文件开头的任何内容时,都需要添加额外的内容。试试这个

<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>
Run Code Online (Sandbox Code Playgroud)