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)?
Asked
Active
Viewed 3.1k times
7

john
- 71
- 1
- 1
- 2
-
Assuming you chose the default Razor view engine, it's still just basically html, so you can do whatever you want. – Yuriy Faktorovich Mar 06 '17 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 – john Mar 06 '17 at 19:19
-
@YuriyFaktorovich and this is css not html – john Mar 06 '17 at 19:20
-
Are you using the style tag? – Yuriy Faktorovich Mar 06 '17 at 19:31
-
@YuriyFaktorovich oh haha, now I see that I missed it, thanks – john Mar 06 '17 at 19:41
3 Answers
15
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.

Chris Pratt
- 232,153
- 36
- 385
- 444
4
You can also define
CSS
inrazor
view by using style tag as shown below:
<style>
table {border: 5px solid red;}
</style>

Murat Yıldız
- 11,299
- 6
- 63
- 63
1
Some like this.
@Html.Label("Hello World",new{@style="your style code"})