gpt4 book ai didi

css - 如何使用 ViewData 更改特定 View 的 css

转载 作者:行者123 更新时间:2023-11-28 03:42:33 25 4
gpt4 key购买 nike

所以我有一个使用布局的 View ,我想只为他更改 body 的 css,我偶然发现了这个解决方案:

我的观点:

@{
ViewData["PageId"] = "Login";
}

CSS:

body#Login {
padding-top: 0;
background: #000428;
background: -webkit-linear-gradient(to left, #004e92, #000428);
background: linear-gradient(to left, #004e92, #000428);
}

但是当我决定我也想更改 html 的 css 时,它不起作用吗?

html#Login {
overflow-y: auto;
}

谁能解释一下它是如何工作的,为什么 body#Login 之间不能有任何空格

最佳答案

您的选择器需要 body 标签有一个名为 Login 的 Id,如下所示:

<body id="Login">
This is the Target Element of your Selector
</body>

因此,如果主体没有此 Id,则选择器不匹配。一个不匹配的例子可能是这个:

<body>
This element has no Id called Login and so Css wouldn't apply
</body>

可以用一些额外的代码覆盖body,没错。但请记住,顺序在 CSS 中很重要!因此,您必须在要覆盖的原始 CSS 代码之后包含第二个代码。当 CSS 加载到 _Layout 文件中时,您可以使用一个部分,例如,确保它在共享 CSS 之后应用以覆盖它:

<link rel="stylesheet" rel="css/master-shared-stylesheet.css">
@RenderSection("AdditionalPageCss")

然后你可以在 View 中做这样的事情,你想覆盖 master-shared-stylesheet.css:

@section AdditionalPageCss {
<style type="text/css">
/* Overwrite */
body {
overflow-y: auto;
}
</style>
}

关于css - 如何使用 ViewData 更改特定 View 的 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44140898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com