Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - Adding CSS to your view (inside of the view not an external .css file)

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)?

like image 642
john Avatar asked Mar 06 '17 19:03

john


2 Answers

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.

like image 81
Chris Pratt Avatar answered Sep 28 '22 05:09

Chris Pratt


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

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

Hope this helps...

like image 39
Murat Yıldız Avatar answered Sep 28 '22 05:09

Murat Yıldız