无法使用我自己的样式表覆盖引导程序样式 [英] Can't override bootstrap style with my own stylesheet
问题描述
ASP MVC RAZOR
我尝试覆盖引导程序默认样式.我声明了自己的样式表,如下所示:
I try to override the bootstrap default style. I declared my stylesheet as below:
/*bootstrap and default style*/
@Styles.Render("~/Content/css");
/*My CSS*/
<link href="@Url.Content("~/css/main.css")" rel="stylesheet" type="text/css" />
@RenderSection("Styles", required: false)
在bundleconfig中:(仅默认样式)
In the bundleconfig:(only the default styles)
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/site.css" ));
但是正如您在下面看到的那样,我的样式已被bootstrap覆盖.
But as you can see below my style is overriden by bootstrap .
我认为这与head标签中样式的顺序有关.但是我是在引导程序之后才来的,所以它应该可以工作...
I thought it has something to do with the order of the style in the head tag. But mine comes after bootstrap so it should work...
我复制了完整的版面设计页面,以便您查看发生了什么事情.
I copy the full layout page so you can check what's going on.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title</title>
<link href="@Url.Content("https://fonts.googleapis.com/css?family=Roboto")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("https://fonts.googleapis.com/css?family=Tangerine")" rel="stylesheet" type="text/css" />
@Styles.Render("~/Content/css");
<link href="@Url.Content("~/css/main.css")" rel="stylesheet" type="text/css" />
@RenderSection("Styles", required: false)
</head>
<body>
<div class="cachetop col-lg-12 navbar-fixed-top"></div>
<div class=" topmenubar navbar navbar-default navbar-fixed-top ">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-left " href="@Url.Action("Accueil","Home")">
<img src="@Url.Content("../img/minilogo.png")" />
</a>
</div>
<div class=" navbar-collapse collapse">
<ul class="menuitembar nav navbar-nav">
<li class="menuitem nav-item active">@Html.ActionLink("Notre carte", "pizzas", "Carte", null, new { @class = "nav-link" })</li>
<li class="menuitem nav-item">@Html.ActionLink("Nous trouver", "Localisation", "Home", null, new { @class = "nav-link" })</li>
<li class="menuitem nav-item">@Html.ActionLink("Un peu d'histoire", "Histoire", "Home", null, new { @class = "nav-link" })</li>
<li class="menuitem nav-item">@Html.ActionLink("Travailler chez Michel", "Workwith", "Home", null, new { @class = "nav-link" })</li>
</ul>
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<div class="text-center">
<p>
</p>
</div>
</footer>
</div>
@Scripts.Render("~/bundles/jquery")
@RenderSection("Javascript", required: false)
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/modernizr")
</body>
</html>
推荐答案
If you make your CSS selectors more specific, there is a level of specificity that determines which styles get applied. So doing:
li.menuitem { color: black; }
使其更具体.另外添加!important 表示此样式将有优先权:
Makes it more specific. Also adding !important indicates that this style would have priority:
.menuitem { color: black !important; }
注意嵌套的CSS层次结构并使用!important.如果您想在不同的上下文中覆盖这些更改,则可能会导致问题.仅在必要时使用,但可以使用.
Careful with nested CSS hierarchies and using !important. It could cause issues if you want to override those changes in a different context. Only use when necessary, but it's OK to use.
这篇关于无法使用我自己的样式表覆盖引导程序样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!