Home ASP.NET MVC razor: conditional attribute in CSS block
Reply: 1

ASP.NET MVC razor: conditional attribute in CSS block

Yich Lin
1#
Yich Lin Published in 2017-11-15 03:01:29Z

Can I use if condition in style tag like this?

<style>
    @@media (min-width: 765px) {
    html,body 
       {
        min-width: auto;
        width: 100%;
        @{
            if (ViewBag.type == "pic")
            {
                @:min-height:100vh;
                @:height:auto;
            }
            else {
                @:height: 100vh;
            }
          }
       }
    }
</style>

Or I should use CSS directly in HTML?

Mario Lopez
2#
Mario Lopez Reply to 2017-11-15 04:59:03Z

You should put your css in css files and target specific classes instead of loading files conditionally. Then you can use Razor to include conditional classes on your tags. Something like:

@{
    var imgType = ViewBag.type == "mobile"?"for-mobile":"for-desktop";
}

<img class="@imgType" src="..."/>

Then in your CSS file:

.for-mobile{
      min-height:100vh;
      height:auto;
}

.for-desktop{
     height: 100vh;
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.311828 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO