Html 如何将 .css 添加到 .cshtml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13144234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:38:16  来源:igfitidea点击:

How to add a .css to a .cshtml

htmlcssrazor

提问by Smoore

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?

我是一个完整的 html/css 新手,我正在尝试获取一个 .cshtml 文件来使用我编写的一些基本 CSS。我需要在我的 .cshtml 中放入什么代码才能使用 CSS 文件?

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

编辑:这是我的 .css 文件中的代码。它旨在div使用“comment_box1”的 id来设计我的样式,但即使按照答案进行操作,它也不起作用。知道出了什么问题吗?

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

回答by Fenton

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

这是基本选项 - 您将样式标签添加到<head>文档的...

<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.

如果您的目标是 id,则使用#,而不是点,后者用于类。

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

回答by Adarsh Punj

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:

您也可以使用 while 在 .cshtml 文件中使用 CSS。在这种情况下,请将以下代码直接粘贴到您的 .cshtml 文件中:

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