ASP.Net CSS + CSS 语法问题中 Class 与 CSSClass 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1203228/
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
Difference between Class vs CSSClass in ASP.Net CSS + CSS syntax question
提问by tbone
What is the difference between:
有什么区别:
<asp:GridView CssClass="someclass"
and
和
<table class="someclass">
And how does it relate to how one defines CSS? For example, using CssClass, one can (I think) write CSS like so:
它与人们如何定义 CSS 有什么关系?例如,使用 CssClass,你可以(我认为)像这样编写 CSS:
.someclass {font-family:"arial";
background-color:#FFFFFF;
width: 100%;
font-size: small;}
.someclass th {background: #7AC142;
padding: 5px;
font-size:small;}
But using class, it seems this syntax doesn't work, and judging from http://www.w3.org/TR/css3-selectors/#class-htmlI would have to write the above like this:
但是使用类,似乎这种语法不起作用,从http://www.w3.org/TR/css3-selectors/#class-html来看,我必须像这样写上面的内容:
.someclass {font-family:"arial";
background-color:#FFFFFF;
width: 100%;
font-size: small;}
th.someclass {background: #7AC142;
padding: 5px;
font-size:small;}
Can someone shed some light on which is the correct way, or if they are both correct, but there is a difference between class and CssClass in ASP.Net?
有人可以阐明哪种方法是正确的,或者它们是否都正确,但是 ASP.Net 中的 class 和 CssClass 之间存在差异吗?
UPDATE
更新
Ok, looks like they are the same thing....so, are the above syntaxes both correct when using class or cssclass, because they don't seem to be.
好的,看起来它们是一样的......所以,使用 class 或 cssclass 时,上述语法是否都正确,因为它们似乎不是。
采纳答案by womp
ASP.Net CssClass is an abstract wrapper around the css "class" specifier.
ASP.Net CssClass 是围绕 css“类”说明符的抽象包装器。
Essentially, for most intents and purposes, they are the same thing. When you set the CssClass
property to some string like "someclass", the html that the WebControl will render will be class = "someclass"
.
从本质上讲,对于大多数意图和目的,它们是一回事。当您将该CssClass
属性设置为诸如“someclass”之类的字符串时,WebControl 将呈现的 html 将是class = "someclass"
.
EDIT: The CSS selectors you have written are both "correct", but they do two different things. ".someclass th" matches any descendantth element of an element that has the "someclass" class. The second one matches the th element itself that has the "someclass" class.
编辑:您编写的 CSS 选择器都是“正确的”,但它们做了两件事。“.someclass th”匹配具有“someclass”类的元素的任何后代元素。第二个匹配具有“someclass”类的第 th 个元素本身。
Hope that's clear. Regardless of the way you specify the class for the elements (using ASP.Net's CSSClass, or just setting the class), your CSS selectors will do the same thing. They don't have anything to do with ASP.Net specifically.
希望这很清楚。无论您为元素指定类的方式如何(使用 ASP.Net 的 CSSClass,或者只是设置类),您的 CSS 选择器都会做同样的事情。它们与 ASP.Net 没有任何关系。
回答by xypho
Actually, there is a difference between class and CssClass: the class will not be seen by the code behind, but the CssClass will.
实际上,class 和 CssClass 之间是有区别的:类不会被后面的代码看到,但是 CssClass 会。
Thus, if you add a new class to a control in your code behind, for example:
因此,如果您在后面的代码中向控件添加一个新类,例如:
myControl.CssClass += " foo";
while your control is set as follows:
而您的控件设置如下:
<asp:TextBox class="Text" ID="myControl" runat="server" />
(Note class attribute: class="Text"
)
(注class属性:class="Text"
)
When inspecting your rendered element in your browser, you will see that it will be rendered as follows:
在浏览器中检查呈现的元素时,您将看到它将呈现如下:
<input class=" foo" name="ctl00$MainContent$myControl" type="text" id="MainContent_myControl" >
(Note how the class has been overridden: class= " foo"
.)
(注意该类是如何被覆盖的:class= " foo"
.)
If you set the CssClass on the other hand:
另一方面,如果您设置 CssClass:
<asp:TextBox CssClass="Text" ID="myControl" runat="server" />
you will get it rendered (as expected) like so:
你会像这样渲染它(如预期的那样):
<input class="Text foo" name="ctl00$MainContent$myControl" type="text" id="MainContent_myControl">
(note the class has now both classes set, as expected! class="Text foo"
)
(请注意,该类现在已按预期设置了两个类!class="Text foo"
)
回答by Charlie
When you use the CssClass
attribute on an ASP.NET server control, it will render out as class
in the HTML.
当您CssClass
在 ASP.NET 服务器控件上使用该属性时,它将像class
在 HTML 中一样呈现。
For example, if I were to use a label tag in my mark up:
例如,如果我要在我的标记中使用标签标签:
<asp:label runat="server" CssClass="myStyle" AssociatedControlID="txtTitle" />
would render to:
将呈现为:
<label class="myStyle" for="txtTitle" />
回答by Paul van Brenk
There is no difference between the CssClass and class in Asp.Net other than the CssClass is a property of the Control and class is an attribute specified in the html. CssClass is rendered as the class attribute in Html.
除了 CssClass 是 Control 的属性和 class 是 html 中指定的属性之外,CssClass 和 Asp.Net 中的类之间没有区别。CssClass 在 Html 中呈现为 class 属性。
回答by Steve
Also note that CssClass="someclass anotherclass" works too as the string is carbon copied.
另请注意, CssClass="someclass anotherclass" 也可以使用,因为字符串是碳复制的。
回答by Punit Vora
so, are the above syntaxes both correct when using class or cssclass, because they don't seem to be.
因此,在使用 class 或 cssclass 时,上述语法是否都正确,因为它们似乎不正确。
I am not sure what you mean when you say they do not "seem" to be correct? Do they render differently, even when you are using <table class='someClass'>
in both cases?
当您说它们“似乎”不正确时,我不确定您的意思是什么?即使您<table class='someClass'>
在两种情况下都使用它们,它们的呈现方式是否不同?
IMHO, as far as the 'class' attribute is concerned, they are both correct. Did you try .someClass > th
instead of .someClass th
in the second case? That might solve the problem.
恕我直言,就“类”属性而言,它们都是正确的。您是否尝试过.someClass > th
而不是.someClass th
在第二种情况下?那可能会解决问题。