所以你可以通过这种方式使用 css id 选择器。
#layoutsectiondiv { color: red }
使用以下html
<div id="layoutsectiondiv">
</div>
或者像这样的 css 类 html 选择器。
.layoutsectiondiv { color: blue }
使用以下html
<div class="layoutsectiondiv">
</div>
如果您想控制特定 .net 控件的样式,即具有 runat="server" 属性的控件,那么我们知道 .net 会“修改”id 以确保其唯一性。
在这种情况下,在我们的代码中,我们可以使用 FindControl 来访问 div 并更改其样式
<div id="testDiv" runat="server">
</div>
即。
HtmlGenericControl testDiv =
(HtmlGenericControl)Page.FindControl("testDiv");
// to hide
testDiv.Attributes.Add("style", "display: none"); // OR
testDiv.Attributes["style"] = "display: none";
// to show
testDiv.Attributes.Add("style", "display: block"); // OR
testDiv.Attributes["style"] = "display: block";
// or to add a class
testDiv.Attributes.Add("class", "MyCssClassName"); // OR
testDiv.Attributes["class"] = "MyCssClassName";
这里很好地解释了 css id 和 class 之间的区别 - CSS: div id VS. div class。
这里是How to edit CSS style of a div using C# in .NET