Smoore
Smoore

Reputation: 772

How to add a .css to a .cshtml

I'm a complete HTML/CSS novice and I am trying to get a .cshtml file to use some basic CSS I wrote. What code do I need to put in my .cshtml to get it to use the CSS file?

Edit: This is the code in my .css file. It is intended to style my div with the id of "comment_box1", but even after following the answer, it's not working. Any idea what's wrong?

.comment_box1 {
    background-color: #C8E0E8; 
    width: 830px; 
    height: 180px; 
}

Upvotes: 19

Views: 99753

Answers (3)

AminSojoudi
AminSojoudi

Reputation: 2006

dotnet core

First, make sure you have wwwroot folder and put your css file inside it.

Second, check if you have app.UseStaticFiles(); in you startup.cs Configure method

Third, link to your files using a path like this ~/style.css based on the path from wwwroot

Reference

Upvotes: 1

You could also make the use of while using CSS in .cshtml files. In this case, paste the following code directly into your .cshtml file:

<style>
#comment_box1
{
background-color: #C8E0E8; 
width: 830px; 
height: 180px; 
}
</style>

Upvotes: 12

Fenton
Fenton

Reputation: 250832

Here is the basic option - you add a style tag to the <head> of your document...

<link rel="stylesheet" href="app.css" type="text/css" />

If you are using bundles, you place the bundle there instead:

@Styles.Render("~/Content/css")

And finally, if you are using a master layout, that's the best place to put this as it will then apply to all your pages.

Update

If you are targeting an id, you use #, rather than the dot, which is for a class.

#comment_box1
{
background-color: #C8E0E8; 
width: 830px; 
height: 180px; 
}

Upvotes: 46

Related Questions