0

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?

2
  • why you want condition in css you can try to apply on html tags stackoverflow.com/questions/11785166/… Commented Nov 15, 2017 at 3:18
  • This CSS is for mobile device.Maybe I should write in difference file then dynamic load it. Commented Nov 15, 2017 at 3:24

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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.