7

I am new to ASP.NET MVCs. I want to ask if there is a way to add CSS inside your view, but not using an external .css file? For example: table {border: 5px solid red;} (Just like you would do it in a normal HTML-CSS project)?

5
  • Assuming you chose the default Razor view engine, it's still just basically html, so you can do whatever you want. Commented Mar 6, 2017 at 19:14
  • @YuriyFaktorovich yes I use Razor ... Still if i write table, td, th { border: 5px solid red; } I get it written on my page... like normal text Commented Mar 6, 2017 at 19:19
  • @YuriyFaktorovich and this is css not html Commented Mar 6, 2017 at 19:20
  • Are you using the style tag? Commented Mar 6, 2017 at 19:31
  • @YuriyFaktorovich oh haha, now I see that I missed it, thanks Commented Mar 6, 2017 at 19:41

3 Answers 3

16

Consider using sections. In your layout add:

<head>
    ...
    @RenderSection("Styles", required: false)
</head>

Then, in your view:

@section Styles
{
    <style type="text/css">
        table { border: 5px solid red; }
    </style>
}

This will have the effect of adding this style tag into the head of your HTML where it should be, instead of littering style tags throughout your code.

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

Comments

4

You can also define CSS in razor view by using style tag as shown below:

<style>
    table {border: 5px solid red;}
</style>

Comments

1

Some like this.

@Html.Label("Hello World",new{@style="your style code"})

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.